我写了一个简单的骰子游戏,想知道在输入“重置”之类的内容后重置程序的方法。我使用 cmd 来运行我的程序。我的任何程序中都不包含图形。
import java.util.Scanner;
import java.util.Random;
public class Dice
{
public static void main(String[] args)
{
String personPlay; //User's play
String computerPlay = ""; //Computer's play
int computerInt; //Randomly generated number used to determine computer's play
String response;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Let's play some dice!");
//Generate computer's play
computerInt = generator.nextInt(6)+1;
//Translate computer's randomly generated play to
//string using if statements
if (computerInt == 1)
computerPlay = "1";
else if (computerInt == 2)
computerPlay = "2";
else if (computerInt == 3)
computerPlay = "3";
else if (computerInt == 4)
computerPlay = "4";
else if (computerInt == 5)
computerPlay = "5";
else if (computerInt == 6)
computerPlay = "6";
//Get player's play from input
System.out.println("Choose a number between 1 and 6.");
personPlay = scan.next();
//Print computer's play
System.out.println("The dice rolled " + computerPlay);
//See if you won.
if (personPlay.equals(computerPlay))
System.out.println("You won!");
else System.out.println("You lost!");
}
}
最佳答案
您应该使用无限循环并获取用户的输入,检查用户是否输入“RESET”,如果是,则再次掷骰子或执行您现在正在做的任何操作。 如果他输入像“EXIT”这样的短语,则无限循环结束或您的程序结束。
关于java - 在cmd上重置程序的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292562/