gpt4 book ai didi

Java 计算器(我的第一个项目需要帮助)

转载 作者:行者123 更新时间:2023-12-02 10:49:41 27 4
gpt4 key购买 nike

这是我第一次尝试 Java。该项目是一个计算器,它需要一个数字、一个运算符(operator)信号(+、-、*、/)和另一个数字来创建方程并给出其最终值,然后询问用户是否要重新启动程序另一个方程或不是。我想知道是否可以做些什么来改进我的代码。谢谢!

/*
Name: Felipe de Araujo
Project #: 1
*/

import java.util.Scanner; //Importing API.

public class Calculator {
public static void main(String[] args){
int a = 1; //Creating variable for the first while loop.
System.out.println("Welcome to the calculator!"); //Program introduction.

while (a == 1){
Scanner input = new Scanner(System.in); //Creating the input.

double firstNumber, secondNumber, result; char operator; //Creating all variables.

//Program asking for the first number.
System.out.print("First Number: ");
firstNumber = input.nextDouble();

//Program asking for the operator
System.out.print(firstNumber + " (+,-,*,/): ");
operator = input.next(".").charAt(0);

int repeat = 1; //Creating variable for the next while loop.

//Evaluating the operator to determine if it is inside the criteria (+,-,*,/).
if (operator == '+' || operator == '-' || operator == '*' || operator == '/'){
System.out.print(firstNumber + " " + operator + " Second Number: "); //If the operator is inside the criteria than start
//asking for the second number.
}
else { //If the operator is not inside the criteria run the loop until the user type something that is.
while (repeat == 1){
System.out.print(operator + " not recognized, please select between (+,-,*,/): ");
operator = input.next(".").charAt(0);
if (operator == '+' || operator == '-' || operator == '*' || operator == '/') {
System.out.print(firstNumber + " " + operator + " Second Number: ");

repeat++;
}
}
}

secondNumber = input.nextDouble(); //Initialize the secondNumber variable to the number typed.

//Equalling the variable result to the return given by the method calculatorMethod, with the variables given.
result = calculatorMethod(firstNumber, secondNumber, operator);
System.out.println(firstNumber + " " + operator + " " + secondNumber + " = " + result);//Printing the equation.
System.out.println(" ");

// Asking the user to continue the program for another operation.
char out;//Creating the variable out.
System.out.print("[y/Y] - continue | [n/N] or any other to end program: ");
out = input.next(".").charAt(0);//Initializing the variable out to what was typed.

//Verifying if the user wants to continue the program.
if (out == 'y' || out == 'Y'){
System.out.println(" ");
}
else {//If the users type anything besides y/Y the program will exit the main loop, ending the program.
System.out.println("Bye!");
a++;
}
}
}

//Declaring the calculatorMethod and all its behaviors.
private static double calculatorMethod(double a, double b, char c){

if (c == '+'){
return a + b;
}
else if (c == '-'){
return a - b;
}
else if (c == '*'){
return a * b;
}
else {
return a / b;
}
}

}

最佳答案

您好,欢迎来到 Java 世界:)。一些提示:

  1. 尝试为变量提供清晰的名称。 “a”、“b”、“c”可能很难理解。
  2. 优先使用简短的方法来提高代码的可读性。例如,您可以创建另一种方法,该方法返回一个对象:分隔符 + 两个数字以及另一个打印结果的对象。
  3. 您可以在 calculatorMethod 方法中使用 switch(variable)。例如:

    switch (c) { // you have to change the name of all the variables 'c', 'a' and 'b'
    case '+':
    return a + b;
    case '-':
    return a - b;
    case '*':
    return a * b;
    default:
    return a / b;
    }

    }

  4. 您可以使用不同的运算符创建枚举或列表。

  5. 检查输入运算符时,可以使用 while() 循环并删除 if...else 循环。 while 循环的条件可以是“当运算符不是正确的运算符(因此未包含在正确的运算符列表中)时,一次又一次循环”。

  6. 扫描仪初始化Scanner input = new Scanner(System.in);应该在while()循环之外,因为您只需要初始化一次扫描仪,在本例中,您初始化 X 次(X 指循环次数)。

祝你好运:)

关于Java 计算器(我的第一个项目需要帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270842/

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