除了askCalcChoice1之外,这个计算器一切正常。由于askCalcChoice1是一个字符串,我称之为错(显然)。错误说它不能将字符串转换为int,以及将int转换为boolean。但是,当我将inputOperation作为字符串时,它会破坏askCalcChoice1下面的其他2个调用。 (它会破坏displayRedults和askTwoValues,因为它们不是字符串)。我不知道如何格式化askCalcChoice以便调用这个在另一个类中编写的方法而不会破坏任何东西。 askCalcChoice被写成一个字符串,我粘贴在oopCalculator代码下面。有没有办法,有人可以告诉我如何在oopCalculator中编写该部分代码吗?
int inputOperation; // user to choose the function
askCalcChoice1 myAskCalcChoice1 = new askCalcChoice1();
//menu becomes a complete string below
String menu = "Welcome to Hilda Wu's Calculator\t\t"
+ "\n1. Addition\n"
+ "2. Subtraction\n"
+ "3. Multiplication\n"
+ "4. Division\n"
+ "5. Exit\n\n";
calculatorCommands.pickNewSymbol(menu); //complete menu will be picked up as a string and display
calculatorCommands.putDownSymbol();
while (inputOperation = myAskCalcChoice1.calcChoice()) { //this will call for myAskCalcChoice1 class
calculatorCommands.pickNewSymbol("\n"); //pick up the class
calculatorCommands.putDownSymbol(); //display the class
askTwoValues myAskTwoValues = new askTwoValues();
float[] myFloats = myAskTwoValues.inputFloats(inputOperation);
displayResults myDisplayResults = new displayResults();
float result = myDisplayResults.showResults(inputOperation, myFloats);
String strFormat = "The answer is: " + result + "\n\n"; //print out The answer is as a string
calculatorCommands.pickNewSymbol(strFormat); //pick up string from above
calculatorCommands.putDownSymbol(); //display string
calculatorCommands.pickNewSymbol(menu); // pick up menu from the beginning of code, loop to calculator menu
calculatorCommands.putDownSymbol(); //display menu as loop
}
calculatorCommands.pickNewSymbol("\nThank you for using Hilda Wu's Calculator\n"); //when user choose to exit calculator
calculatorCommands.putDownSymbol();
}
String calcChoice() {
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 entered an invalid choice, 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;
}
}
答案 0 :(得分:0)
首先,您使用两个参数调用showResults: int选择 和 float [] f
从不使用选择。 您在交换机中使用输入变量,但默认情况下返回显示选项的错误。
最好将选择作为参数传递给函数,并确保它是char而不是其他类型。
这也不是一个好的陈述问题的形式。我不会评价它,但请重新制作它,以便正确显示整个代码。我无法轻易理解它。我可能已经误解了它。请不要在它们之间添加注释,请确保您有正确的缩进,并且您已获得所有代码。 如果您需要发表评论,请在之后进行评论。它不是很复杂,只需告诉我们代码,然后再问一下是什么问题;)
如果选择是为了传递开关...那么就这样做,但不是作为int而是作为char。