Note that there are some explanatory texts on larger screens.

plurals
  1. PObasic calculator. How to allow user to make multiple choices. code works already
    primarykey
    data
    text
    <p>The code works and was pretty easy. I made another using a switch but teacher wants if statements. So the user enters 2 numbers then makes a choice after from the println menu I created. I need the user to be able to make a second choice. Such as choose "a" for addition then also being able to choose "b" subtraction afterwards. It would be nice to be able to make the menu reproduce after every choice was made also. I need the first user input numbers to be persistent.</p> <p>thanks. edit: this is final version that works</p> <pre><code>import java.util.Scanner; public class Assign3 { public static void main (String [] args) { double numOne; double numTwo; String answerTo = null; System.out.println("============================"); System.out.println("| MENU Calculator Fun |"); System.out.println("============================"); System.out.println("| Options: |"); System.out.println("| a. Add |"); System.out.println("| b. Substract |"); System.out.println("| c. Multiply |"); System.out.println("| d. Divide |"); System.out.println("| e. Mod |"); System.out.println("| f. Compare |"); System.out.println("| g. Print Numbers |"); System.out.println("| h. Quit |"); System.out.println("============================"); Scanner numFinder = new Scanner(System.in); System.out.println("Please enter a number then press enter then enter another number followed by enter."); numOne = numFinder.nextDouble(); numTwo = numFinder.nextDouble(); while(true) { Scanner scan = new Scanner(System.in); System.out.println("Please choose an option."); String menuSelect = scan.next(); if(menuSelect.length() &gt; 1) { System.out.println("Error, too many letters..."); } else if(menuSelect.equals("a")) { answerTo = Double.toString(numOne +numTwo); } else if(menuSelect.equals("b")) { answerTo = Double.toString(numOne - numTwo); } else if(menuSelect.equals("c")) { answerTo = Double.toString(numOne * numTwo); } else if(menuSelect.equals("d")) { answerTo = Double.toString(numOne / numTwo); } else if(menuSelect.equals("e")) { answerTo = Double.toString(numOne % numTwo); } else if (menuSelect.equals("f")) if(numOne &gt; numTwo) { answerTo = "&gt;";//System.out.println("First is grater"); } else if(numOne &lt; numTwo) { answerTo ="&lt;";//System.out.println("Second is grater"); } else { answerTo ="=";//System.out.println("Both are equal"); } else if(menuSelect.equals("g")) { answerTo = "First number: " + numOne + " Second number: " + numTwo; } else if(menuSelect.equals("h")) { break; } System.out.println("answer: " + answerTo); } } } </code></pre>
    singulars
    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