编辑
谢谢你们的帮助。这是帮助我的答案:https ://stackoverflow.com/a/14903642
这是完整的工作代码: http: //pastebin.com/1kbipuAZ
结束编辑
我应该制作一个简单的 cmd 游戏,要求用户输入一定范围内的数字。然后玩家 2 应该尝试猜测这个数字是多少。程序不断告诉“高”或“低”,直到玩家猜出数字。我想我应该使用一些 if/else 语句,也许还有 while 循环,但我不知道以什么方式。我要么得到一个无限循环,要么它在我想要它之前停止。这是我所拥有的:
import java.io.*;
class game
{
public static void main (String[]args) throws IOException
{
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(inStream);
String inData;
System.out.println("Welcome to The Game. Player One, please enter an integer higher than 0, and less than 1000...\n");
inData = stdin.readLine();
int number = Integer.parseInt(inData);
if(number >= 1000 || number <= 0)
{
System.out.println("Sorry, that number is out of the acceptable range on numbers...\n");
}
else
{
System.out.println();
}
String f1;
System.out.println("Player Two, Please enter a guess as to what Player One's number is...\n");
f1 = stdin.readLine();
int guess = Integer.parseInt(f1);
while(guess < number)
{
if(guess != number)
{
System.out.println("Higher. Please Guess Again...\n");
}
else
{
System.out.println();
}
break;
}
while(guess > number)
{
if(guess != number)
{
System.out.println("Lower. Please Guess Again...\n");
}
else
{
System.out.println();
}
break;
}
if(number==guess)
{
System.out.println("Congratulations. Thank you for playing...\n");
}
else
{
System.out.println();
}
}
}
我尝试了循环和语句等的不同组合,但我不能走得太远。