Java简单猜谜游戏
两个玩家的示例程序。玩家1输入密码(范围0-100)。玩家2猜猜那个数字。如果猜测值较高或较低,则输出一条消息。如果玩家2猜对了,则输出一条祝贺消息和猜测次数。这是我到目前为止得到的Java简单猜谜游戏,java,debugging,while-loop,Java,Debugging,While Loop,两个玩家的示例程序。玩家1输入密码(范围0-100)。玩家2猜猜那个数字。如果猜测值较高或较低,则输出一条消息。如果玩家2猜对了,则输出一条祝贺消息和猜测次数。这是我到目前为止得到的 import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { Scanner input = new Scanner(System.in);
import java.util.Scanner;
public class GuessingGame
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number;
System.out.println("Player 1 enter secret number (range 0 - 100): ");
number = input.nextInt();
int guess = 0;
int tries = 0;
while(guess != number)
{
tries = tries + 1;
System.out.print("Player 2 enter your guess (range 0 - 100): ");
guess = input.nextInt();
if (guess > number)
{
System.out.printf("Your guess is higher than the correct number\n");
}
else if (guess < number)
{
System.out.printf("Your guess is lower than the correct number\n");
}
else if (guess == number)
{
System.out.printf("Congratulations! You guess the correct number\n");
System.out.printf("Number of guesses: %d\n", tries);
break;
}
}
}
import java.util.Scanner;
公开课猜谜游戏
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
整数;
System.out.println(“玩家1输入密码(范围0-100):”;
number=input.nextInt();
int guess=0;
int=0;
while(猜测!=数字)
{
尝试=尝试+1;
系统输出打印(“玩家2输入你的猜测(范围0-100):”;
guess=input.nextInt();
如果(猜测>数字)
{
System.out.printf(“您的猜测高于正确的数字\n”);
}
else if(猜测<数字)
{
System.out.printf(“您的猜测低于正确的数字\n”);
}
else if(猜测==数字)
{
System.out.printf(“祝贺您!您猜对了数字\n”);
System.out.printf(“猜测次数:%d\n”,尝试次数);
打破
}
}
}
}
我的问题是:如何隐藏玩家1输入的秘密号码,使玩家2看不见?
如何将密码的范围限制在0到100之间。在输入player#1之后,我使用了下面的代码。但之后,它仍然会显示player#2的输入
if (number < 0)
System.out.print("Please enter number greater than 0");
if (number > 100)
System.out.print("Please enter number smaller than 100");
if(数字<0)
系统输出打印(“请输入大于0的数字”);
如果(数量>100)
系统输出打印(“请输入小于100的数字”);
任何人请帮帮我!我使用的是NetBeans。您可以使用作业窗格(对话框)让玩家1输入密码
For example:
String input=JOptionPane.showInputDialog(null,"Player 1 enter secret number (range 0 - 100): ");
int number=Integer.parseInt(input);
有关对话框的更多信息,请参见此处。您可以在第一个播放机使用以下方法选择值后立即清除控制台:
public final static void clearConsole()
{
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (final Exception e)
{
// Handle any exceptions.
}
}
现在,您可以将代码更改为:
import java.util.Scanner;
public class GuessingGame
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number;
do {
System.out.println("Player 1 enter secret number (range 0 - 100): ");
number = input.nextInt();
}while (number < 0 || number > 100);
clearConsole();
int guess = 0;
int tries = 0;
while(guess != number)
{
tries = tries + 1;
System.out.print("Player 2 enter your guess (range 0 - 100): ");
guess = input.nextInt();
if (guess > number)
{
System.out.printf("Your guess is higher than the correct number\n");
}
else if (guess < number)
{
System.out.printf("Your guess is lower than the correct number\n");
}
else if (guess == number)
{
System.out.printf("Congratulations! You guess the correct number\n");
System.out.printf("Number of guesses: %d\n", tries);
break;
}
}
}
import java.util.Scanner;
公开课猜谜游戏
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
整数;
做{
System.out.println(“玩家1输入密码(范围0-100):”;
number=input.nextInt();
}而(数量<0 | |数量>100);
clearConsole();
int guess=0;
int=0;
while(猜测!=数字)
{
尝试=尝试+1;
系统输出打印(“玩家2输入你的猜测(范围0-100):”;
guess=input.nextInt();
如果(猜测>数字)
{
System.out.printf(“您的猜测高于正确的数字\n”);
}
else if(猜测<数字)
{
System.out.printf(“您的猜测低于正确的数字\n”);
}
else if(猜测==数字)
{
System.out.printf(“祝贺您!您猜对了数字\n”);
System.out.printf(“猜测次数:%d\n”,尝试次数);
打破
}
}
}
编辑以包括数字范围检查。使用随机数创建var?替换
系统。在没有自己的系统的流中,
不会将字符回显到屏幕上…有一段时间了,可能无法工作。。。