从数组中的文件中捕获值
Capturing values from file in array
我有以下代码,它从文件中读取数字,然后为 10 位数字范围内出现的数字打印星号。问题是我的 for 循环将数字添加到数组。每当我执行代码时,它都会打印文件中的最后一个数字 (22) 20 次,并在 21-30 字符串中列出一个星号。我确定这一定是一个简单的订单问题。我错过了什么?
import java.util.Scanner;
import java.io.*;
public class histogram {
public static void main(String[] args) {
try {
final File numbers = new File("numbers.txt");
Scanner filescan = new Scanner(numbers);
int[] list = new int[101];
while (filescan.hasNext()) {
int num = filescan.nextInt();
for(int i = 0; i < 101; i++)
list[i] = num;
}
for(int value : list)
System.out.println(value);
String[] Stars = {"1-10 |", "11-20 |", "21-30 |", "31-40 |", "41-50 |", "51-60 |", "61-70 |", "71-80 |", "81-90 |", "91-100 |"};
int number = 0;
for (int st = 0; st < 101; st++)
number = list[st];
if (number < 11)
Stars[0] += "*";
else if (number < 21)
Stars[1] += "*";
else if (number < 31)
Stars[2] += "*";
else if (number < 41)
Stars[3] += "*";
else if (number < 51)
Stars[4] += "*";
else if (number < 61)
Stars[5] += "*";
else if (number < 71)
Stars[6] += "*";
else if (number < 81)
Stars[7] += "*";
else if (number < 91)
Stars[8] += "*";
else if (number < 101)
Stars[9] += "*";
for (int print = 0; print < 10; print++)
System.out.println(Stars[print]);
filescan.close();
} catch (FileNotFoundException ex) {
}
}
}
我觉得这个-[=12=]
while (filescan.hasNext()) {
int num = filescan.nextInt();
for(int i = 0; i < 101; i++)
list[i] = num;
}
应该是这个-
int i = 0, num;
while (filescan.hasNext()) {
num = filescan.nextInt();
list[i] = num;
if(i == 100)
break;
i++;
}
我有以下代码,它从文件中读取数字,然后为 10 位数字范围内出现的数字打印星号。问题是我的 for 循环将数字添加到数组。每当我执行代码时,它都会打印文件中的最后一个数字 (22) 20 次,并在 21-30 字符串中列出一个星号。我确定这一定是一个简单的订单问题。我错过了什么?
import java.util.Scanner;
import java.io.*;
public class histogram {
public static void main(String[] args) {
try {
final File numbers = new File("numbers.txt");
Scanner filescan = new Scanner(numbers);
int[] list = new int[101];
while (filescan.hasNext()) {
int num = filescan.nextInt();
for(int i = 0; i < 101; i++)
list[i] = num;
}
for(int value : list)
System.out.println(value);
String[] Stars = {"1-10 |", "11-20 |", "21-30 |", "31-40 |", "41-50 |", "51-60 |", "61-70 |", "71-80 |", "81-90 |", "91-100 |"};
int number = 0;
for (int st = 0; st < 101; st++)
number = list[st];
if (number < 11)
Stars[0] += "*";
else if (number < 21)
Stars[1] += "*";
else if (number < 31)
Stars[2] += "*";
else if (number < 41)
Stars[3] += "*";
else if (number < 51)
Stars[4] += "*";
else if (number < 61)
Stars[5] += "*";
else if (number < 71)
Stars[6] += "*";
else if (number < 81)
Stars[7] += "*";
else if (number < 91)
Stars[8] += "*";
else if (number < 101)
Stars[9] += "*";
for (int print = 0; print < 10; print++)
System.out.println(Stars[print]);
filescan.close();
} catch (FileNotFoundException ex) {
}
}
}
我觉得这个-[=12=]
while (filescan.hasNext()) {
int num = filescan.nextInt();
for(int i = 0; i < 101; i++)
list[i] = num;
}
应该是这个-
int i = 0, num;
while (filescan.hasNext()) {
num = filescan.nextInt();
list[i] = num;
if(i == 100)
break;
i++;
}