通過錯誤基本上我試圖建立一個計算器,將允許用戶選擇1-5
乘法,減法,乘法或除法的選項。無論他們選擇什麼,我都會要求他們在選擇他們想要的功能後輸入2
數字。我試圖把這一點放入下面的計算位,但我在這裏遇到這個錯誤,不知道它應該如何格式化。在這部分代碼中,我收到錯誤。如何編碼它獲得方法
inputOperation = mySelection.selectionOne().equals("5"); //this is where I a getting the error "cannot convert boolean to int"
我試圖在下面的代碼中插入的部分是(我認爲):
public class Selection {
Scanner readInput = new Scanner(System.in);
String selectionOne() {
String input;
do { //do loop will continue to run until user enters correct response
System.out.print("Please enter a number between 1 and 5, A for Addition, S for Subtraction, M for Multiplication, or D for Division, or X for Exit: ");
try {
input = readInput.nextLine(); //user will enter a response
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D") || input.equals("X")) {
System.out.println("Thank you");
break; //user entered a character of A, S, M, or D
} else if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5) {
System.out.println("Thank you");
break; //user entered a number between 1 and 5
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
continue;
}
catch (final NumberFormatException e) {
System.out.println("You have entered an invalid choice. Try again.");
continue; // loop will continue until correct answer is found
}
} while (true);
return input;
}
。
public class answers {
public float answer(int choice, float [] f){
float result = 0.00f;
switch (choice){
case 1: result = f[0]+f[1]; break;
case 2: result = f[0]-f[1]; break;
case 3: result = f[0]*f[1]; break;
case 4: result = f[0]/f[1]; break;
case 5: System.out.println("You are now exting the program \n"); break;
default: System.out.println("Cannot calculate.\n You entered" + choice); break;
}
return result;
}
public static void main (String args[]){
int inputOperation;
Selection mySelection = new Selection();
inputOperation = mySelection.selectionOne().equals("5"); //this is where I a getting the error "cannot convert boolean to int"
Symbol.newSymbol("You entered a choice of " + inputOperation + " successfully\n\n");
Symbol.displaySymbol();
float [] myFloats = mySelection.pickNumbers(inputOperation);
Symbol.newSymbol("You entered " + myFloats[0] + " and " + myFloats[1] + " successfully\n\n") ;
Symbol.displaySymbol();
}
}
布爾型\t equals(Object anObject) 將此字符串與指定對象進行比較。 – 2014-11-04 05:40:20
equals方法的結果是布爾值 – Taras 2014-11-04 05:40:44