创建一个基于用户字符串输入返回整数的方法

时间:2023-01-26 15:11:11

I am not too great at java, but I'm trying to create a method that takes input from the user and returns a value. However difficulty always returns 0?

我在java上不太好,但我正在尝试创建一个从用户获取输入并返回值的方法。但难度总是返回0?

chosenDifficulty = userName.next().toUpperCase();

chosenDifficulty = userName.next()。toUpperCase();

 public int getDifficulty() {
    int difficulty = 0;
    if (chosenDifficulty == "HUMAN") {
            difficulty = 1;
        if (chosenDifficulty == "RANDOM") {
            difficulty = 2;
        }else{
            difficulty = 3;
        }
    }
     return difficulty;
}

Thanks for any help, it may just be because its late, but i really can't figure it out....or maybe its just not possible to do it this way....thanks in advance for any help.

感谢您的帮助,这可能只是因为它已经很晚了,但我真的无法弄清楚....或者它可能不会这样做......请提前感谢任何帮助。

4 个解决方案

#1


Kevin's answer is good (to wit: there's a missing close-brace, and String equality comparisons should be done with .equals(), not ==).

凯文的答案是好的(比如:缺少关闭括号,字符串相等比较应该用.equals(),而不是==)。

But as an additional option: this sort of comparison is a good place for a switch statement, instead of chained if. This assumes you're running in at least Java 7, which added switch statements for strings. Matter of taste. switch() is supposedly a bit more efficient, but I think the deciding factor in most circumstances should be which you find more readable.

但作为一个额外的选择:这种比较是一个开关语句的好地方,而不是链接if。这假设您至少运行Java 7,它为字符串添加了switch语句。味道很重要。 switch()应该更有效率,但我认为在大多数情况下决定因素应该是你发现更具可读性的因素。

public int getDifficulty() {
    int difficulty;
    switch (chosenDifficulty) {
        case "HUMAN":
            difficulty = 1;
            break;
        case "RANDOM":
            difficulty = 2;
            break;
        default:
            difficulty = 3;
    }
    return difficulty;
}

#2


It may always return 0, for two reasons.

由于两个原因,它可能总是返回0。

First, you can't perform string equality checks in Java with == and expect consistent results. You almost always want to use the ".equals()" method for checking equality between objects. The exact reasons are explained well here:

首先,您不能使用==在Java中执行字符串相等性检查,并期望得到一致的结果。您几乎总是希望使用“.equals()”方法来检查对象之间的相等性。这里解释了确切的原因:

http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

Second, you are missing an end-brace.

其次,你缺少一个结束支撑。

Below should work:

下面应该工作:

public int getDifficulty() {
    int difficulty = 0;
    if (chosenDifficulty.equals("HUMAN")) {
        difficulty = 1;
    } else if (chosenDifficulty.equals("RANDOM")) {
        difficulty = 2;
    }else {
        difficulty = 3;
    }
    return difficulty;
}

#3


First of all you you should use the .equals() method instead of == when comparing strings.

首先,在比较字符串时,您应该使用.equals()方法而不是==。

if (chosenDifficulty.equals("HUMAN")) {

The second if statement, comparing to "RANDOM", is inside the first. This means it is only used when chosenDifficulty is equal to "HUMAN". As this second if statement has an else condition difficulty would always be set to 3 if the if statement was reached, there is no scenario where difficulty is set to 2. This is because to satisfy the first if statement chosenDifficulty must be "HUMAN".Difficulty woud then be set to 1. It would then be set to 3 as it would not be equal to "RANDOM". Or if the first criteria was not met, chosenDifficulty equal to "HUMAN", difficulty would remain as 0.

与“RANDOM”相比,第二个if语句位于第一个。这意味着它仅在被选择的难度等于“人类”时使用。由于第二个if语句具有else条件,如果达到if语句,则难度总是设置为3,没有将难度设置为2的情况。这是因为要满足第一个if语句selectedDifficulty必须是“HUMAN”。然后将难度设置为1.然后将其设置为3,因为它不等于“RANDOM”。或者如果没有达到第一个标准,选择难度等于“人”,难度将保持为0。

I have posted below what i think you wanted. If chosenDifficulty is "HUMAN" the difficulty is 1. If it is "RANDOM" the difficulty is 2. If it is anything else it returns 3.

我在下面发布了我认为你想要的东西。如果选择难度为“人”则难度为1.如果是“随机”则难度为2.如果是其他任何东西则返回3。

 public int getDifficulty() {
       int difficulty = 3;
    if (chosenDifficulty.equals("HUMAN")) {
            difficulty = 1;
          }
        if (chosenDifficulty.equals("RANDOM")) {
            difficulty = 2;
        }   
     return difficulty;
  }

#4


Instead of casting to UPPERCASE and checking the content. Here the objective is check the content irrespective of case so remove

而不是转换为大写和检查内容。这里的目标是检查内容而不管案例如何删除

      chosenDifficulty = userName.next().toUpperCase();

Then use equalsIgnoreCase

然后使用equalsIgnoreCase

      chosenDifficulty.equalIgnoreCase("HUMAN")

So that ignoring the case only content will be matched.

因此忽略案例只会匹配内容。

#1


Kevin's answer is good (to wit: there's a missing close-brace, and String equality comparisons should be done with .equals(), not ==).

凯文的答案是好的(比如:缺少关闭括号,字符串相等比较应该用.equals(),而不是==)。

But as an additional option: this sort of comparison is a good place for a switch statement, instead of chained if. This assumes you're running in at least Java 7, which added switch statements for strings. Matter of taste. switch() is supposedly a bit more efficient, but I think the deciding factor in most circumstances should be which you find more readable.

但作为一个额外的选择:这种比较是一个开关语句的好地方,而不是链接if。这假设您至少运行Java 7,它为字符串添加了switch语句。味道很重要。 switch()应该更有效率,但我认为在大多数情况下决定因素应该是你发现更具可读性的因素。

public int getDifficulty() {
    int difficulty;
    switch (chosenDifficulty) {
        case "HUMAN":
            difficulty = 1;
            break;
        case "RANDOM":
            difficulty = 2;
            break;
        default:
            difficulty = 3;
    }
    return difficulty;
}

#2


It may always return 0, for two reasons.

由于两个原因,它可能总是返回0。

First, you can't perform string equality checks in Java with == and expect consistent results. You almost always want to use the ".equals()" method for checking equality between objects. The exact reasons are explained well here:

首先,您不能使用==在Java中执行字符串相等性检查,并期望得到一致的结果。您几乎总是希望使用“.equals()”方法来检查对象之间的相等性。这里解释了确切的原因:

http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

Second, you are missing an end-brace.

其次,你缺少一个结束支撑。

Below should work:

下面应该工作:

public int getDifficulty() {
    int difficulty = 0;
    if (chosenDifficulty.equals("HUMAN")) {
        difficulty = 1;
    } else if (chosenDifficulty.equals("RANDOM")) {
        difficulty = 2;
    }else {
        difficulty = 3;
    }
    return difficulty;
}

#3


First of all you you should use the .equals() method instead of == when comparing strings.

首先,在比较字符串时,您应该使用.equals()方法而不是==。

if (chosenDifficulty.equals("HUMAN")) {

The second if statement, comparing to "RANDOM", is inside the first. This means it is only used when chosenDifficulty is equal to "HUMAN". As this second if statement has an else condition difficulty would always be set to 3 if the if statement was reached, there is no scenario where difficulty is set to 2. This is because to satisfy the first if statement chosenDifficulty must be "HUMAN".Difficulty woud then be set to 1. It would then be set to 3 as it would not be equal to "RANDOM". Or if the first criteria was not met, chosenDifficulty equal to "HUMAN", difficulty would remain as 0.

与“RANDOM”相比,第二个if语句位于第一个。这意味着它仅在被选择的难度等于“人类”时使用。由于第二个if语句具有else条件,如果达到if语句,则难度总是设置为3,没有将难度设置为2的情况。这是因为要满足第一个if语句selectedDifficulty必须是“HUMAN”。然后将难度设置为1.然后将其设置为3,因为它不等于“RANDOM”。或者如果没有达到第一个标准,选择难度等于“人”,难度将保持为0。

I have posted below what i think you wanted. If chosenDifficulty is "HUMAN" the difficulty is 1. If it is "RANDOM" the difficulty is 2. If it is anything else it returns 3.

我在下面发布了我认为你想要的东西。如果选择难度为“人”则难度为1.如果是“随机”则难度为2.如果是其他任何东西则返回3。

 public int getDifficulty() {
       int difficulty = 3;
    if (chosenDifficulty.equals("HUMAN")) {
            difficulty = 1;
          }
        if (chosenDifficulty.equals("RANDOM")) {
            difficulty = 2;
        }   
     return difficulty;
  }

#4


Instead of casting to UPPERCASE and checking the content. Here the objective is check the content irrespective of case so remove

而不是转换为大写和检查内容。这里的目标是检查内容而不管案例如何删除

      chosenDifficulty = userName.next().toUpperCase();

Then use equalsIgnoreCase

然后使用equalsIgnoreCase

      chosenDifficulty.equalIgnoreCase("HUMAN")

So that ignoring the case only content will be matched.

因此忽略案例只会匹配内容。