Java摇滚纸剪刀打印声明奇怪

时间:2017-03-24 02:10:40

标签: java debugging logic

所以在这里我试图创建一个程序,将输入作为int,然后玩一个Rock纸剪刀游戏。它似乎想要重新印刷它不应该出现的声明,也正在跳过打印声明。如果可能的话,我会喜欢一些帮助。我尝试过在任何地方设置打印语句,但它更令人困惑。

    import java.util.Scanner;
        public class RPSS{
        //Main method
          public static void main(String[ ] argc) 
          {
            System.out.println("Lets play rock paper scissors");

            Scanner tnt = new Scanner(System.in); 
            String computerHand; // string variable for computer choice
            String userHand; // string variable for user choice
        //    
            String answer = "";
            while (!a

nswer.equals("No") && (!answer.equals("no"))){

          userHand = userHand();
          computerHand = computerHand();
          System.out.println("The User picks " + userHand + " " );
          System.out.print("The Computer picks " + computerHand );

          String winner = getWinner(computerHand, userHand);
          System.out.println(winner);
          System.out.println("play again?");
          answer = tnt.next();
        }

        //Condition for the do-while loop


      }

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

        //prints message to user giving them choices
        System.out.println("     ");
        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 userChoice
      }

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

        int computernum =  1 + (int)(Math.random() * (( 2) +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="";


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

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

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

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

          return winner;
      } 
      }
    }

1 个答案:

答案 0 :(得分:0)

  • 第一个问题是userhand()和computerHand()每个“round”被调用两次,一次在main方法内部的while循环开始,一次在getWinner()方法的开头。消除getWinner()方法开头的调用应解决重复问题。

  • 第二个问题是,在返回之前,不是在getWinner()方法中修改wins的值,而是通过println()输出消息。解决这个问题的一个例子就是转换它:

    if(userChoice.equals(“Rock”)&& computerChoice.equals(“Paper”){     System.out.println(“计算机获胜”);     回报获胜者; }

    到此:

    if(userChoice.equals(“Rock”)&& computerChoice.equals(“Paper”)){     获胜者=“电脑赢了”;     回报获胜者; }

  • 另一个小问题是

    userChoice.equals(“Rock”)&& computerChoice.equals( “纸”)

    被检查两次,id只是删除了基于的整个if else块 第二次检查

  • 最后,我会将最后的其他条款视为玩家赢得一个并将获胜者设置为“玩家获胜”