-
Notifications
You must be signed in to change notification settings - Fork 7
Pr1 done #10
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?
Pr1 done #10
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,71 @@ | ||
| package school.lemon.changerequest.java.introduction.pr1; | ||
|
|
||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class ConsoleCalculator { | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| makeChoice(); | ||
| String input; | ||
| int a, b; | ||
| while (true) { | ||
| while (!scanner.hasNext("(exit|help|add|sub|mul|div)")) { | ||
| System.out.println("False, please try again."); | ||
| makeChoice(); | ||
| scanner.next(); | ||
| } | ||
| input = scanner.next(); | ||
|
|
||
| if (input.equals("exit")) { | ||
| System.out.println("Bye-bye"); | ||
| scanner.close(); | ||
| break; | ||
| } | ||
| if (input.equals("help")) { | ||
| makeChoice(); | ||
| scanner.next(); | ||
| } | ||
|
|
||
| System.out.println("Enter first number:"); | ||
| a = scanner.nextInt(); | ||
| System.out.println("Enter second number:"); | ||
| b = scanner.nextInt(); | ||
|
|
||
| switch (input) { | ||
| case "add": | ||
| int sum = a + b; | ||
| System.out.println(a + "+" + b + "= " + sum); | ||
| break; | ||
| case "sub": | ||
| int sub = a - b; | ||
| System.out.println(a + "-" + b + "= " + sub); | ||
| break; | ||
| case "mul": | ||
| int mul = a * b; | ||
| System.out.println(a + "*" + b + "= " + mul); | ||
| break; | ||
| case "div": | ||
| int div = a / b; | ||
| System.out.println(a + "/" + b + "= " + div); | ||
| break; | ||
| } | ||
| makeChoice(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static void makeChoice () { | ||
|
Member
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. This function should be renamed, as it don't really "make any choice", it just prints your "help" or "menu" messages. |
||
| 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."); | ||
| System.out.println("Make your choice."); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,15 @@ | |
|
|
||
| public class HammingDistance { | ||
| public static int compute(String a, String a1) { | ||
| return -1; | ||
|
|
||
| if (a.length() != a1.length()) return -1; | ||
|
|
||
| int result = 0; | ||
| for (int i = 0; i < a.length(); i++) { | ||
| if (a.charAt(i) != a1.charAt(i)) { | ||
| return result++; | ||
|
Member
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. for sure, you shouldn't return result here, you should only increment it here. |
||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
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.
I personally don't like such solution, where you move some
switchclauses into separateif-elsestatements, I'd really prefer some other implementation where all possible commands would be inside oneswitch.