简体   繁体   English

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

[英]How can I set up my game to accept either a boolean or an int at the same time

Inside of the while loop, I try to set up purchase as an option to use as an input storage device and turn into a choice from among purchasable options. 在while循环内,我尝试将购买设置为用作输入存储设备的选项,并从可购买选项中选择一种。

I would like to know if it is possible, and if so, how, to accept either a boolean or an integer at the same time, depending on whether the user would like to increase pageAmount, or buy an upgrade. 我想知道是否有可能,如果可以,如何同时接受布尔值或整数,具体取决于用户是要增加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);
            }
        }
    }
}

You could accept an entire line, and then see whether it's an integer. 您可以接受整行,然后查看它是否为整数。 Something like this: 像这样:

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. */)) {
        // ...
    }
}

Also note that you have a bug in your line 另请注意,您的生产线中存在错误

while (gameStarted = true)

which makes the while loop infinite (since = sets the gameStarted variable to true , causing the loop never to end). 这使得while循环无限(因为=gameStarted变量设置为true ,导致循环永不结束)。 It should be 它应该是

while (gameStarted == true)

or just 要不就

while (gameStarted)

This probably is not the most efficient solution, and certainly not the most sophisticated as I have only been learning Java for two years at my high school, so no fancy methods or special stuff. 这可能不是最有效的解决方案,当然也不是最复杂的解决方案,因为我在高中时才学习Java两年了,所以没有什么特别的方法或特殊的东西。

You could use a code block like this: 您可以使用如下代码块:

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
}

This is how I would do it because I am not fancy, but I am not confident this is the most efficient or most correct way to solve your problem. 因为我不喜欢它,所以我会这样做,但是我不确定这是解决您的问题的最有效或最正确的方法。 And, as Doorknob pointed out, the condition for your while - loop should not have a single = , but should have a == as this is the proper way to compare primitive data such as booleans. 而且,正如Doorknob所指出的,while- while - loop的条件不应只有单个= ,而应具有==因为这是比较原始数据(如布尔值)的正确方法。 In the case that you have, you are setting a boolean to a certain value which would, in this case, put you into an infinite loop. 在您拥有的情况下,您正在将布尔值设置为某个值,在这种情况下,这将使您陷入无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM