我在整理程序时遇到了一些麻烦。这是我得到的和指示。创建一个包含500个随机数的文件,范围从0到1000,使用for循环将数字写入文件。然后1.找到最小值2.找到最大值3.找到平均值4.确定连续数字的出现次数。我有创建随机数文件和第4步的问题。提前感谢!
public static void main(String[] args) throws FileNotFoundException {
PrintWriter prw = new PrintWriter("results.txt");
File numfile = new File("randomdata.txt");
Scanner infile = new Scanner(numfile);
int num, largest, smallest, sum = 0, count = 0;
int programnumber = 6;
header (prw, programnumber );
double average = 0;
Random gen = new Random();
System.out.println("From gen: ");
for (int i = 0; i < 1000; i++){
System.out.print(gen.nextInt(500) + "");
num = gen.nextInt(500);
System.out.println(num);
sum = sum + num;
count++;
average = (double) sum / count;
}
while (infile.hasNext()){
num = infile.nextInt();
largest = infile.nextInt();
smallest = infile.nextInt();
System.out.println(num);
if (num < largest){
largest = num;
}
if (num > smallest){
smallest = num;
}
System.out.println("The largest is: " + largest);
prw.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
prw.println("The smallest is: " + smallest);
System.out.println("The average is: " + average);
prw.println("The average is: " + average);
}
prw.close();
}
答案 0 :(得分:1)
我无法使用随机数
创建文件
您正在尝试打开不存在的文件:
File numfile = new File("randomdata.txt");
Scanner infile = new Scanner(numfile);
Scanner
构造函数尝试打开该文件,但它尚不存在,因此会抛出FileNotFoundException
。您还不需要Scanner
,因为您首先要创建该文件。改为使用FileWriter
:
FileWriter fw = new FileWriter("randomdata.txt");
for (int i = 0; i < 1000; i++){
num = gen.nextInt(500);
fw.write(num + "\n");
}
fw.close();
您可能还需要检查for
- 循环 - 它会生成0到500范围内的1000个数字,反之亦然。
然后,您需要重新设计while
- 循环:
largest
和smallest
for
- 循环while
- 循环的平均值计算
有了这个,你应该有一个工作程序,在那里你可以检查结果是否符合预期。
确定连续数字出现的次数
考虑如何解决它 - 您想知道当前读取的数字是否等于先前读取的数字+ 1.因此,您需要跟踪while
- 循环中先前读取的数字,类似于您对largest
和smallest
所做的工作。注意第一个循环迭代,其中还没有先前的数字。
BTW:我故意没有在代码中给出完整的答案;)
答案 1 :(得分:0)
首先,我们在这里做你的功课; - )
第二,先写下你想要的东西。
例如:
关闭/保存文件
打开文件
在将其用于java之前,将其转换为您理解的逻辑。
第4步:你如何计算纸上连续数字的数量?
F.E.
- 每当一个新数字高于之前的数字时,增加临时变量。
- 每次不是(新值>旧值)将临时变量与最高连续变量进行比较并保存最高变量。之后重置temp变量
这将导致:
int largestConsecutive = 0;
int largestConsecutiveTemp=0;
int previewValue =9999;
// loop through the lines of the file, each Next elements represents one number.
// Use this loop to calc the result values, not to print out on every new number
while (infile.hasNext()){
...
if(num > previewValue){
//do something
} else {
//do something
}
...
}
在寻找最小/最大数字时,while循环也是错误的:
while (infile.hasNext()){
num = infile.nextInt();
largest = infile.nextInt();
smallest = infile.nextInt();
System.out.println(num);
if (num < largest){
largest = num;
}
if (num > smallest){
smallest = num;
}
System.out.println("The largest is: " + largest);
prw.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
prw.println("The smallest is: " + smallest);
System.out.println("The average is: " + average);
prw.println("The average is: " + average);
}
每次最小/最大变量时都会覆盖。
largest = 0; // 0, because when you compare with numbers between 0-1000, the new value is larger
smallest = 9999; // 0, would be wrong here as it would be abvious the smallest number
total = 0;
amount=0;
// loop through the lines of the file, each Next elements represents one number.
// Use this loop to calc the result values, not to print out on every new number
while (infile.hasNext()){
num = infile.nextInt();
// override the largest variable with the new largest number
if (num > largest){
largest = num;
}
// override the smallest variable with the new smallest number
if (num < smallest){
smallest = num;
}
// needed to calc the average
total += num;
amount++;
}
average = (total / amount);
System.out.println("The largest is: " + largest);
prw.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
prw.println("The smallest is: " + smallest);
System.out.println("The average is: " + average);
prw.println("The average is: " + average);