我实际上正在制作累积奖金游戏,它会生成随机数并要求用户猜测随机数,如果它符合打印的条件胜利,如果不符合则转移到其他条件语句。
我差不多完成了我的代码编写,只是我对这一步感到困惑。我的疑问是:
我在randInt(max)
上设置了一个限制a,即随机数,但我想知道如果它超出了限制,它必须显示一条我无法自拔的消息。
如果有人可以,请分享您的想法。
public class Jackpot {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int mode;
System.out.println("Choose the difficulty level mode");
System.out.println("1: Easy (0-15)");
System.out.println("2: Medium (0-30)");
System.out.println("3: Difficult (0-50)");
System.out.println("or type another number to quit");
mode = input.nextInt();
if(mode == 1){
Start_Game(randInt(15));
}else if(mode == 2){
Start_Game(randInt(30));
}else if(mode == 3){
Start_Game(randInt(50));
}else if(mode <= 4 || mode >= 0){
System.out.println("Quit");
Restart_Game();
}
}
public static int randInt(int max){
int randomNum = (int) (Math.random() * ((max) + 1));
return randomNum;
}
public static int Start_Game(int max){
System.out.println("Please enter your guessed number");
int GuessNum , life = 5;;
do{
GuessNum = input.nextInt();
if(GuessNum == max){
System.out.println("You WIN");
break;
}else if(GuessNum > max){
System.out.println("BIG");
life--;
System.out.println("life: " + life);
}else if(GuessNum < max){
System.out.println("SMALL");
life--;
System.out.println("life: " + life);
}
}while(life != 0);
Restart_Game();
return 0;
}
public static void Restart_Game(){
System.out.println("if you want to restart the game \nPress Y to continue or N to exit");
char restart = input.next().charAt(0);
if(restart == 'Y' || restart == 'y'){
//Start_Game(randInt(10));
int mode;
System.out.println("Choose the difficulty level mode");
System.out.println("1: Easy (0-15)");
System.out.println("2: Medium (0-30)");
System.out.println("3: Difficult (0-50)");
System.out.println("or type another number to quit");
mode = input.nextInt();
if(mode == 1){
Start_Game(randInt(15));
}else if(mode == 2){
Start_Game(randInt(30));
}else if(mode == 3){
Start_Game(randInt(50));
}else if(mode <= 4 || mode >= 0){
System.out.println("Quit");
Restart_Game();
}
} else if(restart == 'n'|| restart == 'N'){
System.out.println("Thank you for playing");
System.exit(0);
}
}
private static int randIt(int max) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
答案 0 :(得分:0)
你的“游戏”没有实际范围的线索,即使你有一个参数调用“max”,它只是找到的值,你可以重命名它或你真的可以发送接受的最大值:
Start_Game(15);
让游戏基于此生成随机值。您已找到max
值和random value
。现在很容易检查你的情况。
public static int Start_Game(int max){
int toFind = randInt(max);
do{
GuessNum = input.nextInt();
if(GuessNum > max){
System.out.println("Out of range");
} else if(GuessNum == toFind ){
System.out.println("You WIN");
break;
}else if(GuessNum > toFind ){
System.out.println("BIG");
life--;
System.out.println("life: " + life);
}else if(GuessNum < toFind ){
System.out.println("SMALL");
life--;
System.out.println("life: " + life);
}
}while(life != 0);
}
注意:你应该重命名你的方法和变量,JAVA有一个约定(就像其他语言一样),不要命名一个以大写字母开头的变量。
public static int startGame(int max){
或
int guessNum;