当答案错误时,如何使int计数器失效

时间:2015-02-16 06:25:41

标签: java int counter

我让玩家2猜测玩家1的数字游戏。我做了一个int = = 10的int计数器,并且每次玩家2得到错误答案时都会失败。我不能让它工作,我需要帮助如何做到这一点。你会明白我的意思......

package guessMain;

import java.awt.*;

import java.util.Scanner;

public class GuessCodeSource {
    public static void main(String[] args){
        System.out.println("WELCOME TO GUESSING GAME BY JOSH!");
        System.out.println("Rules: Player 1 picks number between 1 - 100 while Player 2 has 10 tries to guess");
        Scanner josh = new Scanner(System.in);
        System.out.println("Enter name here PLAYER 1: ");
         String p1 = josh.nextLine();
         System.out.println("Enter name here PLAYER 2: ");
         String p2 = josh.nextLine();
         System.out.println("Ok, " +  p2 + " look away. " +  p1 + ", Please enter a number and press enter:");
            int answer = josh.nextInt();
            if (answer >= 100){
                System.out.println("BUSTED! I said a number between 1 - 100!");

            }else if (answer <= 100){       
                System.out.println("Guess in the space below.");
                int guess = josh.nextInt();
                if (guess == answer){
                System.out.println("CORRECT!!!!!");

                }else if (guess != answer);
                     for (int counter = 10; counter-=1);
                    System.out.println("You have " + count + " of guesses left");


            }
    }
}

2 个答案:

答案 0 :(得分:1)

要使数字减1,请使用减量运算符。

例如,

counter--;

会从柜台中扣除一个。

如果要减去多个,可以按以下方式使用“ - =”运算符:

counter -= 2;

因此,在您的代码中,在最后的if if块中,您可以将代码更改为以下内容以将“counter”减少1。

else if (guess != answer) {
    counter--;
    System.out.println("You have " + count + " of guesses left");
}

但是,在您的代码中,您永远不会声明变量计数器。在某处,您可能希望创建此变量。要创建Integer变量,请执行以下操作:

int counter = 10;

你也问过如何循环,所以在这里。阅读评论以了解代码的作用。如果您还有其他问题,请在下面提问。

public static void main(String[] args) {

    System.out.println("WELCOME TO GUESSING GAME BY JOSH!");
    System.out.println("Rules: Player 1 picks number between 1 - 100 while Player 2 has 10 tries to guess");

    Scanner josh = new Scanner(System.in);

    int guess = 0; // Create these variables up here to access them everywhere in "main"
    int counter = 0;

    boolean continueTheGame = true; // A boolean variable that holds ONLY either true or false

    System.out.println("Enter name here PLAYER 1: ");

    String p1 = josh.nextLine();

    System.out.println("Enter name here PLAYER 2: ");
    String p2 = josh.nextLine();

    System.out.println("Ok, " +  p2 + " look away. " +  p1 + ", Please enter a number and press enter:");

    int answer = josh.nextInt();

    // A while loop will continue as long as a boolean expression is true.
    // So, we create a boolean variable somewhere above called "continueTheGame"
    // As long as this is true, the code INSIDE of the while loop's brackets will repeat.

    // If the user has less than zero guesses left, we can set the variable to false,
    // which will make the loop stop!

    while (continueTheGame == true) { // The start of the while loop

        if (answer >= 100) {

            System.out.println("BUSTED! I said a number between 1 - 100!");

        } else if (answer <= 100) { 

            System.out.println("Guess in the space below.");
            guess = josh.nextInt();

        }

        if (guess == answer) {
            System.out.println("CORRECT!!!!!");
        } else if (guess != answer) {
            counter--;
            System.out.println("You have " + counter + " of guesses left");

            if (counter > 0) { // If they have MORE than zero guesses left, loop again!
                continueTheGame = true;
            } else { // If they have zero guesses left, make it stop looping
                continueTheGame = false;
            }
        }

    }

    // Once the loop ends, the code will start again here,
    // because the bracket above is the final bracket of the WHILE loop

}

答案 1 :(得分:0)

好的,这是您正在寻找的全功能主要方法:

public static void main(String[] args){
            System.out.println("WELCOME TO GUESSING GAME BY JOSH!");
            System.out.println("Rules: Player 1 picks number between 1 - 100 while Player 2 has 10 tries to guess");
            Scanner josh = new Scanner(System.in);
            System.out.println("Enter name here PLAYER 1: ");
             String p1 = josh.nextLine();
             System.out.println("Enter name here PLAYER 2: ");
             String p2 = josh.nextLine();
             System.out.println("Ok, " +  p2 + " look away. " +  p1 + ", Please enter a number and press enter:");
                int answer = josh.nextInt();
                if (answer >= 100){
                    System.out.println("BUSTED! I said a number between 1 - 100!");

                }else {       
                    System.out.println("Guess in the space below.");

                    }
                      for (int count = 10; count>=0; count--) {
                          int guess = josh.nextInt();
                          if (guess == answer){
                              System.out.println("CORRECT!!!!!");
                              System.exit(0);
                          } else {
                        System.out.println("You have " + count + " of guesses left");
                        if (count == 0) {
                            System.out.println("Sorry, you lost, no more tries..");
                            System.exit(0);
                        } 

                      }

                    }


          josh.close();      
        }