我希望在这里允许发布大量的代码,但我对分配没有好运。我们必须创建一个游戏,计算机生成一个随机数,用户有七次机会猜测它。问题是我们必须验证每个用户输入,也就是说,我认为,我正在分崩离析。我们的讲师坚定地声明,用户输入和整数以及该整数是否在指定范围内的验证应采用不同的方法。我拙劣的事情,但因为我甚至不确定错误是什么我在这里发布它。我希望这不违反任何规则。
import java.util.*;
public class RandomNumber {
public static void main(String[] args) {
// Welcome user to program
System.out.println("Welcome to the Random Number Game!\n");
// Create Scanner Object
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// Get Random Double Number
double randNumber = Math.random();
double d = randNumber * 100;
int randomNum = (int)d + 1;
// Beginning Game Message
System.out.println("I'm thinking of a number between 1 - 100.");
System.out.println("Can you guess it?");
// Obtain User Guesses
for(int i = 1; i <= 7; i++) {
System.out.println("Let''s Play!\n");
int userInt = getIntWithinRange(sc, "Enter your guess: ", 1, 100);
if (userInt >= randomNum + 10)
System.out.println("Way too high!");
else if (userInt > randomNum)
System.out.println("Too high!");
else if (userInt > randomNum)
System.out.println("Too low!");
else
System.out.println("You guessed right!");
} // End For Loop
// See if user wants to play again
choice = "x";
while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
System.out.println("Do ou wish to play again? (y/n): ");
choice = sc.next();
sc.nextLine();
if (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
System.out.println("Error! Not a valid responce!");
} // End if Loop.
} // End While Choice Loop.
} // End While Loop.
} // End Main.
public static int getIntWithinRange(Scanner sc, String prompt, int min, int max) {
int number = 0;
boolean isValid = false;
while (isValid == false) {
number = getInt(sc, prompt);
if (number <= min)
System.out.println("Error! Number must be greater than " + min + ".");
else if (number >= max)
System.out.println("Error! Number must be greater than " + max + ".");
else
isValid = true;
}// End While Loop
return number;
} // End Rage Checker
public static int getInt (Scanner sc, String prompt) {
int number = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextInt()) {
number = sc.nextInt();
isValid = true;
} // End If
else {
System.out.println("Error! Invalid integer value. Try again.");
} // End Else
sc.nextLine();
} // End While Loop
return number;
}// End Integer Checker
} // End Class.
答案 0 :(得分:2)
进行此更改使其适合我。
System.out.println("Let's Play!\n");//Moved outside for loop
// Obtain User Guesses
int userInt=0;//declaration moved outside for loop
for(int i = 1; i <= 7; i++) {
userInt = getIntWithinRange(sc, "Enter your guess: ", 1, 100);
if (userInt >= randomNum + 10)
System.out.println("Way too high!");
else if (userInt > randomNum)
System.out.println("Too high!");
else if (userInt < randomNum)//Changed to < otherwise if userInt<=randomNum, you win.
System.out.println("Too low!");
else{
System.out.println("You guessed right!");
break;//exit loop once user guesses right
}
} // End For Loop
if(userInt!=randomNum)
System.out.println("You lose"); //Print loss.
代码量很好,甚至有用,因为我们可以自己测试程序。 也改变了:
else if (number >= max)
System.out.println("Error! Number must be less than " + max + ".");
答案 1 :(得分:2)
您的计划中有几个小错误:
"Too low!"
猜测消息之前的行中,您使用了错误的比较运算符。getIntWithinRange
方法中,您应该允许min
和max
生效。答案 2 :(得分:0)
如果数字太大,您需要更改验证的措辞。
Enter your guess: 101
Error! Number must be greater than 100.
您需要为验证创建单独的函数,并且您可能希望在输入上使用Integer.parseInt(String s)。由于这是一项任务,我不打算告诉你究竟该怎么做。
Integer.parseInt的文档:http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29