我有一個條件,如果用戶輸入負數或大于 100 的數字或字串,則應列印錯誤訊息“這不是有效百分比,我需要一個介于 0-100 之間的數字。嘗試再次。” 并要求用戶重新輸入有效號碼。如果用戶決定只輸入,則應計算所有輸入并列印平均金額。
public static void main(String[ ] args) {
int count = 0; //count to stop loop
double[ ] aGrade = new double[SIZE];
String input = new String("");
Scanner scan = new Scanner(System.in);
double total = 0;
int gTotal = aGrade.length;
boolean exit = false;
while ((count < SIZE) && (!exit)) {
System.out.print("Enter number " (count 1) ": " "\n");
try {
input = scan.nextLine();
if (Double.parseDouble(input) > 0 && Double.parseDouble(input) < 100) {
aGrade[count] = Double.parseDouble(input); //put into the array
count ; //only increment count if success
} else
System.out.println("That wasn't a valid percentage,"
" I need a number between 0-100. Try again.");
} catch (NumberFormatException nfe) {
exit = true; //exit loop
}
}
System.out.println("number of grades entered: " count "\n");
for (int i = 0; i < count; i ) {
// print entered grade
System.out.println("grade " (i 1) ": " aGrade[i]);
}
for (int i = 0; i < count; i ) {
total = aGrade[i];
}
// calculate and print the average
System.out.println("\n" "Average grade: " total /count);
但是當我運行我的代碼時,如果我輸入字母,它將不允許用戶重新輸入值,而是列印計算出的任何內容。我認為它在我的 if-else 陳述句中,但我不確定如何
uj5u.com熱心網友回復:
當我們嘗試將 String 轉換為 Double 時,它??會拋出java.lang.NumberFormatException。因此,無論何時您輸入 String 或 char 而不是 else 時,它??都會進入 catch 塊。根據您的代碼 else 塊僅在用戶輸入負數或大于 100 的數字時執行。
我更新了你的代碼。請檢查一下。
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
int count = 0; // count to stop loop
double[] aGrade = new double[3];
String input = new String("");
Scanner scan = new Scanner(System.in);
double total = 0;
int gTotal = aGrade.length;
boolean exit = false;
while ((count < 3) && (!exit)) {
System.out.print("Enter number " (count 1) ": " "\n");
try {
input = scan.nextLine();
if (Double.parseDouble(input) > 0 && Double.parseDouble(input) < 100) {
aGrade[count] = Double.parseDouble(input); // put into the array
count ; // only increment count if success
} else
System.out
.println("That wasn't a valid percentage," " I need a number between 0-100. Try again.");
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
exit = true; // exit loop
}
}
if (!exit) {
System.out.println("number of grades entered: " count "\n");
for (int i = 0; i < count; i ) {
// print entered grade
System.out.println("grade " (i 1) ": " aGrade[i]);
}
for (int i = 0; i < count; i ) {
total = aGrade[i];
}
// calculate and print the average
System.out.println("\n" "Average grade: " total / count);
}else {
System.out
.println("That wasn't a valid percentage," " I need a number between 0-100. Try again.");
}
}
}
uj5u.com熱心網友回復:
嘗試這樣的事情:
boolean ok = false;
try {
input = scan.nextLine();
if ("".equals(input)) {
ok = true;
exit = true;
} else if (Double.parseDouble(input) >= 0 && Double.parseDouble(input) <= 100) {
aGrade[count] = Double.parseDouble(input); //put into the array
count ; //only increment count if success
ok = true;
}
} catch (NumberFormatException nfe) {
// nothing
}
if (!ok) {
System.out.println("That wasn't a valid percentage,"
" I need a number between 0-100. Try again.");
}
uj5u.com熱心網友回復:
如果您輸入 letter 作為輸入,您將永遠不會else進入 if 陳述句的部分,因為里面的代碼if拋出例外,然后您就進入了catch部分。另外,您在內部catch部分寫了,當NumberFormatException發生時(當您輸入字母而不是數字時),設定exit為true,這就是程式不允許您在輸入字母后再次輸入的原因。解決這些問題,它會起作用。另外,看看如何除錯您的程式,學習該技能,它將幫助您將來解決此類問題。
uj5u.com熱心網友回復:
''Double.parseDouble'' 表示將字串轉換為double
一NumberFormatException,當你進入無法決議的字串拋出例外,所以此行exit = true;的代碼會被執行:
/**
* Returns a new {@code double} initialized to the value
* represented by the specified {@code String}, as performed
* by the {@code valueOf} method of class
* {@code Double}.
*
* @param s the string to be parsed.
* @return the {@code double} value represented by the string
* argument.
* @throws NullPointerException if the string is null
* @throws NumberFormatException if the string does not contain
* a parsable {@code double}.
* @see java.lang.Double#valueOf(String)
* @since 1.2
*/
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354276.html
上一篇:每盒包含多個資訊的陣列
下一篇:一個陣列到另一個陣列的轉換
