[英]Java Calculator program that stops when the user enters period
说明:修改您的程序,以便用户能够将操作链接在一起。 输入 integer 运算符,然后输入另一个 integer 不应再自动结束程序。 在向用户显示第一个操作的结果后,允许用户输入另一个操作,然后输入另一个 integer。 然后程序应该使用第一个操作的结果作为操作左侧的 integer 和新输入的 integer 作为操作右侧的 integer。 结果显示后,用户应该能够键入句点 (.) 来结束程序。 输入句点后,程序应打印出消息:“程序已关闭。”
需要帮助弄清楚当用户输入一个句号时如何让程序停止我已经尝试通过 do-while示例:3+5 结果:8 +3 结果:11 -5 结果:6。程序已关闭。
请帮忙
import java.util.Scanner;
class Main {
static final String VERSION = "1.0";
public static void main(String[] args) {
public static void printCalculatorInstructions(){
System.out.println("Welcome to version "+VERSION+" of the Calculator.");
System.out.println("In order to use the calculator, do the following:");
System.out.println("1. Enter the integer that is left of the operator.");
System.out.println("2. Press enter.");
System.out.println("3. Enter an operation. Available operations are:\n\t + (addition) \n\t - (subtraction) \n\t * (multiplication) \n\t / (division) \n\t % (remainder) \n\t ^ (exponentiation)");
System.out.println("4. Press enter.");
System.out.println("5. Enter the integer to the right of the operator.");
System.out.println("6. Press enter.");
System.out.println("7. Look at the result that is printed out!");
}
printCalculatorInstructions();
// Get input for calculator
Scanner keyboard = new Scanner(System.in);
int num1 = keyboard.nextInt();
keyboard.nextLine(); //clear line break character: \n
String operator = keyboard.nextLine();
int num2 = keyboard.nextInt();
int loop = 0;
do
{
//Call the method to perform the calculation based on what operation needs to be used
if(operator.equals("+")){
addition(num1, num2);
}else if(operator.equals("-")){
subtraction(num1, num2);
}//you will need to write more else if statements as you implement each of the operators
else if (operator.equals("*")){
multiplication(num1, num2);
}
else if (operator.equals("/")){
division(num1, num2);
}
else if (operator.equals("%")){
modulardivision(num1, num2);
}
else if (operator.equals("^")){
exponentiation(num1, num2);
}
public static int addition(int a, int b){
int result = a + b;
System.out.println("Result: " + result);
return result;
result = result +
}
public static int subtraction(int a, int b){
//fill in method here
int result = a - b;
System.out.println("Result: " + result);
return result;
}
//write the next method for multiplication here.
public static int multiplication(int a, int b){
int result = a * b;
System.out.println("Result: " + result);
return result;
}
//Then write the methods for division, the % operator, and finally exponentiation.
public static int division(int a, int b){
int result = a / b;
System.out.println("Result: " + result);
return result;
}
public static int modulardivision(int a, int b){
int result = a % b;
System.out.println("Result: " + result);
return result;
}
public static double exponentiation(double a, double b){
double result = Math.pow(a,b);
System.out.println("Result: " + result);
return result;
}
if (a == .) {
int loop = 1;
}
} while (loop != 1);
}
/* retrun result;
void = nothing is being return void ! = addition*/ ```
您的代码存在严重的语法问题。
尝试这个:
import java.util.Scanner;
public class Calculator {
static final String VERSION = "1.0";
public static void printCalculatorInstructions(){
System.out.println("Welcome to version "+VERSION+" of the Calculator.");
System.out.println("In order to use the calculator, do the following:");
System.out.println("1. Enter the integer that is left of the operator.");
System.out.println("2. Press enter.");
System.out.println("3. Enter an operation. Available operations are:\n\t + (addition) \n\t - (subtraction) \n\t * (multiplication) \n\t / (division) \n\t % (remainder) \n\t ^ (exponentiation)");
System.out.println("4. Press enter.");
System.out.println("5. Enter the integer to the right of the operator.");
System.out.println("6. Press enter.");
System.out.println("7. Look at the result that is printed out!");
}
public static int addition(int a, int b){
int result = a + b;
System.out.println("Result: " + result);
return result;
}
public static int subtraction(int a, int b) {
int result = a - b;
System.out.println("Result: " + result);
return result;
}
public static int multiplication(int a, int b) {
int result = a * b;
System.out.println("Result: " + result);
return result;
}
public static int division(int a, int b) throws ArithmeticException {
int result = a / b;
System.out.println("Result: " + result);
return result;
}
public static int modulardivision(int a, int b) {
int result = a % b;
System.out.println("Result: " + result);
return result;
}
public static double exponentiation(double a, double b) {
double result = Math.pow(a, b);
System.out.println("Result: " + result);
return result;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean toStop = false;
while (toStop == false) {
printCalculatorInstructions();
int num1 = keyboard.nextInt();
keyboard.nextLine(); //clear line break character: \n
String operator = keyboard.nextLine();
int num2 = keyboard.nextInt();
switch (operator) {
case "+":
addition(num1, num2);
break;
case "-":
subtraction(num1, num2);
break;
case "*":
multiplication(num1, num2);
break;
case "/":
try {
division(num1, num2); // in case value of num2 is zero, this won't terminate the program
} catch (ArithmeticException e) {
System.out.println("Value of num2 was zero. Enter a non-zero value for num2.");
}
break;
case "%":
modulardivision(num1, num2);
break;
case "^":
exponentiation(num1, num2);
break;
default:
toStop = true; // any other operator, character or string entered here will stop the loop
break;
}
}
keyboard.close();
}
}
我做了一个 while 循环,一直运行到 boolean 条件toStop == false
变得不正确,即toStop
的值变为true
。 当用户输入不是您提到的运算符时,此值将被更改。 我没有使用if-else
语句,而是使用switch
语句来简化流程。
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.