拿起棍子游戏

时间:2017-03-11 19:19:09

标签: java if-statement while-loop

您已经让游戏拿起棍子,除了2个部分外,它可以正常工作。我希望程序显示无效输入的消息,例如"只能输入数字1或2"。此外,我试图让它在最后你选择是否继续,但它似乎没有工作。可以有人帮助我这些代码部分?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Sticks {

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;
 int input1;
 int B = 1;



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


            }
       int Y=5, N=10;
       System.out.println("Your score is "+ score);
        System.out.println("Do you want to play again? Press(5) For Yes/(10) For No");
       input1 = Integer.parseInt(br.readLine());
       if(input1==Y){
         B=1;   
         System.out.println("There are 21 sticks How many sticks do you want to take (1 or 2)");
       }
       else if(input1==N)
       {System.exit(0);
        System.out.println("Have A Good Day!");
           }
  }
 }

}  }

0 个答案:

没有答案