2017-05-27 54 views
1

我有一個任務是讓計算機生成1到100之間的數字,並且用戶試圖猜測它。我有它編碼我只是無法讓我的重播工作。它給我一個關於索引超出範圍的錯誤,我不知道爲什麼。我還需要該程序能夠在遊戲過程中隨時退出並調用方法出口。我不知道該怎麼做。這是我目前的代碼。重播循環,允許腳本在任何時候退出

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(); 
} 
} 

回答

0

可能這會做。

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(); 
} 
} 
+0

Thanjs。看起來不錯 – user2860253

0

就試試這個代碼(我想這應該工作 - 更改已被標記有變化碼:找「改變」爲關鍵字):

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; 
    //Change Code : Change No. 4 : Start 
    boolean replay = true; 
    //Change Code : Change No. 4 : End [Refer change No 5. for WHY?] 

    public void computerguess() 
    { 
     computernumber = generator.nextInt(100)+1; 
    } 
    public void userguess() 
    { 
     if(play == 'y') 
     { 

      /* 
      Your Code 
      usernumber = scan.nextInt(); 
      highorlow(); 
      */ 

      //Change Code : Change No. 1 : Start 
      boolean correctNumber = false; 

      while (!correctNumber) { // loop is used to ask user for a correct input 

       // this statment moved from above into the loop 
       System.out.println("Please guess a number between 1 and 100"); 


       String inputFromUser = scan.next(); 
       try{ 
        usernumber = Integer.parseInt(inputFromUser); 
        correctNumber = true; 
       } catch (NumberFormatException e) { 
        if(inputFromUser.equals("e")) { // Then user want to exit the game 
         exit(); // call the exit method 
        } else { 
         System.out.println(" Please input a correct number Please !!! "); 
        } 
       } 
      } 
      //Change Code : Change No. 1 : End 
     } else { 
      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 

       //Change Code : Change No. 7 : Because we added userguess in the loop 
       //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 

        //Change Code : Change No. 7 : Because we added userguess in the loop 
        //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().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.next().charAt(0); 
     if(playagain =='y') 
     { 
      replay = true; 
      guesses = 0; // If you play again guesses will become zero 
      play(); 
     } 
     //Change Code : Change No. 2 : Start 
     else { 
      replay = false; // Only In case of Y it will continue 
     } 
     //Change Code : Change No. 2 : End 
    } 
    public void exit() 
    { 
     System.out.println("Thanks for playing! The number was "+computernumber+""); 
     //Change Code : Change No. 3 : Start 
     System.exit(3); // Whenever exit will be called user will be moved out of the application 
     //Change Code : Change No. 3 : End 
    } 
    public void play() 
    { 
     //Change Code : Change No. 5 : Start 
     /*instructions(); 
     computerguess(); 
     userguess(); 
     printscore(); 
     replay();*/ 
     //Change Code : Change No. 5 : End [Here we do not require this code if we make replay by default true] 
     if(replay == true) 
     { 
      instructions(); 
      computerguess(); 
      //Change Code : Change No. 6 : Start - User must be given multiple guesses 
      while(true) { 
       userguess(); 
       highorlow(); 
       if(usernumber == computernumber) { 
        break; 
       } 
      } 
      //Change Code : Change No. 6 : End 
      printscore(); 
      replay(); 
     } 
     else 
     { 
      if(replay == false) 
      { 
       System.exit(3); 
      } 
     } 
    } 

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

謝謝。我感謝你展示這些變化。 – user2860253