Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make this java code go back to a certain point in the code?
    primarykey
    data
    text
    <p>So as you could probably tell, it is a calculator. This is the second program I have made in Java, besides robotics code. I am pretty new to Java, so I don't know a ton about it. I want to be able to go back to the menu (lines 19-27) after it gives the answer to the question. If I can do this, I will put something like M for menu or E to exit after the answer. I want it so if they put M, it will go back to those lines of code to be able to start over again instead of restarting the program. Thanks, Matt.</p> <pre><code>public class Calculator { public static void main(String[] args) { System.out.println("Calculator"); System.out.println("----------"); System.out.println("Press 1 for addition"); System.out.println("Press 2 for subtraction"); System.out.println("Press 3 for multiplication"); System.out.println("Press 4 for division"); int Menu; Scanner menu_scanner = new Scanner(System.in); Menu = menu_scanner.nextInt(); if (Menu == 1){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("Please enter the first number in the problem"); System.out.println("--------------------------------------------"); double iAddition1; double iAddition2; double aAddition; Scanner sAddition1 = new Scanner(System.in); iAddition1 = sAddition1.nextDouble(); System.out.println("Please enter the second number in the problem"); System.out.println("---------------------------------------------"); Scanner sAddition2 = new Scanner(System.in); iAddition2 = sAddition2.nextDouble(); aAddition = iAddition1 + iAddition2; System.out.println("The answer to " + iAddition1 + " + " + iAddition2 + " = " + aAddition); } if (Menu == 2){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("Please enter the first number in the problem"); System.out.println("--------------------------------------------"); double iSubtraction1; Scanner sSubtraction1 = new Scanner(System.in); iSubtraction1 = sSubtraction1.nextDouble(); System.out.println("Please enter the second number in the problem"); System.out.println("---------------------------------------------"); double iSubtraction2; Scanner sSubtraction2 = new Scanner(System.in); iSubtraction2 = sSubtraction2.nextDouble(); double aSubtraction = iSubtraction1 - iSubtraction2; System.out.println("The answer to " + iSubtraction1 + " - " + iSubtraction2 + " = " + aSubtraction); } if (Menu == 3){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("Please enter the first number in the problem"); System.out.println("--------------------------------------------"); double iMultiplication1; double iMultiplication2; Scanner sMultiplication1 = new Scanner(System.in); iMultiplication1 = sMultiplication1.nextDouble(); System.out.println("Please enter the second number in the problem"); System.out.println("---------------------------------------------"); Scanner sMultiplication2 = new Scanner(System.in); iMultiplication2 = sMultiplication2.nextDouble(); double aMultiplication = iMultiplication1 * iMultiplication2; System.out.println("The answer to " + iMultiplication1 + " * " + iMultiplication2 + " = " + aMultiplication); } if (Menu == 4){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("Please enter the first number in the problem"); System.out.println("--------------------------------------------"); Scanner sDivision1 = new Scanner(System.in); double iDivision1; iDivision1 = sDivision1.nextDouble(); System.out.println("Please enter the second number in the problem"); System.out.println("---------------------------------------------"); Scanner sDivision2 = new Scanner(System.in); double iDivision2; iDivision2 = sDivision1.nextDouble(); double aDivision = iDivision1 / iDivision2; System.out.println("The answer to " + iDivision1 + " / " + iDivision2 + " = " + aDivision); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload