Sunday, 8 September 2013

I need help making my code more efficiant and neat and other things listed below

I need help making my code more efficiant and neat and other things listed
below

I'm making a calculator in Java and i need a better way of getting user
input from the console. For example, when the calculator is launched it
will ask you what operation you want to use and for now I had to use
numbers, but i would appreciate if someone can tell me how to do it in a
better way. I also need to know how to prevent invalid responses or to
have procedures to deal with invalid responses.
By the way sorry, but I'm new to Java and so my coding is very sloppy and
I'm not really good at coding yet, so if you could also explain why you
would do what you would tell me to do. Also if you have any tips on how to
make this code more efficient and neat please let me know. Thank you in
advance!
Here is my code:
import java.util.Scanner;
public class Calculator {
public static void main(String args[]){
@SuppressWarnings("resource")
Scanner Input = new Scanner(System.in);
double fnum, snum, answer;
double Process;
double counter = 0;
while (counter <10) {
System.out.println("");
System.out.println("Type 0 for Addition (+)");
System.out.println("Type 1 for Subtraction (-)");
System.out.println("Type 2 for Multiplication (*)");
System.out.println("Type 3 for Division (/)");
Process = Input.nextDouble();
if (Process > 3 || Process < 0){
System.out.println("Error: Invalid Operation!!!");
System.out.println("Restarting The Calculator!!!");
++counter;
System.out.println(counter);
try {
Thread.sleep(4000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}else if (Process == 0){
System.out.println("Enter First Number: ");
fnum = Input.nextDouble();
System.out.println("Enter Second Number: ");
snum = Input.nextDouble();
answer = fnum + snum;
System.out.println("The Answer is ...");
System.out.print(answer);
try {
Thread.sleep(5000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}else if (Process == 1){
System.out.println("Enter First Number: ");
fnum = Input.nextDouble();
System.out.println("Enter Second Number: ");
snum = Input.nextDouble();
answer = fnum - snum;
System.out.println("The Answer is ...");
System.out.println(answer);
try {
Thread.sleep(5000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}else if (Process == 2){
System.out.println("Enter First Number: ");
fnum = Input.nextDouble();
System.out.println("Enter Second Number: ");
snum = Input.nextDouble();
answer = fnum * snum;
System.out.println("The Answer is ...");
System.out.println(answer);
try {
Thread.sleep(5000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}else if (Process == 3){
System.out.println("Enter First Number: ");
fnum = Input.nextDouble();
System.out.println("Enter Second Number: ");
snum = Input.nextDouble();
answer = fnum / snum;
System.out.println("The Answer is ...");
System.out.println(answer);
try {
Thread.sleep(5000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
}

No comments:

Post a Comment