-
Notifications
You must be signed in to change notification settings - Fork 7
1,2 Tasks from PR1 done #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,98 @@ | ||
| package school.lemon.changerequest.java.introduction.pr1; | ||
|
|
||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class ConsoleCalculator { | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Console calculator:"); | ||
| System.out.println("Enter 'add' to perform addition."); | ||
| System.out.println("Enter 'sub' to perform subtraction."); | ||
| System.out.println("Enter 'mul' to perform multiplication."); | ||
| System.out.println("Enter 'div' to perform division."); | ||
| System.out.println("Enter 'exit' to exit."); | ||
| System.out.println("Enter 'help' to see help message."); | ||
| boolean start = true; | ||
| while (start) { | ||
| System.out.println("Make your choice."); | ||
| Scanner sc = new Scanner(System.in); | ||
| String choice = sc.nextLine(); | ||
| choice.toLowerCase(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not working properly because string is immutable. Please read documentation about string methods. |
||
| switch (choice) { | ||
| case "add": | ||
| add(); | ||
| break; | ||
| case "sub": | ||
| sub(); | ||
| break; | ||
| case "mul": | ||
| mul(); | ||
| break; | ||
| case "div": | ||
| div(); | ||
| break; | ||
| case "exit": | ||
| exit(); | ||
| break; | ||
| case "help": | ||
| help(); | ||
| break; | ||
| default: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer print help message by default. |
||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| private static void help() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about help message? |
||
| } | ||
|
|
||
| private static void exit() { | ||
| System.out.println("Bye-bye"); | ||
| System.exit(0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bad practice to use |
||
| } | ||
|
|
||
| private static void div() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are too many duplicated code... Reading two integers for example. Maybe you can get rid of it? |
||
| Scanner sc = new Scanner(System.in); | ||
| int a, b, result; | ||
| System.out.println("Enter first number:"); | ||
| a = sc.nextInt(); | ||
| System.out.println("Enter second number:"); | ||
| b = sc.nextInt(); | ||
| result = a / b; | ||
| System.out.println("Result of " + a + "/" + b + " is " + result); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
| } | ||
|
|
||
| private static void mul() { | ||
| Scanner sc = new Scanner(System.in); | ||
| int a, b, result; | ||
| System.out.println("Enter first number:"); | ||
| a = sc.nextInt(); | ||
| System.out.println("Enter second number:"); | ||
| b = sc.nextInt(); | ||
| result = a * b; | ||
| System.out.println("Result of " + a + "*" + b + " is " + result); | ||
| } | ||
|
|
||
| private static void sub() { | ||
| Scanner sc = new Scanner(System.in); | ||
| int a, b, result; | ||
| System.out.println("Enter first number:"); | ||
| a = sc.nextInt(); | ||
| System.out.println("Enter second number:"); | ||
| b = sc.nextInt(); | ||
| result = a - b; | ||
| System.out.println("Result of " + a + "-" + b + " is " + result); | ||
| } | ||
|
|
||
| private static void add() { | ||
| Scanner sc = new Scanner(System.in); | ||
| int a, b; | ||
| System.out.println("Enter first number:"); | ||
| a = sc.nextInt(); | ||
| System.out.println("Enter second number:"); | ||
| b = sc.nextInt(); | ||
| System.out.println("Result of " + a + "+" + b + " is " + a + b); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating scanner in each iteration is not a good idea.