这个简单的高尔夫游戏Java代码有什么问题?
我迷路了。在我的Java1类中,我应该调试并修复这个简单的代码。这是一场简单的高尔夫比赛。我知道这个问题基本上是要求你们也做我的家庭作业,但我想帮助你们在未来的调试任务中找到正确的方向 GolfGame.java这个简单的高尔夫游戏Java代码有什么问题?,java,debugging,Java,Debugging,我迷路了。在我的Java1类中,我应该调试并修复这个简单的代码。这是一场简单的高尔夫比赛。我知道这个问题基本上是要求你们也做我的家庭作业,但我想帮助你们在未来的调试任务中找到正确的方向 GolfGame.java import java.util.Scanner; /* * Debugging Exercise - Chapter 4 * * Debug the error(s) and submit to the Dropbox on Angel * Please do not su
import java.util.Scanner;
/*
* Debugging Exercise - Chapter 4
*
* Debug the error(s) and submit to the Dropbox on Angel
* Please do not submit if it is not debugged
*
*/
///////////////////////////////////////////////////////////////////////////////
// READ ME FIRST:
// This program compiles, but, there is logic error in the while statement.
// Debug the logic error and run the program to calculate your golf score.
// Try inputting several numbers to get the total score.
// The program should keep looping until the user selects -1 to terminate.
///////////////////////////////////////////////////////////////////////////////
public class GolfGame {
Scanner input = new Scanner(System.in);
public void getTotalScore() {
int score = 0, total = 0;
while ( score == -1 )
{
System.out.print("Please enter a score [-1 to quit]: ");
score = input.nextInt();
System.out.println();
total += score;
}
if (total != -1)
System.out.println("Your total score is " + total);
}
}
/*
* This is the MAIN class; RUN this class to show
* that the GolfGame.java program functions correctly.
*
* NOTE: You must first debug the GolfGame.java file.
* There is no need to debug this file.
*/
public class GolfGameTest {
public static void main(String[] args)
{
System.out.println("Golf Game Calculator");
GolfGame golfGame = new GolfGame();
golfGame.getTotalScore();
}
}
GolfGameTest.java
import java.util.Scanner;
/*
* Debugging Exercise - Chapter 4
*
* Debug the error(s) and submit to the Dropbox on Angel
* Please do not submit if it is not debugged
*
*/
///////////////////////////////////////////////////////////////////////////////
// READ ME FIRST:
// This program compiles, but, there is logic error in the while statement.
// Debug the logic error and run the program to calculate your golf score.
// Try inputting several numbers to get the total score.
// The program should keep looping until the user selects -1 to terminate.
///////////////////////////////////////////////////////////////////////////////
public class GolfGame {
Scanner input = new Scanner(System.in);
public void getTotalScore() {
int score = 0, total = 0;
while ( score == -1 )
{
System.out.print("Please enter a score [-1 to quit]: ");
score = input.nextInt();
System.out.println();
total += score;
}
if (total != -1)
System.out.println("Your total score is " + total);
}
}
/*
* This is the MAIN class; RUN this class to show
* that the GolfGame.java program functions correctly.
*
* NOTE: You must first debug the GolfGame.java file.
* There is no need to debug this file.
*/
public class GolfGameTest {
public static void main(String[] args)
{
System.out.println("Golf Game Calculator");
GolfGame golfGame = new GolfGame();
golfGame.getTotalScore();
}
}
永远不会进入while循环
向正确的方向推动…查看循环控制。如果您在进入循环时遇到困难,那么下一个更痛苦的缺陷就在不远处,“无限循环”
在对循环进行编码时,实践是在几次迭代中,在思想上或纸面上跟踪循环控制变量,确保:
上述排序的原因是基于未能这样做导致缺陷的次数百分比。如果我可以回到开始编程时,自学一件事,那就是使用调试器的基础知识 它将真正帮助您了解netbeans的调试特性。为了更好地解决调试问题,这可能是最重要的一件事。我建议从以下几点开始学习:
在您的示例中,您可以一步一步地浏览代码,并看到您从未将其放入while循环。您还可以查看score变量的值(即0),并查看“while(score=-1)”中的条件是否为true。应为
while(score!=-1)
请不要在此处转储作业。展示你的作品,解释你的困境,提出具体的问题。@hoverskifullofeels我不是在问答案,我是在要求往正确的方向推进。我不是说太多,把作业扔在上面就行了。谢谢你的提醒。@JordanSimps:你所需要做的就是在问这个问题时表现出更多的努力;这将有很长的路要走。展示你所做的工作,并提出更具体的问题。否则,类似的问题通常会被关闭。谢谢你接受我的批评。谢谢。这是一个完美的答案,我很感激没有给我答案。我不明白为什么我会被降级,但不管怎样。