输入数字时从扫描仪获取字符串

时间:2015-03-24 02:00:52

标签: java

我正在尝试编写一个程序,询问用户2个数字,然后要求用户通过输入命令的对应编号从菜单中选择命令。 我可以编写程序,如果我将输入作为一个Int,但无法弄清楚字符串,它也必须是一个字符串。 当我进入while循环以验证用户输入时,我遇到问题,当语句为false时它不会停止它将保持在循环中我无法弄清楚我做错了什么。 这是我的代码。

import java.util.Scanner;
public class ab {
  public static void main(String[] args) {
    System.out.println("-------------------------------------");
    Scanner stdIn = new Scanner(System.in);
    double L;
    System.out.print("Enter the left operand: ");
    L = stdIn.nextDouble();
    double R;
    System.out.print("Enter the right operand: ");
    R = stdIn.nextDouble();
    System.out.println("-------------------------------------");
    System.out.println("1 -> Multiplication");
    System.out.println("2 -> Division");
    System.out.println("3 -> Addition");
    System.out.println("4 -> Subraction");
    System.out.println("-------------------------------------");
    String input;
    System.out.print("Choose one of the following commands by enterning the corresponding number: ");
    input = stdIn.next();
    System.out.println();
    while (!input.equals(1) && !input.equals(2) && !input.equals(3) && !input.equals(4)) {
      System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
      input = stdIn.next();
      System.out.println();
      if (input.equals(1)) {
        System.out.print(L + " * " + R + " = " + (L * R));
      } else if (input.equals(2)) {
        System.out.print(L + " / " + R + " = " + (L / R));
      } else if (input.equals(3)) {
        System.out.print(L + " + " + R + " = " + (L + R));
      } else {
        System.out.print(L + " - " + R + " = " + (L - R));
      }
    }
    stdIn.close();
  }
}

非常感谢任何帮助。 先谢谢你。

2 个答案:

答案 0 :(得分:0)

input = stdIn.next();行正在输入String 而你的比较则是整数。因此,String永远不会等于Int

您可以尝试将while循环条件更改为:

while (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4"))

请注意数字附近的双引号

答案 1 :(得分:0)

已回答,但请查看

import java.util.Scanner;

public class ab {

public static void main(String[] args) {
    System.out.println("-------------------------------------");
    Scanner stdIn = new Scanner(System.in);
    double L;
    System.out.print("Enter the left operand: ");
    L = stdIn.nextDouble();
    double R;
    System.out.print("Enter the right operand: ");
    R = stdIn.nextDouble();
    System.out.println("-------------------------------------");
    System.out.println("1 -> Multiplication");
    System.out.println("2 -> Division");
    System.out.println("3 -> Addition");
    System.out.println("4 -> Subraction");
    System.out.println("-------------------------------------");
    String input;
    System.out.print("Choose one of the following commands by enterning the corresponding number: ");

    input = stdIn.next();

    while (true) {

        if (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4")) {
            System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
            input = stdIn.next();
        } else {
            if (input.equals("1")) {
                System.out.print(L + " * " + R + " = " + (L * R));
                break;
            } else if (input.equals("2")) {
                System.out.print(L + " / " + R + " = " + (L / R));
                break;
            } else if (input.equals("3")) {
                System.out.print(L + " + " + R + " = " + (L + R));
                break;
            } else {
                System.out.print(L + " - " + R + " = " + (L - R));
                break;
            }
        }

    }


    stdIn.close();
}

}