简体   繁体   English

如何在 Java 的 Switch 语句中使用字符串

[英]How to use a String inside a Switch Statement in Java

My input String wont appear when im running the code.当我运行代码时,我的输入字符串不会出现。 First it said it was unusable when i had it inside my Main method.首先它说当我在我的 Main 方法中使用它时它无法使用。 After i took it out of my Main method and placed it in the Public class and the unusable problem got away.在我将它从我的 Main 方法中取出并将它放在 Public 类中之后,无法使用的问题就消失了。 But still it wont appear while running the code.但是在运行代码时它仍然不会出现。

public class Number0to9 {    
  public static String input;

  public static void main(String[] args) {        
    tryagain: while (true) {
      System.out.print("Type a number between 0 and 9 : ");

      Scanner scan = new Scanner(System.in);          
      int x = scan.nextInt();
      int output = x;
      input = "You entered number: ";

      switch (x) {
        case 0:
          input += output;
          break tryagain;
        case 1:
          input += output;
          break tryagain;
        case 2:
          input += output;
          break tryagain;
        case 3: 
          input += output;
          break tryagain;
        case 4:
          input += output;
          break tryagain;
        case 5: 
          input += output;
          break tryagain;
        case 6: 
          input += output;
          break tryagain;
        case 7:
          input += output;
          break tryagain;
        case 8:
          input += output;
          break tryagain;
        case 9:
          input += output;
          break tryagain;
        default:
          System.out.println("Your number was bigger than 9");
          break;
      }   
    }        
  } 
} 

should be something like this ?应该是这样的?

public static void main(String[] args) {
        boolean digitEntered = false;
        String input = "You enetred number : ";
        while (digitEntered == false) {
            System.out.print("Type a number between 0 and 9 : ");
            Scanner scan = new Scanner(System.in);
            int x = scan.nextInt();

            if (x >= 0 && x <= 9) {
                digitEntered = true;
                input += x;
            } else {
                System.out.println("Your number was bigger than 9 ");

            }
        }
        System.out.println(input);
    }

Java break statement to terminate the labeled loop.用于终止标记循环的 Java break 语句。 And your String input contains value in it.并且您的 String input包含其中的值。 Try to access it after the while loop.尝试在 while 循环后访问它。

public class Number0to9 {

    public static String input;

    public static void main(String[] args) {

    tryagain : while (true) {
    System.out.print("Type a number between 0 and 9 : ");
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    int output = x;
    input = "You enetred number : ";


    switch (x) {
        case 0:
            input += output;
                break tryagain;
        case 1:
            input += output;
                break tryagain;
        case 2:
            input += output;
                break tryagain;
        case 3: 
            input += output;
                break tryagain;
        case 4:
            input += output;
                break tryagain;
        case 5: 
            input += output;
                break tryagain;
        case 6: 
            input += output;
                break tryagain;
        case 7:
            input += output;
                break tryagain;
        case 8:
            input += output;
                break tryagain;
        case 9:
            input += output;
                break tryagain;
        default:
                System.out.println("Your number was bigger than 9 ");
            break;
    }   
    }
System.out.println(input);
} 
} 
input += output;

The above statement of code can be used for adding input and output only if both the variables are int (or any other arithmetic variable type).只有当两个变量都是 int(或任何其他算术变量类型)时,上述代码语句才能用于添加输入和输出。

If you want to concatenate input and output, first of all, output should be converted to String.如果要连接输入和输出,首先要将输出转换为字符串。

input = input + Integer.toString(output);

Make this change in each case.在每种情况下进行此更改。

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

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