我的程序似乎工作正常,除了當它應該循環回部分詢問用戶操作員消息顯示,但是沒有空間讓用戶輸入操作員,它直接跳轉到要求第一個號碼,我不知道我做錯了什麼或不做什麼。我需要幫助,請!程序沒有正確循環
這裏是我到目前爲止有:
import java.util.Scanner;
public class LabTresFinal
{
public static void main (String [] arguments)
{
Scanner Keyboard = new Scanner (System.in);
//Declared Variables
String operator;
double more;
double number1;
double number2;
double ans;
boolean flag= true;
do
{
//Prompt user for operation and numbers
//Had to prompt for operation first otherwise my program was not runing correctly
System.out.println("Please choose an operation to be performed: + , - , * ,/, or % .");
operator = Keyboard.nextLine();
System.out.println("Enter first number");
number1 = Keyboard.nextDouble();
System.out.println("Enter second number");
number2 = Keyboard.nextDouble();
switch (operator)
{
case ("+"):
ans = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + ans);
break;
case ("-"):
ans = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + ans);
break;
case ("*"):
ans = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + ans);
break;
case ("/"):
if (number2 >= 1)
{
ans = number1/number2;
System.out.println(number1 + "/" + number2 + " = " + ans);
}else if (number2 ==0)
{
System.out.println("Error: Operation cannot be performed with second number as zero, please try again");
while(flag)
{
System.out.println("Enter second number");
number2 = Keyboard.nextDouble();
if(number2>=1)
{
ans = number1/number2;
System.out.println(number1 + "/" + number2 + " = " + ans);
flag=false;
}else if(number2 ==0)
{
System.out.println("Error: Operation cannot be performed with second number as zero, please try again");
}
}
}
break;
case ("%"):
ans = number1 % number2;
System.out.println(number1 + " % " + number2 + " = " + ans);
break;
}
//setting loop for program to restart if user wants to
System.out.println("Again?"
+ "\n" + "enter 1 for yes"
+ "\n" + "enter 2 for no");
more = Keyboard.nextDouble();
}while (more== 1);
} }
這就是我的計劃是應該做的:
它必須:
- CLI基於 - 保持每個運營商的運行總數(多少次增加,多少次乘法......)
- 提示用戶輸入2個號碼
- 用於操作員
提示用戶 - 由零 處理除法的情況下 - 進行操作
- 顯示屏幕向用戶
對結果 - 循環播放,直到用戶使用定點值
- 顯示統計:爲每個操作計數,最小值,最大值。
我知道我仍然需要在這方面做很多工作,但現在我只需要幫助解決這個問題,所以我可以繼續工作。
非常感謝。
使用調試器... –
@Manzanita解決辦法是向用戶提供與操作員相對應的數字列表,並要求他們選擇一個數字 –
還有另一個會阻止你的錯誤'more == 1'你正在比較double與'int'使用['Double.compare()'](http://docs.oracle。com/javase/6/docs/api/java/lang/Double.html#compare%28double,%20double%29) –