fork download
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. int num1 = 0, num2 = 0, total = 0, option = 0, ex; // Creates integer variables
  7. do {
  8. Scanner sc = new Scanner(System.in);
  9. System.out.println("\tBasic Math Calculator");// Title
  10. System.out.println("\t---------------------");
  11. System.out.println("\tEnter your choice from the following menu:");
  12. System.out.println("\t------------------------------------------");
  13. System.out.println("1.\tAddition");// All the menu options
  14. System.out.println("2.\tSubtraction");
  15. System.out.println("3.\tMultiplication");
  16. System.out.println("4.\tDivision");
  17. System.out.println("5.\tGenerate Random number");
  18. System.out.println("6.\tQuit");
  19.  
  20. boolean valid;
  21. do {
  22. valid = true;
  23. try {
  24. option = Integer.parseInt(sc.nextLine());// Stores the users answers
  25. if (option < 1 || option > 6) {
  26. System.out.println("Invalid input. Please try again.");
  27. valid = false;
  28. }
  29. } catch (NumberFormatException e) {
  30. System.out.println("Invalid input. Please try again.");
  31. valid = false;
  32. }
  33. } while (!valid);
  34.  
  35. switch (option) {// The math and titles for every option
  36. case 1:
  37. System.out.println("You chose to add two numbers: ");
  38. System.out.println("Enter your first number:");
  39. num1 = sc.nextInt();
  40. System.out.println("Enter your second number:");
  41. num2 = sc.nextInt();
  42. total = num1 + num2;
  43. System.out.println("The two numbers you chose added together is " + total);
  44. break;
  45. case 2:
  46. System.out.println("You chose to subtract two numbers: ");
  47. System.out.println("Enter your first number:");
  48. num1 = sc.nextInt();
  49. System.out.println("Enter your second number:");
  50. num2 = sc.nextInt();
  51. total = num1 - num2;
  52. System.out.println("The two numbers you chose subtracted together is " + total);
  53. break;
  54. case 3:
  55. System.out.println("You chose to multiply two numbers: ");
  56. System.out.println("Enter your first number:");
  57. num1 = sc.nextInt();
  58. System.out.println("Enter your second number:");
  59. num2 = sc.nextInt();
  60. total = num1 * num2;
  61. System.out.println("The two numbers you chose multiplied together is " + total);
  62. break;
  63. case 4:
  64. System.out.println("You chose to divide two numbers: ");
  65. System.out.println("Enter your first number:");
  66. num1 = sc.nextInt();
  67. System.out.println("Enter your second number:");
  68. num2 = sc.nextInt();
  69. total = num1 / num2;
  70. if (num2 == 0) {
  71. System.out.println("You can't divide by 0");
  72. } else {
  73. System.out.println("The two numbers you chose divided together is " + total + "with a quotient of "
  74. + (num1 % num2));
  75. }
  76. break;
  77. case 5:
  78. System.out.println("You chose to get two random numbers: ");
  79. System.out.println("Enter your lower limit:");
  80. num1 = sc.nextInt();
  81. System.out.println("Enter your upper limit:");
  82. num2 = sc.nextInt();
  83. total = num1 + num2;
  84. Random rand = new Random();
  85. int rand_int1 = rand.nextInt(num1 + num2);
  86. System.out.println("The random intigers is: " + rand_int1);
  87. break;
  88. case 6:// If the user wants to quit
  89. ex = 2;
  90. break;
  91. default:// Tells their option was incorrect
  92. System.out.println("Invalid choice, choice " + option + " was not an option");
  93.  
  94. }
  95. System.out.println("Do you want to continue?1.Yes 2.No");// Asks the user if they want to proceed
  96. ex = sc.nextInt(); // A thank you message for the user for running the program
  97. } while (ex == 1);
  98. System.out.println("-----------------------------------------");
  99. System.out.println("Thank you for using the basic calculator!");
  100. }
  101. }
Runtime error #stdin #stdout #stderr 0.14s 35464KB
stdin
Standard input is empty
stdout
	Basic Math Calculator
	---------------------
	Enter your choice from the following menu:
	------------------------------------------
1.	Addition
2.	Subtraction
3.	Multiplication
4.	Division
5.	Generate Random number
6.	Quit
stderr
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
	at Main.main(Main.java:24)