我错过了将实现放在驱动程序类中的要点,而不是让驱动程序类只在其他类中调用方法。我不确定如何将实现移到主类之外。建议欢迎!
/*
*Deitel Chapter 6 Exercise 6.30
*/
package guess;
import java.util.Scanner;
/**
*Driver class for number guessing game
*/
public class GuessDriver {
/**
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*
*instantiate guess class
*/
Guess Guess = new Guess();
/*
*loops program until user guesses correct number
*/
while (Guess.getGuess() != Guess.secretNumber){
/*
*takes input from user
n*/
System.out.println("Enter a number from 1 to 1000: ");
int setGuess = input.nextInt();
Guess.setGuess(setGuess);
/*
*nested if-else statements compares guess against secret number
*/
if(Guess.secretNumber < Guess.guess){
System.out.println("too high");
} else if (Guess.secretNumber > Guess.guess){
System.out.println("too low");
} else {
System.out.println("Congratulations, you win!");
}
}
}
}
/*
*Deitel Chapter 6 Exercise 6.30
*/
package guess;
import java.util.Random;
/**
* Takes in and stores an integer, sets value for random number
*/
public class Guess {
/**
*instance variable
*/
public int guess;
/**
*instance variable
*/
public int secretNumber = (int) (Math.random() * 999 + 1);
/**
*return secret number
* @return
*/
public int getSecretNumber() {
return secretNumber;
}
/**
*return guess
* @return
*/
public int getGuess() {
return guess;
}
/**
*set guess
* @param guess
*/
public void setGuess(int guess) {
this.guess = guess;
}
}
答案 0 :(得分:1)
嗯,首先。我建议私有Guess类的所有变量。 (看到你有一个getSecretNumber()方法,它已经为你提供了secretNumber变量的值。
其次,你可以在Guess类中创建一个像checkGuessing(int guess)这样的公共方法。此方法将接受用户输入,并在控制台上写入答案(&#34;太高&#34;,&#34;太低&#34;等等)。 另一个选择可能是将相同的功能添加到setGuess方法。
最后,为了简化将来的调试,您应该使用大写字母,变量(甚至是类的实例)用camelCase字母命名类,并尝试将类的变量命名为与类的名称不同
所以最终的代码可能是这样的: 在GuessDriver班:
package guessdriver;
import java.util.Scanner;
/**
*Driver class for number guessing game
*/
public class GuessDriver {
/**
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*
*instantiate guess class
*/
Guess guess = new Guess();
/*
*loops program until user guesses correct number
*/
while (guess.checkGuessing()==false){
/*
*takes input from user
n*/
System.out.println("Enter a number from 1 to 1000: ");
int setGuess = input.nextInt();
guess.setGuess(setGuess);
}
}
}
在Guess课程中:
package guessdriver;
public class Guess {
/**
*instance variable
*/
private int guessing;
/**
*instance variable
*/
private int secretNumber = (int) (Math.random() * 999 + 1);
/**
*return secret number
* @return
*/
public int getSecretNumber() {
return secretNumber;
}
/**
*return guess
* @return
*/
public int getGuess() {
return guessing;
}
/**
*set guess
* @param guess
*/
public void setGuess(int guessing) {
this.guessing = guessing;
}
public boolean checkGuessing(){
if(secretNumber < guessing){
System.out.println("too high");
return false;
} else if (secretNumber > guessing){
System.out.println("too low");
return false;
} else {
System.out.println("Congratulations, you win!");
return true;
}
}
}