hello guys, this is my first post well I'm having this problem with my program.. for me its a big problem but I don't know about you guys if its just a simple one.. anyway here's my program..
===========================================================
What I want is the output should be like this:
Sample run:
Enter a number: 10
Enter an arithmetic operator: +
Enter a number: 5
Running result = 15
Enter an arithmetic operator: *
Enter a number: 2
Running result = 30
Enter an arithmetic operator: -
Enter a number: 10
Running result = 20
Enter an arithmetic operator: *
Enter a number: 3
Running result = 60
Enter an arithmetic operator: /
Enter a number: 5
Running result = 12
Enter an arithmetic operator: +
Enter a number: 88
Running result = 100
Enter an arithmetic operator: =
Final result = 100
========================
I appreciate for the help..
Code:
/**
* @Sta. Maria, Jerome Augustus S.
* @version 1.00 2011/11/15
*/
import java.util.*;
public class Review {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Introduction();
keyboard.nextLine();
System.out.println("\n");
double operand1, operand2, runningResult=0;
char operator='x';
System.out.print("Enter a number: ");
operand1 = Double.parseDouble(keyboard.nextLine());
boolean firstOperator = true;
while(operator != '=') {
operator = readOperator();
if(firstOperator && operator == '=') {
System.out.println("cannot be the first operator");
operator = 'x';
continue;
}
firstOperator = false;
switch(operator) {
case '*':
System.out.print("Enter a number: ");
operand2 = Double.parseDouble(keyboard.nextLine());
runningResult = multiply(operand1, operand2);
System.out.println("Running Result: " + runningResult);
continue;
case '/':
System.out.print("Enter a number: ");
operand2 = Double.parseDouble(keyboard.nextLine());
runningResult = divide(operand1, operand2);
System.out.println("Running Result: " + runningResult);
continue;
case '+':
System.out.print("Enter a number: ");
operand2 = Double.parseDouble(keyboard.nextLine());
runningResult = addition(operand1, operand2);
System.out.println("Running Result: " + runningResult);
continue;
case '-':
System.out.print("Enter a number: ");
operand2 = Double.parseDouble(keyboard.nextLine());
runningResult = subtraction(operand1, operand2);
System.out.println("Running Result: " + runningResult);
continue;
case '=':
System.out.println("Final Result: " + runningResult);
break;
}
}
}
public static double multiply(double n1, double n2) {
return n1*n2;
}
public static double divide(double n1, double n2) {
return n1/n2;
}
public static double addition(double n1, double n2) {
return n1+n2;
}
public static double subtraction(double n1, double n2) {
return n1-n2;
}
public static char readOperator() {
Scanner keyboard = new Scanner(System.in);
char operator = 'x';
do {
System.out.print("Enter an arithmetic operator: ");
operator = keyboard.nextLine().charAt(0);
if(operator != '+' && operator != '-'&& operator != '*' && operator != '/'&& operator != '=') {
System.out.println("You must enter +, -, *, / or =");
}
}while(operator != '+' &&operator != '-'&& operator != '*' && operator != '/' && operator != '=');
return operator;
}
public static void Introduction(){
System.out.println("\n\n\n");
System.out.println("==============================================");
System.out.println("College of Information and Computing Sciences ");
System.out.println(" Saint Louis University ");
System.out.println(" Baguio City ");
System.out.println("----------------------------------------------");
System.out.println(" Programmer ");
System.out.println(" Jerome Augustus S. Sta. Maria ");
System.out.println(" 11/15/11 ");
System.out.println("==============================================");
System.out.println("\n");
System.out.print("Press enter to continue...");
}
}
===========================================================
What I want is the output should be like this:
Sample run:
Enter a number: 10
Enter an arithmetic operator: +
Enter a number: 5
Running result = 15
Enter an arithmetic operator: *
Enter a number: 2
Running result = 30
Enter an arithmetic operator: -
Enter a number: 10
Running result = 20
Enter an arithmetic operator: *
Enter a number: 3
Running result = 60
Enter an arithmetic operator: /
Enter a number: 5
Running result = 12
Enter an arithmetic operator: +
Enter a number: 88
Running result = 100
Enter an arithmetic operator: =
Final result = 100
========================
I appreciate for the help..