简体   繁体   English

为什么我的Java switch语句不起作用?

[英]Why isn't my Java switch statement working?

I am trying to do the following for a Java switch method with a series of JUnit Asserts but am stuck on using "less than" and "greater than" for two cases (see string/int error below), and am not sure how to use the ">" and "<" in my cases. 我正在尝试对具有一系列JUnit断言的Java切换方法执行以下操作,但在两种情况下都无法使用“小于”和“大于”(请参见下面的字符串/整数错误),并且不确定如何在我的情况下,请使用“>”和“ <”。

Here is the exercise followed by my code, followed by the error. 这是练习,后面是我的代码,后面是错误。

/*  
Create a method which uses a switch statement to return a String representing the int passed in as a parameter to the method:
            • given 1 return "One"
            • given 2 return "Two"
            • given 3 return "Three"
            • given 4 return "Four"
            • given an integer > 4, return "Too big"
            • given an integer < 1, return "Too small"
*/
@Test
public void switchIntExample() {
    assertEquals("One", stringRepInt(1));
    assertEquals("Two", stringRepInt(2));
    assertEquals("Three", stringRepInt(3));
    assertEquals("Four", stringRepInt(4));
    assertEquals("Too big.", stringRepInt(>4));      
//        assertEquals("Too small.", stringRepInt(<4));
}

//Switch statement for above:
public String stringRepInt(int numberSize) {

    String numVar = null;

    switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;           
     //TODO:  question on how to do LESS THAN and GREATER THAN:
     // error line: 
     case  (numVar > 4):
            numVar = "Too big.";
            break;

        default:
            break;
    }
    System.out.println(numVar);
    return numVar;
}

ERROR: Error:(293, 27) java: bad operand types for binary operator '>' first type: java.lang.String second type: int 错误:错误:(293,27)Java:二进制运算符'>'的错误操作数类型第一类型:java.lang.String第二类型:int

case statements only support constant expressions (you cannot do less than, or greater than, in a case and you can't test numVar - the String - with less than). case说明只支持常量表达式(你不能做的比小于或更高,在一个case ,你不能测试numVar -的String -用不到)。 You can use an if and something like 您可以使用if和类似的东西

public String stringRepInt(int numberSize) {
    String numVar = null;
    if (numberSize > 4) {
         numVar = "Too big.";
    } else {
        switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;       
        default:
            break;
    }
    System.out.println(numVar);
    return numVar;
}

You should add it to the default section, so like this: 您应该将其添加到默认部分,如下所示:

default:
    numVar = "Too Big";
    break;

The purpose of the default section is to deal with all cases not dealt with by the switch cases. 默认部分的目的是处理所有未由切换案例处理的案例。 You should be taking advantage of that (as @GreenMatt and @FredK mentioned in comments) by putting the 'if checks' in the default section, as follows: 您应该通过在默认部分中放置“如果检查”来利用这一点(如注释中提到的@GreenMatt和@FredK),如下所示:

public String stringRepInt(int numberSize) {

    String numVar = null;

    switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;
        default:
            if(numberSize > 4)
                numVar = "Too big";
            break;
    }
    System.out.println(numVar);
    return numVar;
}

Further, you could add else if (numberSize < 1) numVar = "Too small"; 此外, else if (numberSize < 1) numVar = "Too small";可以添加else if (numberSize < 1) numVar = "Too small"; under the if statement if you want to check for number's smallest than one. 如果要检查是否小于1的数字,请在if语句下。 This is also important because it prevents your method from returning null . 这也很重要,因为它防止您的方法返回null (Which currently happens if the user enters a value less than 1) (如果用户输入的值小于1,则当前会发生这种情况)

The resulting code is as follows: 结果代码如下:

public String stringRepInt(int numberSize) {

    String numVar = null;

    switch (numberSize) {
        case 1:
            numVar = "One";
            break;
        case 2:
            numVar = "Two";
            break;
        case 3:
            numVar = "Three";
            break;
        case 4:
            numVar = "Four";
            break;
        default:
            if(numberSize > 4)
                numVar = "Too big";
            else
                numVar = "Too small";
            break;
    }
    System.out.println(numVar);
    return numVar;
}

In the error line you are comparing String and int. 在错误行中,您正在比较String和int。

error line: case (numVar > 4): numVar = "Too big."; break;

you have declared numVar as String and comparing that with int value 4. Please correct that or I think you are trying to compare numberSize with value 4. Convert that String to int and compare it. 您已将numVar声明为String并将其与int值4进行比较。请更正该错误,或​​者我认为您正尝试将numberSize与值4进行比较。将String转换为int并进行比较。

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

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