Java 如何设置我的游戏以同时接受布尔值或整数

Java 如何设置我的游戏以同时接受布尔值或整数,java,Java,在while循环中,我尝试将purchase设置为一个选项,用作输入存储设备,并从可购买选项中进行选择 我想知道是否有可能,如果有,如何同时接受布尔值或整数,这取决于用户是否希望增加pageAmount或购买升级 import java.util.Scanner; public class BookieClickerV1 { public static void main(String [] args) { double pageAmount = 0; int penNumbe

在while循环中,我尝试将purchase设置为一个选项,用作输入存储设备,并从可购买选项中进行选择

我想知道是否有可能,如果有,如何同时接受布尔值或整数,这取决于用户是否希望增加pageAmount或购买升级

import java.util.Scanner;

public class BookieClickerV1
{
public static void main(String [] args)
{
    double pageAmount = 0;
    int penNumber = 0;
    int scribeNumber = 0;
    int printingNumber = 0;
    int printerNumber = 0;
    int conwayNumber = 0;
    int mageNumber = 0;
    boolean gameStarted = true;
    boolean click = false;
    int purchase = 0;
    Scanner reader = new Scanner(System.in);

        System.out.println("Welcome to Bookie Clicker V1.0.");
        System.out.println("As this is the first version of the game, it can        be rather buggy");
        System.out.println("When you are ready to begin, type 'True' to represent a click.");
        System.out.println("Use the reference handed to you to know what you're purchasing.");

    while (gameStarted = true)
    {
        click = reader.nextBoolean();


            if (purchase == 1)
            {
                pageAmount -= Math.pow(15, (1.2 * (penNumber + 1)));
                penNumber += 1;
                purchase = 0;
            }
            if (purchase == 2)
            {
                pageAmount -= Math.pow(100, (1.2 * (scribeNumber + 1)));
                scribeNumber += 1;
                purchase = 0;
            }
            if (click == true)
            {
                if (penNumber > 0)
                {
                    pageAmount++;
                }


                pageAmount++;
                click = false;
                System.out.println("Your total number of pages written is "       + pageAmount);
            }
        }
    }
}

您可以接受整行,然后查看它是否为整数。大概是这样的:

String line = reader.nextLine();
try {
    int purchase = Integer.parseInt(line);
    // ...
} catch (NumberFormatException e) {
    // Just use .equals() if you want it to be case-sensitive.
    if (line.equalsIgnoreCase("True" /* or "Click", etc. */)) {
        // ...
    }
}
String a = scanner.next();//store input, makes it easier to manipulate
if(a.startsWith("T") || a.startsWith("F"))//this assumes the user enters true/false as an input
{
     //execute correct code, you could set a boolean true, execute a method
     //or whatever else you wanted to do
}

else
{
    int upgradeInt = Integer.parseInt(a);//you need this to get the 
    //input converted into an int. This is an edit, I forgot this step.      
    //executes correct code and can use String a to pass an upgrade or 
    //however you need your code to run
}

还要注意的是,您的产品线中有一个bug

while (gameStarted = true)
这使得while循环无限大(因为
=
gameStarted
变量设置为
true
,导致循环永不结束)。应该是

while (gameStarted == true)
或者只是

while (gameStarted)

这可能不是最有效的解决方案,当然也不是最复杂的解决方案,因为我在高中只学了两年Java,所以没有什么新奇的方法或特殊的东西

您可以使用如下代码块:

String line = reader.nextLine();
try {
    int purchase = Integer.parseInt(line);
    // ...
} catch (NumberFormatException e) {
    // Just use .equals() if you want it to be case-sensitive.
    if (line.equalsIgnoreCase("True" /* or "Click", etc. */)) {
        // ...
    }
}
String a = scanner.next();//store input, makes it easier to manipulate
if(a.startsWith("T") || a.startsWith("F"))//this assumes the user enters true/false as an input
{
     //execute correct code, you could set a boolean true, execute a method
     //or whatever else you wanted to do
}

else
{
    int upgradeInt = Integer.parseInt(a);//you need this to get the 
    //input converted into an int. This is an edit, I forgot this step.      
    //executes correct code and can use String a to pass an upgrade or 
    //however you need your code to run
}

我会这样做,因为我不喜欢,但我不相信这是解决问题的最有效或最正确的方法。而且,正如门把手所指出的,while-loop的条件不应该有一个
=
,而应该有一个
=
,因为这是比较布尔等原始数据的正确方法。在这种情况下,您将布尔值设置为某个值,在这种情况下,这将使您进入一个无限循环。

我只是好奇……像这样检查用户输入对我来说是有意义的,但是您是否建议在启动时将整个代码块放入(gameStarted==true)在catch block?@doorknold中,您可以继续删除您的评论。一旦你有了,我会清除这个。如果用户输入“Test”作为他的输入字符串会怎么样?@SaurabhJinturkar两件事:1)根据问题中的信息,他为什么要输入“Test”?我假设,用户将输入一个数字用于升级,输入“真”/“假”用于增加
pageAmount
。2) 这只是一个例子。逻辑是正确的。OP所要做的就是调整逻辑,使其符合允许用户输入的确切条件。Daylen,请在可能的情况下检查答案,并选择一个符合您需要且有效的答案。Cmon Daylen。我不是想纠缠你,但你需要选择一个解决方案。哇。自从你发布问题后,你就一直没上过电视。你一定在别处找到了答案。