-2

我在完成我的程序时遇到了一些麻烦。这是我得到的和说明。创建一个包含 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();
}
4

2 回答 2

1

我无法使用随机数创建文件

您正在尝试打开一个不存在的文件:

File numfile = new File("randomdata.txt");
Scanner infile = new Scanner(numfile); 

构造Scanner函数试图打开文件,但它还不存在,所以它抛出一个FileNotFoundException. 您不需要Scanner,因为您首先要创建该文件。使用 aFileWriter代替:

FileWriter fw = new FileWriter("randomdata.txt");
for (int i = 0; i < 1000; i++){
    num = gen.nextInt(500);
    fw.write(num + "\n");
}
fw.close();

您可能还需要检查for-loop - 它会生成 0..500 范围内的 1000 个数字,反之亦然。

然后,您需要重新设计您的while-loop:

  • 从数据文件中删除读取最大/最小的行。这需要计算,而不是从数据文件中读取。
  • 将结果的打印移到循环之外 - 您不需要在每次循环迭代时打印它
  • 初始化largestsmallest使用合理的值
  • 将平均值的计算从for-loopwhile移到 -loop中

有了这个,您应该有一个工作程序,您可以在其中检查结果是否符合您的预期。

确定连续数字出现的次数

想一想如何解决它 - 您想知道当前读取的数字是否等于先前读取的数字 + 1。因此,您需要在while-loop 中跟踪先前读取的数字,类似于您对largestand所做的事情smallest。请注意第一次循环迭代,其中尚不存在先前的数字。

顺便说一句:我故意没有在代码中给出完整的答案;)

于 2013-03-19T15:39:47.437 回答
0

首先,我们不是来做你的功课的 ;-)

第二,先写下你想要的。

例如:

  • 创建文件
  • 将随机数写入文件(0-1000)(是否允许有两次数字?)
  • 关闭/保存文件

  • 打开文件

  • 读取文件
  • 找到最大的数字
  • 找到最小的数字
  • 计算平均值
  • 计算最高的连续值

在使用 java 进行尝试之前,将其转换为您理解的逻辑。

第4步:你如何计算纸上连续数字的数量?fe - 每次新数字比之前的数字高一时,增加 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);
于 2013-03-19T15:42:18.127 回答