调试程序,在文件中计算前10个值和之后的10个值。

huangapple go评论79阅读模式
英文:

Debugging program that sums first 10 values on a file and the next 10 values after

问题

我正在编写一个程序,该程序应该对文件中的前10个值和文件中的后10个值进行求和。后10个值是前10个值的副本,因此答案应该是相同的。我的程序会检查文件是否已找到,并在未找到文件时停止执行。以下是我拥有的代码:

package experiment8;

import java.io.*;
import java.util.Scanner;

public class Bugs
{
  public static Scanner inData;

  public static void main(String[] args) throws IOException
  {
    int fileFound;
    try
    {
      inData = new Scanner(new FileReader("Bugs.dat"));
      fileFound = 1;
    }
    catch (IOException exception)
    {
      fileFound = 2;
    }
    int value;
    switch (fileFound)
    {

      case 1 :
             // do loop
             int counter = 1;
             int sum = 0;
             do
             {
               value = inData.nextInt();
               sum = sum + value;
             } while (counter <= 10);
             System.out.println(sum);
             // for loop
             sum = 0;
             for (counter = 1; counter <= 10; counter++)
             {
               value = inData.nextInt();
               sum = sum + value;
               counter++;
             }
            System.out.println(sum);
      case 2 : System.out.println("Bugs.dat not found");
    }
  }
}

而这是程序应该查看的文件(名为 "Bugs")的内容:

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

当我运行该程序时,它输出以下内容:

Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at lab8.Bugs.main(Bugs.java:31)
英文:

I am coding a program that is supposed to sum the first 10 values on a file and the second 10 values on a file. The second 10 values are a duplicate of the first 10, so the answers should be the same. My program checks to make sure that the file has been found and halts execution if it has not been found. This is the code I have:

package experiment8;

import java.io.*;
import java.util.Scanner;
public class Bugs
{
  public static Scanner inData;

  public static void main(String[] args) throws IOException
  {
    int fileFound;
    try
    {
      inData = new Scanner(new FileReader(&quot;Bugs.dat&quot;));
      fileFound = 1;
    }
    catch (IOException exception)
    {
       fileFound = 2;  
    }
    int  value;
    switch (fileFound)
    {
      
      case 1 :     
             // do loop
             int  counter = 1;
             int  sum = 0;
             do
             {
               value = inData.nextInt();
               sum = sum + value;
             } while (counter &lt;= 10);
             System.out.println(sum);
             // for loop
             sum = 0;
             for (counter = 1; counter &lt;= 10; counter++)
             {
               value = inData.nextInt();
               sum = sum + value;
               counter++;
             }
            System.out.println(sum);
      case 2 : System.out.println(&quot;Bugs.dat not found&quot;);
    }
  }
}

And this is the content of the file (called "Bugs") the program is supposed to be looking at:

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

When I run the program, it outputs the following:

Exception in thread &quot;main&quot; java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at lab8.Bugs.main(Bugs.java:31)

答案1

得分: 1

你的do-while循环似乎没有增加计数器的值。另外,我不确定为什么在你的for循环中添加了counter++,因为在for循环的声明中已经设置了它。

在你的do-while循环中添加counter++,并从for循环内部移除它。

英文:

Your do-while loop doesn't seem to increment your counter. Also, I'm not sure why you're adding a counter++ in your for loop, its already set in the for loop declaration itself.

Add the counter++ in your do-while loop and remove it from inside the for loop.

huangapple
  • 本文由 发表于 2020年4月6日 22:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定