根据用户输入重新启动程序

时间:2017-03-11 22:49:30

标签: java loops if-statement while-loop

我想根据用户输入重启我的游戏。

例如,如果我打印" 您是否想再次播放,请键入(5)表示“是”或(10)表示“否”"。我尝试添加do while循环,但我不知道该怎么做。任何帮助重启程序的代码都将非常感谢!

import java.util.Scanner;
public class Sticks3 {

     public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(
               new InputStreamReader(System.in));

     int numSticks = 21;
     System.out.println("Would You Like to go first? (Yes/No)");
     Scanner input = new Scanner(System.in);
     String goFirst = input.nextLine();
     Scanner take = new Scanner (System.in);
     int numToTake = 0;
     int score = 0;





     while (numSticks > 0) {

       if (goFirst.equals("Yes") || goFirst.equals("yes")) {
         System.out.println("There are " + numSticks + " sticks ");
         System.out.println("How many sticks do you want to take (1 or 2)");
         numToTake = take.nextInt();

         if (numToTake > 2) {
           numToTake = 2;

         }

         else if (numToTake < 1) {
           numToTake = 1;
         }
         numSticks = numSticks - numToTake;

         if (numSticks <= 0) {
           System.out.println("You lose");
           System.out.println("Your score is " + score );
         }
         else {

           if((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
             numToTake = 1;
           }
           else {
             numToTake = 2;
           }
           System.out.println("Computer takes " + numToTake + " sticks " );
           numSticks = numSticks - numToTake;

           if (numSticks <= 0) {
             System.out.println(" You win ");
             score++;
             System.out.println("Your score is " + score );
           }
         }

      }
      else {
             if((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
             numToTake = 1;
           }
           else {
             numToTake = 2;
           }
           System.out.println("Computer takes" + numToTake + " sticks " );
           numSticks = numSticks - numToTake;

           if (numSticks <= 0) {
             System.out.println("You win");
             score++;
             System.out.println("Your score is " + score );
           }
           else {
               System.out.println("There are " + numSticks + " sticks ");
               System.out.println("How many sticks do you want to take (1 or 2)");
               numToTake = take.nextInt();

               if (numToTake > 2) {
                   numToTake = 2;
               }
               else if (numToTake < 1){
                   numToTake = 1;
               }
               numSticks = numSticks - numToTake;

               if(numSticks <= 0){
                   System.out.println("You win");
                   score++;
                   System.out.println("Your score is " + score );
               }

           }

      }
     }
   }
 }

2 个答案:

答案 0 :(得分:2)

最干净的方法是将所有代码放在main函数中。然后在循环中调用该函数。像这样:

Scanner scanner = new Scanner(System.in);

    while (true) {
        runGame(scanner);
        // Game has finished

        System.out.println("Do you want to play again, type (5) for yes or (10) for no");

        if (scanner.nextLine().equals("10")) {
            break;
        }
    }

扫描仪传递给该功能,因为那里可以使用相同的扫描仪。您的计划and it should be avoided没有理由拥有多台扫描仪。

答案 1 :(得分:0)

围绕它构建一段时间(或者做,甚至是更好的选择)。询问是否应该再次播放,如果是,则while的条件保持为真,否则更改它。当然你必须初始化numSticks,numToTake并再次得分为0。

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Sticks3 {

    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int numSticks = 21;
        System.out.println("Would You Like to go first? (Yes/No)");
        Scanner input = new Scanner(System.in);
        String goFirst = input.nextLine();
        Scanner take = new Scanner(System.in);
        int numToTake = 0;
        int score = 0;

        boolean playAgain = true;
        Scanner askPlayAgainScanner = new Scanner (System.in);

        while (playAgain == true) {

            numSticks = 21;
            numToTake = 0;
            score = 0;

            while (numSticks > 0) {

                if (goFirst.equals("Yes") || goFirst.equals("yes")) {
                    System.out.println("There are " + numSticks + " sticks ");
                    System.out.println("How many sticks do you want to take (1 or 2)");
                    numToTake = take.nextInt();

                    if (numToTake > 2) {
                        numToTake = 2;

                    }

                    else if (numToTake < 1) {
                        numToTake = 1;
                    }
                    numSticks = numSticks - numToTake;

                    if (numSticks <= 0) {
                        System.out.println("You lose");
                        System.out.println("Your score is " + score);
                    } else {

                        if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
                            numToTake = 1;
                        } else {
                            numToTake = 2;
                        }
                        System.out.println("Computer takes " + numToTake + " sticks ");
                        numSticks = numSticks - numToTake;

                        if (numSticks <= 0) {
                            System.out.println(" You win ");
                            score++;
                            System.out.println("Your score is " + score);
                        }
                    }

                } else {
                    if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
                        numToTake = 1;
                    } else {
                        numToTake = 2;
                    }
                    System.out.println("Computer takes" + numToTake + " sticks ");
                    numSticks = numSticks - numToTake;

                    if (numSticks <= 0) {
                        System.out.println("You win");
                        score++;
                        System.out.println("Your score is " + score);
                    } else {
                        System.out.println("There are " + numSticks + " sticks ");
                        System.out.println("How many sticks do you want to take (1 or 2)");
                        numToTake = take.nextInt();

                        if (numToTake > 2) {
                            numToTake = 2;
                        } else if (numToTake < 1) {
                            numToTake = 1;
                        }
                        numSticks = numSticks - numToTake;

                        if (numSticks <= 0) {
                            System.out.println("You win");
                            score++;
                            System.out.println("Your score is " + score);

                        }



                    }

                }
            }
            System.out.print("Do you want to play again? true for yes, false for no: ");
            boolean askPlayAgain = askPlayAgainScanner.nextBoolean();
        }
    }
}