0

I decided to try to make a very simple calculator in Java. I copied someone else's code and I'm trying to add my own ideas to it.

Now the question is, how do I get it to re-ask the question and clear the previous answers?

Here's my code:

package calculator;
import java.util.*;

public class Calculator {

public static void main(String[] args) {
   double num1;
   double num2;
   String operation;

   boolean run = true;

   while(run) {

   Scanner input = new Scanner(System.in);

   System.out.println("Please enter the first number");
   num1 = input.nextInt();

   System.out.println("Please enter the second number");
   num2 = input.nextInt();

   Scanner op = new Scanner(System.in);

   System.out.println("Please enter an operation");
   operation = op.next();

   if (operation.equals("+"))
   {
       System.out.println("Your answer is " + (num1 + num2));
   }
   if (operation.equals("-"))
   {
       System.out.println("Your answer is " + (num1 - num2));
   }
   if (operation.equals("*"))
   {
       System.out.println("Your answer is " + (num1 * num2));
   }
   if (operation.equals("/"))
   {
       System.out.println("Your answer is " + (num1 / num2));
   }

   }
}
}
braX
  • 11,506
  • 5
  • 20
  • 33
A. Lee
  • 29
  • 2
  • 6
  • 4
    Possible duplicate of [Java: Clear the console](http://stackoverflow.com/questions/2979383/java-clear-the-console) and [How to delete stuff printed to console by System.out.println()?](http://stackoverflow.com/questions/7522022/how-to-delete-stuff-printed-to-console-by-system-out-println) – Vince Jul 15 '16 at 23:47
  • what question you want to reask? – Shubham Sharma Jul 15 '16 at 23:52
  • It's not a duplicate because this question is asked by a beginner. So one should hint to the fact that clearing the console isn't supported (but not impossible). I would suggest that a beginner should check out some UI library which can be updated which would probably achieve what the user has in mind. – giro Jul 15 '16 at 23:55
  • 1
    @A.Lee except clearing the console your question "is asked again" - caused by the while loop. Do you want some UI code (e.g. Swing) because clearing the screen is mandatory? If you need to use the console follow the link in the comment of VinceEmigh - but if you don't yet understand basic programming concepts I would not recommend to follow any of the mentioned paths. – giro Jul 15 '16 at 23:59
  • This is a duplicate, but beware that there is no single answer. It depends on the terminal emulator and the operating system, and all the similar questions on SO have answers that do not work, even hghly voted and even accepted answers. The only guaranteed way to end up with an empty screen is to write a bunch of newlines to scroll previous content off the top. All other methods will fail for some combination of OS and terminal type. And, of course, a bunch of newlines will be very frustrating for anybody using a real printing terminal :-) – Jim Garrison Jul 16 '16 at 01:16

0 Answers0