这是我第一次尝试Java。该项目是一个计算器,它接受一个数字,一个运算符信号(+,-,*,/)和另一个数字以创建一个方程式并给出其最终值,然后询问用户是否要重新启动该程序以是否有其他方程式。我想知道我是否可以做些改进我的代码的事情。 谢谢!
/*
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;
}
}
}
答案 0 :(得分:1)
您好,欢迎来到Java世界:)。一些提示:
您可以在 calculatorMethod 方法中使用 switch(变量)。例如:
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;
}
}
您可以使用不同的运算符创建一个枚举或列表。
当检查输入运算符时,可以使用 while()循环并删除 if ... else 循环。 while循环的条件可能是“当运算符不是正确的运算符时(因此,不包含在正确的运算符列表中),一次又一次地循环”。
扫描仪初始化扫描仪输入=新的Scanner(System.in); 应该位于 while()循环之外,因为您只需初始化一次扫描仪,在这种情况下,您需要初始化X次(X表示循环数)。
祝你好运:)