嗨,我還是 java 的新手,我想知道如何檢查用戶是否只輸入數字而不是字母,當我不得不將輸入從字串決議為雙精度以便能夠在安慰。但是當我用谷歌搜索如何檢查我的輸入是否只是數字時,我不得不接受一個字串輸入,它給了我 *2 輸入(希望我說得通)。是否有更簡單的版本可以做到這一點,或者我是遺漏了什么。
public class javaCalculator {
public static void main(String[] args){
//initializing two scanners for numbers and operations and creating 3 variables
Scanner numbers = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double number1;
double number2;
String operator;
//getting user input for the type of operations and numbers they want to enter
System.out.print("Enter the operator you would like to choose( , -, *, /): ");
operator = operation.next();
//My program didn't want to take decimals, so I had to parseDouble which takes the input as a string and then
//converts(parse) it to a double which then makes my program run with decimal numbers
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
number1 = Double.parseDouble(numbers.nextLine());
System.out.print("Enter your second number: ");
String num2 = numbers.nextLine();
number2 = Double.parseDouble(numbers.nextLine());
boolean check1 = num1.matches("[0-9] ");
boolean check2 = num2.matches("[0-9] ");
if (check1 == true && check2 == true){
System.out.println("...");
}else {
System.out.println("Only enter numbers not letters.");
}
//Using if else statements to check what operation was chosen above and then depending on
//that choice( , -, *, /) printing a suitable answer to console
//Creating a calculation variable to use in the writing to a file
String calculation;
if (operator.equals(" ")){
calculation = (number1 " " number2 " = " (number1 number2));
System.out.println(calculation);
}else if (operator.equals("-")){
calculation = (number1 " - " number2 " = " (number1 - number2));
System.out.println(calculation);
}else if (operator.equals("*")){
calculation = (number1 " * " number2 " = " (number1 * number2));
System.out.println(calculation);
}else if (operator.equals("/")){
calculation = (number1 " / " number2 " = " (number1 / number2));
System.out.println(calculation);
}else{
calculation = operator ":" " Is not a valid operator!";
System.out.println(calculation);
}
uj5u.com熱心網友回復:
你的方法沒問題。盡管 Scanner lib 提供了 nextDouble() 方法,但我建議使用您正在使用的正則運算式控制元件。即使有一些小問題需要解決:
您首先決議(從字串轉換為雙精度),然后檢查格式。例如,如果用戶輸入了一個字母,那么當 parseDouble 嘗試將 String 轉換為 double 時,您的程式將失敗。因此,從輸入中讀取字串,應用匹配控制元件,如果沒有錯誤則決議。
您的正則運算式匹配任何具有 1 個或多個數字的字串。例如,輸入Hello1將匹配,因為至少有一個數字。然后決議將失敗,因為Hello1不是有效數字。您必須使用僅匹配數字的正則運算式。這個運算式看起來像這樣:“^[0-9] $”
^ 字符表示運算式必須在行首匹配,$ 字符強制運算式在行尾匹配。換句話說,這個運算式應該從字串的開頭到結尾都有數字。添加一個 .trim() (num1.trim().matches("[0-9] ");) 來洗掉開頭或結尾的任何多余空格是一件好事。
第三個建議是,如果您不想使用小數,則 Double 型別可能不是要使用的正確資料型別。Double 可以表示小數。正確的型別應該是整數。
number1 = Integer.parseInt(num1);
@christopher 當您提出錯誤時,您正在列印一條訊息,但程式繼續運行。這就是為什么您在 @Turamarth 解決方案評論中收到錯誤評論的原因
uj5u.com熱心網友回復:
您的代碼試圖讀取相同的數字兩次:
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
number1 = Double.parseDouble(numbers.nextLine());
您應該做的是將數字作為字串讀取,確認它是一個數字,然后僅在匹配時對其進行決議。
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
boolean check1 = num1.matches("[0-9] ");
if(check1){
number1 = Double.parseDouble(num1);
}
else{
//Error handling
}
或者,您可以簡單地嘗試直接決議字串并捕獲例外,即
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
try{
number1 = Double.parseDouble(num1);
} catch(NumberFormatException e){
//Error handling
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/444585.html
上一篇:MSAccess/SQLServer-VBA:將本地檔案上傳到遠程SQL服務器上的檔案流
下一篇:掃描成指標
