通过错误基本上我试图建立一个计算器,将允许用户选择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