我迷路了。在我的 Java 1 类中,我应该调试这个简单的代码并修复它。这是一个简单的高尔夫游戏。我知道这个问题基本上是在问你们也做我的功课,但我希望得到帮助,以便在未来的调试任务中朝着正确的方向前进。
高尔夫游戏.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);
}
}
GolfGameTest.java
/*
* 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();
}
}