同时循环辅助

时间:2015-11-05 04:43:12

标签: java loops while-loop

所以我有这个程序可以让你自己玩石头剪刀,我怎么去包括一个while循环来运行程序多次,直到用户选择退出。这是我到目前为止所拥有的:

   public static void main(String[] args) {
   //Instantiate random and scanner function

    Random rNum = new Random ();
    Scanner scan = new Scanner (System.in);

   //instantiate integers for the computer and user number 
    int computerNumber;
    int userNumber;

    //sets parameter from which the computer can draw a number from
    computerNumber = rNum.nextInt(3) +1  ;

    //Prints the purpose of this program to user
    System.out.println("This program lets you play rock paper scissor against"
           + "a CPU, good luck!");

    //Prints the options that the user can input
    System.out.println("Choose your weapon, 1, rock, 2, paper, 3 scissor");

    //captures and holds user answer
    userNumber = scan.nextInt();

    //first set of outcomes, a draw
    if(userNumber == computerNumber) 

    {
        System.out.println("You have drawn, go again."); //Prints outcome of a draw
    }

    //Program uses else if to determine what outcome to print depending on user number
    else if(userNumber == 1 && computerNumber == 2 || userNumber == 2 &&
      computerNumber== 3|| userNumber == 3 && computerNumber == 1)  

        //Prints outcome of a loss
       System.out.println("You fell in combat to " + computerNumber + " ,try again.");

      //Prints outcome of a win
    else System.out.println("Congratulations, you are victorious! You "
            + "defeated " + computerNumber) ;



    }      

}

2 个答案:

答案 0 :(得分:1)

你首先需要选择一个好地方开始你的while循环,我会在这里开始:

public static void main(String[] args) {
    //Instantiate random and scanner function
    Random rNum = new Random ();
    Scanner scan = new Scanner (System.in);

    //instantiate integers for the computer and user number 
    int computerNumber, userNumber;

    do {
        //Code for playing the game
    } while (condition);
} //end of main

然后你需要询问用户是否想再次播放,并将结果作为do while循环的条件:

do {
    //Code for playing the game
    System.out.print("Do you want to play again? <y/n> :");
} while (scan.next() != "n");

答案 1 :(得分:0)

试试此代码

public static void main(String [] args){            //实例化随机和扫描仪功能

        Random rNum = new Random ();
        Scanner scan = new Scanner (System.in);

       //instantiate integers for the computer and user number 
        int computerNumber;
        int userNumber;
        String quit="n";
        //sets parameter from which the computer can draw a number from


        //Prints the purpose of this program to user
        System.out.println("This program lets you play rock paper scissor against"
               + "a CPU, good luck!");
        while(quit.equalsIgnoreCase("n")){
        //Prints the options that the user can input
        System.out.println("Choose your weapon, 1, rock, 2, paper, 3 scissor");
        computerNumber = rNum.nextInt(3) +1  ;
        //captures and holds user answer
        userNumber = scan.nextInt();

        //first set of outcomes, a draw
        if(userNumber == computerNumber) 

        {
            System.out.println("You have drawn, go again."); //Prints outcome of a draw
        }

        //Program uses else if to determine what outcome to print depending on user number
        else if(userNumber == 1 && computerNumber == 2 || userNumber == 2 &&
          computerNumber== 3|| userNumber == 3 && computerNumber == 1)  
        {
            //Prints outcome of a loss
           System.out.println("You fell in combat to " + computerNumber + " ,try again.");
        }
          //Prints outcome of a win
        else{ 
            System.out.println("Congratulations, you are victorious! You "
                + "defeated " + computerNumber) ;
        }
        System.out.println("Do you want you quit the game(y/n)?");
        scan.nextLine();
        quit=scan.nextLine();
        while(!(quit.equalsIgnoreCase("y")||quit.equalsIgnoreCase("n"))){
            System.out.println("enter proper input (y/n)");
            quit=scan.nextLine();
        }
        }

    }