gpt4 book ai didi

当用户输入句点时停止的 Java 计算器程序

转载 作者:行者123 更新时间:2023-11-30 01:44:52 24 4
gpt4 key购买 nike

说明:修改您的程序,以便用户能够将操作链接在一起。输入一个整数、运算符,然后输入另一个整数不应再自动结束程序。向用户显示第一个运算的结果后,允许用户输入另一个运算,然后输入另一个整数。然后,程序应使用第一个运算的结果作为运算左侧的整数,并将新输入的整数作为运算右侧的整数。显示结果后,用户应该能够键入句点 (.) 来结束程序。输入句点后,程序应打印出消息:“程序已关闭。”

需要帮助弄清楚如何在用户输入一个句点时使程序停止我尝试通过 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 语句来使该过程易于遵循。

我希望这有帮助。

关于当用户输入句点时停止的 Java 计算器程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com