Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. package breaking_it_down;
  2. import java.util.Scanner;
  3. public class TooMuchInMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int operand1, operand2, answer; char operator='q'; boolean keepCalculating = true;
  4. while (keepCalculating) { //************************************************* // This next block will get the user desired operator // getOperator should contain the following logic //************************************************* boolean operator_is_good=false;
  5. do {
  6. System.out.print("Enter an Operator: +  - *  /  q for
  7. quit: ");
  8. String strOperator = scan.nextLine(); strOperator = strOperator.trim(); if (strOperator.length() == 0) continue; // Need to try this again with no input operator = strOperator.charAt(0); operator_is_good=false;
  9. switch (operator) { case 'q': case '+': case '-': case '*': case '/': operator_is_good = true; break; default: System.out.println("Your operator is bad ... try
  10. again:");
  11. break;
  12. } } while (!operator_is_good);
  13. //*********************************************** // At this point operator contains a proper operator chosen
  14. by the user
  15. // This will show up in doCalculation //*********************************************** if (operator == 'q') { keepCalculating=false; // not really needed because we are doing a break... but clarifies what will happen break; }
  16. //*********************************************** // Now get operand 1 // getOperand() will get the next hunk of logic //*********************************************** int which =1; System.out.println("Enter operand "+which); String input;
  17. boolean operand_is_bad;
  18. do {
  19. operand_is_bad=false;
  20. input = scan.nextLine(); input = input.trim(); if (input.length() == 0) operand_is_bad=true; for (int i=0; i < input.length(); i++) { char c = input.charAt(i); if (c < '0' || c > '9') { // Oops, bad digit operand_is_bad=true; System.out.println("Your last input was bad, try
  21. again");
  22. }
  23. } } while (operand_is_bad);
  24. // The following statement will be covered later in the
  25. course.  
  26. // For now, just know that it converts a String to an integer // Note that Integer.parseInt is picky and blows up with any
  27. bad characters
  28. operand1 =  Integer.parseInt(input);
  29. //*********************************************** // Now get operand 2 // Note this code is mostly the same as the above code.  // You can just reuse the same getOperand method again //*********************************************** which =2;
  30. System.out.println("Enter operand "+which); do { operand_is_bad=false;
  31. input = scan.nextLine(); input = input.trim(); if (input.length() == 0) operand_is_bad=true; for (int i=0; i < input.length(); i++) { char c = input.charAt(i); if (c < '0' || c > '9') { // Oops, bad digit operand_is_bad=true; System.out.println("Your last input was bad,
  32. try again");
  33. }
  34. } } while (operand_is_bad);
  35. // The following statement will be covered later in the
  36. course.  
  37. // For now, just know that it converts a String to an integer // Note that Integer.parseInt is picky and blows up with any
  38. bad characters
  39. operand2 =  Integer.parseInt(input);
  40. //*********************************************** // Now we have operator, operand1, and operand2. // Time to calculate the answer // doArithmetic will take the following logic //*********************************************** switch(operator) { case '+': answer = operand1 + operand2; break; case '-': answer = operand1 - operand2; break; case '*': answer = operand1 * operand2; break; case '/': answer = operand1 / operand2; break; default: System.out.println("We shouldn't get here in doArithmentic!!!!"); answer = -1; break; }
  41. //***********************************************
  42. // Time to format the answer // the format routine will get the following statement //*********************************************** System.out.println(operand1 + " "+ operator + " "+ operand2 + " = "+ answer);
  43. System.out.println("======================="); } // end of while (continue_calculating)
  44. System.out.println("Finished Calculations");
  45. } // end of main
  46. } // end of class TooMuchInMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement