我有一个for循环,可以在.txt文件(bday.readWord())获取数据。但是我无法弄清楚如何将其调用为for循环值,因此我可以获得DayCount的不同值。我需要DayCount的五个不同的值,所以我可以对它进行排序,并能够计算出最短的日期。
for(int diffinday = 0; diffinday < myStrings.length; diffinday++) {
// making another copy
bmonth = Integer.parseInt(myStrings[0]);
bday = Integer.parseInt(myStrings[1]);
byear = Integer.parseInt(myStrings[2]);
LocalDate start = new LocalDate(year, month, day);
LocalDate end = new LocalDate(year, bmonth, bday);
int dayCount = Days.daysBetween(start, end).getDays();
System.out.println(dayCount);
// making an int array;
// we can do 2 things make an if statement that
// will identify or make an array that will store
// the differneces in the dates!
// we make an array that will sort out the dates
// bad code incoming!
// what if it were 100 lines hmmm?
int [] myArray = new int [] { dayCount, dayCount, dayCount, dayCount, dayCount };
myArray[diffinday] = dayCount;
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));
if(dayCount < 0) {
System.out.println("This value is null!");
} else {
// for now, we can only identify pos ints.
// we need to make an array that will find
// the closest date, W/O IT BEING NEGATIVE
System.out.println("The closest birthday is " + myStrings[4]);
}
dayCount应该是单独的 .txt 文件中的[-238, -196, -103, -76, 96]
b / c,该程序应该计算日期之间的差异。自myStrings.length
为5以来该程序的输出是
[-238,-238,-238,-238,-238]
[-196,-196,-196,-196,-196]
... so on and so forth
如果能够调出正确的循环值,W / O为负数,那么你也可以帮我理清DayCount。任何帮助表示赞赏。
答案 0 :(得分:1)
您可以像这样设置数组:
int[] myArray = new int[5];
对Java说,&#34;我想要一个新的整数数组,它可以存储5个项目&#34;。
因此,您可以创建任何大小的数组:
int x = 7;
int[] myArray = new int[x]
最初将值设置为null
,然后您可以使用for
循环来正确设置它们。
您还可以使用ArrayList
,可以动态添加和删除项目。
例如,如果我使用简单的Scanner
和ArrayList
,我可以执行以下操作:
Scanner scanner = new Scanner();
ArrayList<Integer> myArrayList = new ArrayList<Integer>(); //Make the ArrayList
while(scanner.hasNextInt()) {
myArrayList.add(scanner.nextInt()); //Add an item to the ArrayList
}
for (Integer i:myArrayList) { //Loop through all items in the ArrayList
System.out.println(i);
}