Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package breaking_it_down;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TooMuchInMain {
  6.     public static void main(String[] args) {
  7.        
  8.         Scanner scan = new Scanner(System.in);
  9.        
  10.         int operand1, operand2, answer;
  11.         char operator='q';
  12.         boolean keepCalculating = true;
  13.        
  14.         while (keepCalculating) {
  15.             boolean operator_is_good=false;
  16.            
  17.             do
  18.             {
  19.                 System.out.print("Enter an Operator: +  -  *  /  q for quit: ");
  20.                 String strOperator = scan.nextLine();
  21.                 strOperator = strOperator.trim();
  22.                 if (strOperator.length() == 0)
  23.                     continue;
  24.                 operator = strOperator.charAt(0);
  25.                 operator_is_good=false;
  26.                
  27.                 switch (operator)
  28.                 {
  29.                     case 'q':
  30.                     case '+':
  31.                     case '-':
  32.                     case '*':
  33.                     case '/':
  34.                         operator_is_good = true;
  35.                
  36.                         break;
  37.                
  38.                     default:
  39.                         System.out.println("Your operator is bad ... try again:");
  40.                    
  41.                         break;
  42.                     }
  43.                 }
  44.            
  45.             while (!operator_is_good);
  46.            
  47.             if (operator == 'q') {
  48.                 keepCalculating=false;
  49.                 break;
  50.                 }
  51.            
  52.             int which =1;
  53.             System.out.println("Enter operand "+which);
  54.             String input;
  55.             boolean operand_is_bad;
  56.             do {
  57.                 operand_is_bad=false;
  58.                 input = scan.nextLine();
  59.                 input = input.trim();
  60.                
  61.                 if (input.length() == 0)
  62.                     operand_is_bad=true;
  63.                
  64.                 for (int i=0; i < input.length(); i++) {
  65.                     char c = input.charAt(i);
  66.                    
  67.                     if (c < '0' || c > '9')
  68.                     {
  69.                         operand_is_bad=true;
  70.                         System.out.println("Your last input was bad, try again");
  71.                         }
  72.                     }
  73.                 }
  74.            
  75.             while (operand_is_bad);
  76.            
  77.             operand1 =  Integer.parseInt(input);
  78.             which =2;
  79.            
  80.             System.out.println("Enter operand "+which);
  81.            
  82.             do {
  83.                 operand_is_bad=false;
  84.                 input = scan.nextLine();
  85.                 input = input.trim();
  86.                
  87.                 if (input.length() == 0)
  88.                     operand_is_bad=true;
  89.                 for (int i=0; i < input.length(); i++) {
  90.                     char c = input.charAt(i);
  91.                     if (c < '0' || c > '9') {
  92.                         operand_is_bad=true;
  93.                         System.out.println("Your last input was bad, try again");
  94.                         }
  95.                     }
  96.                 }
  97.            
  98.             while (operand_is_bad);
  99.            
  100.             operand2 =  Integer.parseInt(input);
  101.            
  102.             switch(operator) {
  103.            
  104.             case '+':
  105.                 answer = operand1 + operand2;
  106.                 break;
  107.             case '-':
  108.                 answer = operand1 - operand2;
  109.                 break;
  110.             case '*':
  111.                 answer = operand1 * operand2;
  112.                 break;
  113.             case '/':
  114.                 answer = operand1 / operand2;
  115.                 break;
  116.             default:
  117.                 System.out.println("We shouldn't get here in doArithmentic!!!!");
  118.                 answer = -1;
  119.                 break;
  120.                 }
  121.            
  122.             System.out.println(operand1 + " "+ operator + " "+ operand2 + " = "+ answer);
  123.             System.out.println("=======================");
  124.             }
  125.         System.out.println("Finished Calculations");
  126.         }
  127.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement