Its an Basic Calculator I worked on, and now I wanna know if I can make something better like not so "much" Code or I made somthing to complicated? Have a nice day! Keep safe!
import java.util.Scanner;
public class learningCode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Bitte geben sie einen Rechenoperator für ihre Rechnung ein! (+, -, *, /)");
String numberTest = scan.nextLine();
if (numberTest.equals("+")) {
String questionOne = "Bitte gebe deine erste Zahl ein: ";
System.out.println(questionOne);
double number1 = scan.nextDouble();
String questionTwo = "Bitte gebe deine zweite Zahl ein: ";
System.out.println(questionTwo);
double number2 = scan.nextDouble();
double finalResult = number1 + number2;
String Answer = "Dein Ergebnis ist: ";
System.out.println(Answer + finalResult );
scan.close();
}
else {
if (!"+".equals(numberTest) && !"-".equals(numberTest) && !"*".equals(numberTest) && !"/".equals(numberTest)) {
System.out.println("Das ist kein Rechenoperator. Bitte versuche es erneut!");
}
if (numberTest.equals("*")) {
String questionTwo = "Bitte gebe deine erste Zahl ein: ";
System.out.println(questionTwo);
double number1 = scan.nextDouble();
String questionThree = "Bitte gebe deine zweite Zahl ein: ";
System.out.println(questionThree);
double number2 = scan.nextDouble();
double finalResult = number1 * number2;
String Answer = "Dein Ergebnis ist: ";
System.out.println(Answer + finalResult );
scan.close();
}
if (numberTest.equals("/")) {
String questionThree = "Bitte gebe deine erste Zahl ein: ";
System.out.println(questionThree);
double number1 = scan.nextDouble();
String questionFour = "Bitte gebe deine zweite Zahl ein: ";
System.out.println(questionFour);
double number2 = scan.nextDouble();
double finalResult = number1 / number2;
String Answer = "Dein Ergebnis ist: ";
System.out.println(Answer + finalResult );
scan.close();
}
if (numberTest.equals("-")) {
String questionFive = "Bitte gebe deine erste Zahl ein: ";
System.out.println(questionFive);
double number1 = scan.nextDouble();
String questionSix = "Bitte gebe deine zweite Zahl ein: ";
System.out.println(questionSix);
double number2 = scan.nextDouble();
double finalResult = number1 - number2;
String Answer = "Dein Ergebnis ist: ";
System.out.println(Answer + finalResult );
scan.close();
}
}
}
}
-
1\$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$Mast– Mast ♦2021年05月29日 09:42:11 +00:00Commented May 29, 2021 at 9:42
2 Answers 2
Naming
Adhere to the Java Naming Conventions.
Names of methods and variables start with a lowercase letter, names of classes with an uppercase letter.
Method names should start with a verb so that they read like an instruction. Names of variables should start with a noun.
Exception to that are variables holding or methods returning a boolean which both should start with is, can, has or alike.
Don't surprise your readers
You have a variable named numberTest
but it does not contain a test for a number but the current operation or the users choice.
Code Quality
Structure your code with methods.
You wrote a single long method.
This method contains an if/else
cascade that somehow separates the method in to different sections.
You should extract this blocks in to separate Methods (leaving the if/else
instructions in main
) so that the main
method becomes shorter and easier to read (and to maintain).
Don't repeat yourself
All over your code you repeat this two lines with varying variable names:
System.out.println(questionSix);
double number2 = scan.nextDouble();
This two lines could be extracted to a separate method that takes the String
as parameter and returns the double
value. That would shrink four lines in your code to just one.
since in all if statement you are asking the user to enter the left and right operand aside with receiving (scanning) the input nothing is different but the operand only. the better approach could be to take all those similar statements out of each IF statement and put them at the beginning of the program. then switch the character and perform the arithmetic according to its case.
public static void main(String[] arg){
Scanner scan = new Scanner(System.in);
System.out.println("Bitte gebe deine erste Zahl ein: ");
double number1 = scan.nextDouble();
System.out.println("Bitte geben sie einen Rechenoperator für ihre Rechnung ein! (+, -, *, /)");
String operator = scan.next();
System.out.println("Bitte gebe deine zweite Zahl ein: ");
double number2 = scan.nextDouble();
double finalResult=0.0;
switch(operator){
case "+":
finalResult= number1 + number2;
break;
case "-":
finalResult= number1 - number2;
break;
case "/":
finalResult= number1 / number2;
break;
case "*":
finalResult= number1 * number2;
break;
default:
System.out.println("invalid operator");
}
String Answer = "Dein Ergebnis ist: ";
System.out.println(Answer + finalResult );
scan.close();
}
hope this helps.