英文:
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("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");
}
}
}
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 "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)
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论