Java 如何有效地使用计数器变量来计算点数?

Java 如何有效地使用计数器变量来计算点数?,java,Java,我正在制作一个**猜字游戏* Player1输入一个神秘单词,然后输出该单词中的字母数 然后在while循环中,player2猜测单词,如果它们不是神秘单词,则一条语句说“短语不是神秘单词”,,但当player2猜测神秘单词时,以下应为输出,如下面的打印语句所示: else if (phrase.equals(mysteryphrase)){ go = false; counter--; int points = (mysteryphrase.length()*10-co

我正在制作一个**猜字游戏*

Player1输入一个神秘单词,然后输出该单词中的字母数

然后在while循环中,player2猜测单词,如果它们不是神秘单词,则一条语句说“短语不是神秘单词”,但当player2猜测神秘单词时,以下应为输出,如下面的打印语句所示:

else if (phrase.equals(mysteryphrase)){
    go = false;
    counter--;
    int points = (mysteryphrase.length()*10-counter*5);
    System.out.println("Correct! The mystery word is"+mysteryphrase+".");
    System.out.println("You made "+counter+" incorrect guesses");
    System.out.println("You get "+points+" points");
我让它工作,但另一个要求是,当它达到神秘词长度的两倍时,游戏应该停止,它应该说:

else if (counter2 == mysteryphrase.length()*2){
    go = false;
    System.out.println("\n"+"You made "+counter2+" incorrect guesses");
    System.out.println("You get 0 points");
    System.out.println("You lost the game");
} 
我面临的问题是,有一个计算点的公式:

Points = (number of letters in the word * 10 - incorrect guesses *5)
当游戏在两倍神秘词长度后停止时,此选项有效,但当玩家2正确猜出神秘词时,此选项无效。为什么呢

对于那些感兴趣的人,这是我的全部代码:

import java.util.Scanner;
public class Wordguess{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
    int option = Integer.parseInt(keyboard.nextLine());
    String mysteryphrase = keyboard.next();
        System.out.print("The mystery word has "+mysteryphrase.length()+" letters"+".");
        if (option == 1){
            boolean go = true;
            while (go){
                String phrase = keyboard.next();
                if (!phrase.equals(mysteryphrase)){
                    System.out.print("The mystery word is not "+phrase+".");
                }
                else if (phrase.equals(mysteryphrase)){
                    go = false;
                    System.out.print("Correct! The mystery word is "+mysteryphrase+".");
                }
            }
        }
        else if (option == 2){
            int counter = 1;
            boolean go = true;
            while (go){
                String phrase = keyboard.next();
                if (!phrase.equals(mysteryphrase)){
                    System.out.print("The mystery word is not "+phrase);
                    counter++;
                }
                else if (phrase.equals(mysteryphrase)){
                    go = false;
                    counter--;
                    int points = (mysteryphrase.length()*10-counter*5);
                    System.out.println("Correct! The mystery word is"+mysteryphrase+".");
                    System.out.println("You made "+counter+" incorrect guesses");
                    System.out.println("You get "+points+" points");
                }
            }
        }
        else if (option == 3){
            int counter = 1;
            int counter2 = 0;
            boolean go = true;
            while (go){
                String phrase = keyboard.next();
                if (!phrase.equals(mysteryphrase)){
                    System.out.print("The mystery word is not "+phrase);
                    counter++;
                    counter2++;
                }
                else if (phrase.equals(mysteryphrase)){
                    go = false;
                    counter--;
                    int points = (mysteryphrase.length()*10-counter*5);
                    System.out.println("Correct! The mystery word is"+mysteryphrase+".");
                    System.out.println("You made "+counter+" incorrect guesses");
                    System.out.println("You get "+points+" points");
                }
                else if (counter2 == mysteryphrase.length()*2){
                    go = false;
                    System.out.println("\n"+"You made "+counter2+" incorrect guesses");
                    System.out.println("You get 0 points");
                    System.out.println("You lost the game");
                } 
            }
        }
    }
}
正如你所见,我使用了两个计数器,一个用于赢,一个用于检查神秘单词长度是否加倍,游戏结束。当玩家2猜对时,“计数器”似乎不适用于点数

以下是我的输出,该输出不适用于点:

你得5分
应该是在玩家正确猜测后和5次猜测后:

你得70分

如果用户输入3作为选项,则您有以下代码

if(!phrase.equals(mysteryphrase)){
那你有

else if(短语.equals(mysteryphrase)){
之后你有

else if(counter2==mysteryphrase.length()*2){
如果,您将永远不会输入最后一个
else。用户输入的单词可能是神秘单词,也可能不是。没有第三个选项。也许您只需要删除最后一个else并将其设置为

if(counter2==mysteryphrase.length()*2){
编辑 实际上,您需要在每次错误猜测后立即检查
counter2
的值,因此初始条件应为

if(!phrase.equals(mysteryphrase)){
系统输出打印(“神秘词不是”+短语);
计数器++;
计数器2++;
if(counter2==mysteryphrase.length()*2){
go=假;
System.out.println(“\n”+“您做出了”+counter2+“错误的猜测”);
System.out.println(“得0分”);
System.out.println(“你输掉了比赛”);
} 
}

适用于您的程序的简短代码:

import java.util.*;
public class GFG{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the mystery word");
        String mystery=sc.nextLine();
        int counter=0;
        while(counter<mystery.length()*2){
            System.out.println("Guess the mystery word!");
            String guess=sc.nextLine();
            if(guess.equals(mystery))
                break;
            counter++;
        }
        if(counter==mystery.length()*2)
            System.out.println("You made "+counter+" incorrect guesses so your score is 0");
        int score=mystery.length()*10-counter*5;
        System.out.println("You made "+counter+" incorrect guesses and your score is "+score);
    }
}
else if (counter2 == mysteryphrase.length()*2){
    go = false;
    System.out.println("\n"+"You made "+counter2+" incorrect guesses");
    System.out.println("You get 0 points");
    System.out.println("You lost the game");
}
import java.util.*;
公共类GFG{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入神秘词”);
字符串奥秘=sc.nextLine();
int计数器=0;

而(计数器以下代码将永远不会在您的程序中执行:

import java.util.*;
public class GFG{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the mystery word");
        String mystery=sc.nextLine();
        int counter=0;
        while(counter<mystery.length()*2){
            System.out.println("Guess the mystery word!");
            String guess=sc.nextLine();
            if(guess.equals(mystery))
                break;
            counter++;
        }
        if(counter==mystery.length()*2)
            System.out.println("You made "+counter+" incorrect guesses so your score is 0");
        int score=mystery.length()*10-counter*5;
        System.out.println("You made "+counter+" incorrect guesses and your score is "+score);
    }
}
else if (counter2 == mysteryphrase.length()*2){
    go = false;
    System.out.println("\n"+"You made "+counter2+" incorrect guesses");
    System.out.println("You get 0 points");
    System.out.println("You lost the game");
}
原因是,当您检查时,
如果(!phrase.equals(mysteryphrase))
,则它将是
真的
假的
,因此
否则如果
不仅没有意义,而且还会导致您面临的混乱和问题

正确的程序: 运行示例:

Enter option: 1
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
Enter option: 2
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not hi
Your word: bye
The mystery word is not bye
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hi
The mystery word has 2 letters.
Your word: hello
The mystery word is not 'hello'.
Your word: world
The mystery word is not 'world'.
Your word: bye
The mystery word is not 'bye'.
Your word: moon
The mystery word is not 'moon'.
Your word: hi

You made 4 incorrect guesses.
You get 0 points
You lost the game
另一个示例运行:

Enter option: 1
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
Enter option: 2
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not hi
Your word: bye
The mystery word is not bye
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hi
The mystery word has 2 letters.
Your word: hello
The mystery word is not 'hello'.
Your word: world
The mystery word is not 'world'.
Your word: bye
The mystery word is not 'bye'.
Your word: moon
The mystery word is not 'moon'.
Your word: hi

You made 4 incorrect guesses.
You get 0 points
You lost the game
另一个示例运行:

Enter option: 1
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
Enter option: 2
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not hi
Your word: bye
The mystery word is not bye
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hi
The mystery word has 2 letters.
Your word: hello
The mystery word is not 'hello'.
Your word: world
The mystery word is not 'world'.
Your word: bye
The mystery word is not 'bye'.
Your word: moon
The mystery word is not 'moon'.
Your word: hi

You made 4 incorrect guesses.
You get 0 points
You lost the game
另一个示例运行:

Enter option: 1
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
Enter option: 2
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not hi
Your word: bye
The mystery word is not bye
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hello
The mystery word has 5 letters.
Your word: hi
The mystery word is not 'hi'.
Your word: bye
The mystery word is not 'bye'.
Your word: hello
Correct! The mystery word is 'hello'.
You made 2 incorrect guesses.
You get 40 points.
Enter option: 3
Enter the mystery word: hi
The mystery word has 2 letters.
Your word: hello
The mystery word is not 'hello'.
Your word: world
The mystery word is not 'world'.
Your word: bye
The mystery word is not 'bye'.
Your word: moon
The mystery word is not 'moon'.
Your word: hi

You made 4 incorrect guesses.
You get 0 points
You lost the game

你能告诉我用户在哪里输入这个词吗?你直接把输入解析成整数(在第5行)很抱歉,我接受了你的回答,谢谢,这也是我的新帐户,我想请你帮忙,但我无法评论。我刚刚看到你的消息。我可以看到它已经被回答,你已经接受了,这意味着你已经得到了你想要的。祝你成功!@hussain123-请清理上面的消息,因为它与请参阅本问答。此外,请在问题下方发表评论。应根据so指南删除与当前问答无关的评论。