gpt4 book ai didi

java - 重播循环允许脚本随时退出

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:12 28 4
gpt4 key购买 nike

我有一个任务是制作一个游戏,计算机生成 1 到 100 之间的数字,并且用户尝试猜测它。我已经对其进行了编码,但我无法让我的重播发挥作用。它给了我一个关于索引超出范围的错误,我不知道为什么。我还需要程序能够在游戏过程中随时退出并调用方法exit。我不知道该怎么做。这是我当前的代码。

import java.util.Scanner;
import java.util.Random;
/*
Program to let user guess random generated number between 1 and 100
*/
public class Hilo
{
Scanner scan = new Scanner(System.in);
Random generator = new Random(); // Random generator to generate number
int computernumber;
int usernumber;
int guesses = 0;
char play;
char playagain;
boolean replay;

public void computerguess()
{
computernumber = generator.nextInt(100)+1;
}
public void userguess()
{
if(play == 'y')
{
System.out.println("Please guess a number between 1 and 100");
usernumber = scan.nextInt();
highorlow();
}
}
public void highorlow()
{
if(usernumber == computernumber)
{
System.out.println("Good Job! Your guessed the correct number!"); // Checks if you guessed the correct number
guesses = guesses +1; // Adds 1 to guess count
}
else
{
if(usernumber > computernumber) // Checks if your number is higher than the computers
{
System.out.println("Your guessed to high please try again");
guesses =guesses +1; // Adds 1 to guess count
userguess(); // Lets user guess again
}
else
{
if(usernumber < computernumber) // Checks if your number is less than the computers number
{
System.out.println("Your guess was to low please try again");
guesses = guesses +1;// Adds 1 to guess count
userguess(); // Lets user guess again
}
}
}
}
public void instructions() // Gives instructions on how to play
{
System.out.println("To play simply guess a number between 1 and 100. \nThe computer will tell you if you are correct or inncorrect. \n Press y to continue \n Press e at any time to exit");
play = scan.nextLine().charAt(0);
}
public void printscore()
{
System.out.println("Your took "+guesses+" guesses to get the correct number \n"); // Tells user how many guesses they took
}
public void replay() // lets user replay game
{
System.out.println("Would you like to play again? Y for yes N for no");
playagain = scan.nextLine().charAt(0);;
if(playagain =='y')
{
replay = true;
play();
}
}
public void exit()
{
System.out.println("Thanks for playing! The number was "+computernumber+"");
}
public void play()
{
instructions();
computerguess();
userguess();
printscore();
replay();
if(replay == true)
{
instructions();
computerguess();
userguess();
printscore();
replay();
}
else
{
if(replay == false)
{
System.exit(3);
}
}
}

public static void main(String[] args)
{
Hilo guess = new Hilo();
guess.play();
}
}

最佳答案

也许这样就可以了。

import java.util.Scanner;
import java.util.Random;
/*
Program to let user guess random generated number between 1 and 100
*/
public class Hilo
{
Scanner scan = new Scanner(System.in);
Random generator = new Random(); // Random generator to generate number
int computernumber;
int usernumber;
int guesses = 0;
String play;
boolean replay;

public void computerguess()
{
computernumber = generator.nextInt(100)+1;
}
public void userguess()
{
if("y".equalsIgnoreCase(play))
{
System.out.println("Please guess a number between 1 and 100");
String input = scan.next();
if("e".equalsIgnoreCase(input)) exit();
else usernumber = Integer.parseInt(input);
highorlow();
}else if("e".equalsIgnoreCase(play)) exit();
}
public void highorlow()
{
if(usernumber == computernumber)
{
System.out.println("Good Job! Your guessed the correct number!"); // Checks if you guessed the correct number
guesses = guesses +1; // Adds 1 to guess count
}
else
{
if(usernumber > computernumber) // Checks if your number is higher than the computers
{
System.out.println("Your guessed to high please try again");
guesses =guesses +1; // Adds 1 to guess count
userguess(); // Lets user guess again
}
else
{
if(usernumber < computernumber) // Checks if your number is less than the computers number
{
System.out.println("Your guess was to low please try again");
guesses = guesses +1;// Adds 1 to guess count
userguess(); // Lets user guess again
}
}
}
}
public void instructions() // Gives instructions on how to play
{
System.out.println("To play simply guess a number between 1 and 100. \nThe computer will tell you if you are correct or inncorrect. \n Press y to continue \n Press e at any time to exit");
play = scan.next();
}
public void printscore()
{
System.out.println("Your took "+guesses+" guesses to get the correct number \n"); // Tells user how many guesses they took
}
public void replay() // lets user replay game
{
System.out.println("Would you like to play again? Y for yes N for no");
String input = scan.next();
if("y".equalsIgnoreCase(input))
{
replay = true;
play();
}
}
public void exit()
{
System.out.println("Thanks for playing! The number was "+computernumber+"");
System.exit(3);
}
public void play()
{
if(replay == false)
{
exit();
}

instructions();
computerguess();
userguess();
printscore();
replay();
}

public static void main(String[] args)
{
Hilo guess = new Hilo();
guess.replay = true;
guess.play();
}
}

关于java - 重播循环允许脚本随时退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44218022/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com