2017-03-24 87 views
-1

我得到的錯誤是「答案無法解決」。在頁面下方的1/4處。在網上看了看,仍然沒有看到它應該是什麼。使用while循環會更容易嗎?完全跳過循環?OverLoading方法Var無法解析。

import java.util.Scanner; 
public class RPSS{ 
//Main method 
    public static void main(String[ ] argc) 
    { 


    Scanner tnt = new Scanner(System.in); 
    String computerHand; // string variable for computer choice 
    String userHand; // string variable for user choice 
// do loop begining 
    do 
    { 
     computerHand = computerHand(); 
     userHand = userHand(); 
     String winner = getWinner(computerHand, userHand); 
     System.out.println(winner); 
     System.out.print("User picks" + userHand); 
     System.out.println("Computer picks " + computerHand); 
     System.out.println("play again?"); 
     String answer = tnt.next(); 


    //Condition for the do-while loop HERE IS THE ERROR LOCATION 
    }while (!answer.Equals("No") && (!answer.Equals("no"))); //condition for while loop 
    String answer = tnt.next(); 
    } 

    public static String userHand(){ //method for users choice in the game 

    //prints message to user giving them choices 
    System.out.println("Lets play rock paper scissors"); 
    System.out.println("1. Rock "); 
    System.out.println("2. Paper "); 
    System.out.println("3. Scissors "); 
    int userChoice; // user choice variable in this method 
    Scanner tnt = new Scanner(System.in); // creates instance of scanner class 
    userChoice = tnt.nextInt(); //reads user input 
    return getChoice(userChoice); //returns user choice to master choice 
    } 

    public static String computerHand() //method for computer generated choice 
    { 

    int computernum = (int)(Math.random() * ((3) + 1)); 
    return getChoice(computernum); 
    } 

    public static String getChoice(int num) //method recieving both computer hand and user hand 
    { 
// if statements to place the correct choice 
    String choice = ""; 
    if (num == 1){ 
     choice = "rock"; 
    } 
    else if(num == 2){ 
     choice = "paper"; 
    } 
    else if(num == 3){ 
     choice = "scissors"; 
    } 
    return choice; 
    } 
    // Method determing the winner 
    public static String getWinner(String computerChoice, String userChoice) 
    { 
    computerChoice = computerHand(); //places computerChoice variable in computerhand 
    userChoice = userHand(); //does same for user choice 
    String winner=""; 


    System.out.println(" the comp chose" + computerChoice); 

    if (userChoice.equals("Rock") && computerChoice.equals("Paper")){ 
     System.out.println("The computer"); } 

    else if (userChoice.equals("Paper") && computerChoice.equals("Scissors")){ 
     System.out.println(" The computer wins"); 
    } 

    else if (userChoice.equals("Scissors") && computerChoice.equals("Rock")){ 
     System.out.println(" The computer wins "); 
    } 
    else if (userChoice.equals("Rock") && computerChoice.equals("Paper")){ 
     System.out.println(" The computer wins "); 

    } 

    if (userChoice.equals(computerChoice)) 
    { 
     System.out.println(" There is no winner"); 
     } 
    return winner; 
    } 

} 
+1

*「我很想解釋爲什麼我得到一個nextString()是未定義的掃描儀」* - 因爲它是。看看[Scanner的JavaDocs](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html),看看你能否'nextString' – MadProgrammer

+0

問題'getWinner'只返回值'if(userChoice.equals(computerChoice))',沒有其他條件返回任何類型的值 – MadProgrammer

+0

你真的希望我們計算行數嗎? – shmosel

回答

0

使用tnt.next()它返回下一個字符串。沒有這樣的東西,如nextString()

另外,在getwinner方法的末尾添加return winner;

0

您在do的大括號內標明answer,然後嘗試在大括號外使用它。一旦你離開大括號,變量就超出了範圍。

+0

這是有條件的。爲什麼會影響內部? – Lee

+0

翻轉它並使用一段時間(有條件的){在這裏然後將}放在最後給我同樣的問題。 – Lee

+0

這告訴我,因爲你沒有顯示代碼,所以你仍然在引用一個超出其範圍的變量。你在第一次使用之前是否聲明瞭變量?你是否在包含它的使用範圍內聲明它?你還在討論兩個不同的「答案」變量嗎?除非您展示我們的代碼,否則您希望任何人提供幫助? –