簡體   English   中英

輸入數字時從掃描儀獲取字符串

[英]Getting a string from a scanner when a number is entered

我正在嘗試編寫一個程序,要求用戶輸入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(); } } 

任何幫助將非常感激。 在此先感謝您。

input = stdIn.next(); 與整數比較時,將輸入作為String 因此, String永遠不等於Int

您可以嘗試將while循環條件更改為:

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

注意數字周圍的雙引號

得到回答,但是檢查這個

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();
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM