OverLoading Methods Var无法解析。

时间:2017-03-24 00:01:38

标签: java debugging methods

我得到的错误是"答案无法解决"。向下翻页的1/4。在线查看仍然不知道它应该是什么。使用while循环会更容易吗?完全跳过do循环?

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

}

2 个答案:

答案 0 :(得分:0)

使用nextString()返回下一个字符串。没有return winner;之类的东西。

另外,在getwinner方法的末尾添加public void countChars (){ String currentWord; for(int pass = 0; pass < numberOfTokens; pass++){ currentWord = words[pass]; for (int i = 0; i < currentWord.length(); i++){ char ch = currentWord.charAt(i); if (ch >= 'A' && ch <= 'Z'){ numberOfUpperCase++; } if (ch >= 'a' && ch <= 'z'){ numberOfLowerCase++; } if (ch >= '0' && ch <= '9'){ numberOfDigits++; } } } }//end of countChars

答案 1 :(得分:0)

您在answer的大括号内声明do,然后尝试在大括号外使用它。离开大括号后,变量超出了范围。