我试图制作一个猜谜游戏,我提示用户输入一个密码,他有6次试图让它正确。我把它的大部分都弄了下来,但是一旦他用完了他的尝试我就不能告诉他了。对不起,你已经用完了,秘密号码是x& #34)
我的代码:
import java.util.*;
import java.util.Scanner;
public class GuessGame{
static public void main(String[] args)
{
System.out.println("Welcome to the guessing game. I will pick a number between 1");
System.out.println("and 100 and you will try to guess it in 6 tries or less. Here we go: ");
for (int i = 6; i > 0; i--)
{
Random rand = new Random();
int secNumber = rand.nextInt(5);
int theNumber = secNumber;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
int guess = s.nextInt();
if (guess == theNumber)
{
System.out.println("That's it! You guessed it, the number was: " + theNumber + " and it took you " + (7 - i) + " tries.");
}
while(i > 0) {
if (guess < theNumber)
{
System.out.println("The secret number is bigger than than that. You have " + (i - 1) + " tries left.");
break;
}
else if (guess > theNumber)
{
System.out.println("The secret number is smaller than than that. You have " + (i - 1) + " tries left.");
break;
}
}
告诉用户当他剩下零尝试然后揭示秘密号码时,我试了一下 而(i == 0) { 的System.out.println(....); } 但它在第一次猜测后立即开始。
答案 0 :(得分:0)
while
循环是不必要的,并且样式已关闭。这是我的修复。
import java.util.*;
import java.util.Scanner;
public class GuessGame {
public static void main(String[] args) {
System.out.println("Welcome to the guessing game. I will pick a number between 1");
System.out.println("and 100 and you will try to guess it in 6 tries or less. Here we go: ");
int theNumber = rand.nextInt(5);
for (int i = 6; i > 0; i--) {
Random rand = new Random();
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
int guess = s.nextInt();
if (guess == theNumber) {
System.out.println("That's it! You guessed it, the number was: " + theNumber + " and it took you " + (7 - i) + " tries.");
} else if (guess < theNumber) {
System.out.println("The secret number is bigger than than that. You have " + (i - 1) + " tries left.");
} else if (i == 1) {
System.out.println("You could not guess the number");
} else {
System.out.println("The secret number is smaller than than that. You have " + (i - 1) + " tries left.");
}
}
}
}
答案 1 :(得分:0)
secNumber
。theNumber
是不必要的。rand.nextInt(5)
替换为rand.nextInt(100) + 1
才能获得1到100之间的数字。i - 1
更改为i
,因为猜测次数将等于i
。break
,因为这些会让您离开游戏。break
添加到第一个if语句(如果他们赢了)添加if语句让玩家知道游戏已结束。
import java.util.*;
import java.util.Scanner;
public class GuessGame
{
public static void main(String[] args)
{
System.out.println("Welcome to the guessing game. I will pick a number between 1");
System.out.println("and 100 and you will try to guess it in 6 tries or less. Here we go: ");
Random rand = new Random();
int secNumber = rand.nextInt(100) + 1; // 2 4
for (int i = 6; i > 0; i--)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter a number: ");
int guess = s.nextInt();
if (guess == secNumber)
{
System.out.println("That's it! You guessed it, the number was: " + secNumber + " and it took you " + (7 - i) + " tries.");
break; // 7
}
else if (guess < theNumber)
{
System.out.println("The secret number is bigger than than that. You have " + i + " tries left."); // 5
}
else if (guess > theNumber)
{
System.out.println("The secret number is smaller than than that. You have " + i + " tries left."); // 5
}
if(i == 1) // 8
{
System.out.println("Out of tries.");
}
}
}
}