在查找了許多方法來重新啓動自己的Java程序之後,while循環看起來是最簡單的選項。嘗試重新啓動我的Java程序時遇到問題
import java.util.Scanner;
class a {
public static void main(String args[]){
boolean done = false;
int oper;
Scanner input = new Scanner(System.in);
System.out.println("McMackins Calc v2.0 (Now with fewer crashes!)");
while (!done)
{
System.out.println("What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):");
while (!input.hasNextInt()){
System.out.println("Enter a valid integer.");
input.next();
}
oper = input.nextInt();
switch (oper){
case 0:
done = true;
break;
case 1:
add addObject = new add();
addObject.getSum();
break;
case 2:
sub subObject = new sub();
subObject.getDifference();
break;
case 3:
times multObject = new times();
multObject.getProduct();
break;
case 4:
divide divObject = new divide();
divObject.getQuotient();
break;
case 5:
remain remObject = new remain();
remObject.getRemainder();
break;
case 6:
avg avgObject = new avg();
avgObject.getAvg();
break;
case 7:
interest intObject = new interest();
intObject.getInterest();
break;
default:
System.out.println("Invalid entry.");
break;
}
}
input.close();
}
}
然而,這似乎是在第一次循環結束時拋出了一個NoSuchElementException,而導致程序崩潰:這是一個基本的計算器程序的一個例子,我有嘗試此。這個類的功能是從用戶的初始輸入中確定使用哪個類,這將決定執行哪個數學運算。一切工作正常,沒有while (!done)
循環。
用法示例:
McMackins Calc v2.0 (Now with fewer crashes!)
What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):
1
How many addends?
1
Enter your numbers now.
1
You have entered 1 addend.
The sum is: 1.0
What operation? (0 for quit, 1 for add, 2 for subtract, 3 for multiply, 4 for divide, 5 for divide with remainder, 6 for average, 7 for account interest):
Enter a valid integer.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at a.main(a.java:13)
我也嘗試了具有其他類請參閱本之一,但由於主要是一個靜態方法,我不能訪問它,我希望的方式。
請注意,我在Java上是一個初學者,這就是爲什麼我的程序非常簡單,所以儘量保持它簡單,如果它可以是,或後代碼,然後在詳細說明它是什麼意思,所以我不僅可以解決這個問題,而且可以解決未來的問題。
謝謝!
編輯:
代碼在我的編輯器中格式化得更好。當我在這裏發佈時,大括號出現在奇怪的位置。
由於顯然a
被寫入正確,因此這是我的add
類。希望這會清除一些東西。
import java.util.Scanner;
public class add {
public void getSum(){
Scanner input = new Scanner(System.in);
double total, addend;
int entries, count;
total = 0;
count = 0;
System.out.println("How many addends?");
while (!input.hasNextInt()){
System.out.println("Enter a valid integer.");
input.next();
}
entries = input.nextInt();
System.out.println("Enter your numbers now.");
while (count < entries){
while (!input.hasNextDouble()){
System.out.println("Enter a valid number.");
input.next();
}
addend = input.nextDouble();
total = total + addend;
count++;
if (count == 1){
System.out.println("You have entered " + count + " addend.");
}else if (count > entries){
System.out.println("You have entered too many addends! Contact program developer.");
}else{
System.out.println("You have entered " + count + " addends.");
}
}
System.out.println("The sum is: " + total);
input.close();
}
}
如何發佈異常? – John3136
你能否更好地格式化代碼。縮進是錯誤的,並且不清楚哪個'while'結束於哪裏。 – tom