Hi, I'm very new to Java and I've run into a strange problem while I was writing a calculator class.
For some reason, anytime I run the program it prints this part twice:
System.out.println("Calculator started.");
System.out.println("Would you like to:");
System.out.println("Multiply");
System.out.println("Divide");
System.out.println("Add");
System.out.println("Subtract");
System.out.println("Random");
opr = inp.nextLine();
It's a pretty simple problem, but I'd also like some feedback on the rest of my code. Thanks!
import java.util.*; public class CalculatorAdv { public static void main(String[] args) { int fNum; int sNum; int aNum; String opr; int cont; Scanner inp = new Scanner(System.in); Random rand = new Random(); System.out.println("Hello, welcome to the calculator."); System.out.println("Would you like to proceed? (Press 1 to continue, anything else to terminate.)"); cont = inp.nextInt(); do { int cont2 = 0; System.out.println("Calculator started."); System.out.println("Would you like to:"); System.out.println("Multiply"); System.out.println("Divide"); System.out.println("Add"); System.out.println("Subtract"); System.out.println("Random"); opr = inp.nextLine(); if(opr.equalsIgnoreCase("Multiply")) { System.out.println("You've chosen multiply."); System.out.print("First number: " ); fNum = inp.nextInt(); System.out.print("Second number: " ); sNum = inp.nextInt(); aNum = mult(fNum, sNum); System.out.println(fNum + " times " + sNum + " equals: " + aNum); cont2++; } else if(opr.equalsIgnoreCase("Divide")) { System.out.println("You've chosen divide."); System.out.print("First number: " ); fNum = inp.nextInt(); System.out.print("Second number: "); sNum = inp.nextInt(); aNum = div(fNum, sNum); System.out.println(fNum + " divided by " + sNum + " equals: " + aNum); cont2++; } else if(opr.equalsIgnoreCase("Add")) { System.out.println("You've chosen add."); System.out.print("First number: "); fNum = inp.nextInt(); System.out.print("Second number: "); sNum = inp.nextInt(); aNum = add(fNum, sNum); System.out.println(fNum + " plus " + sNum + " equals: " + aNum); cont2++; } else if(opr.equalsIgnoreCase("Subtract")) { System.out.println("You've chosen subtract."); System.out.print("First number: "); fNum = inp.nextInt(); System.out.print("Second number: "); sNum = inp.nextInt(); aNum = sub(fNum, sNum); System.out.println(fNum + " minus " + sNum + " equals: " + aNum); cont2++; } else if(opr.equalsIgnoreCase("Random")) { System.out.println("You've chosen random."); int rndchoice = rand.nextInt(); System.out.println("Your random number is: " + rndchoice); cont2++; } while(cont2 == 1) { System.out.println("Would you like to perform another calculation?"); System.out.println("1 for yes, anything else to terminate."); cont = inp.nextInt(); cont2--; } }while(cont == 1); System.out.println("System terminated."); } public static int mult(int x, int y) { return x * y; } public static int add(int x, int y) { return x + y; } public static int div(int x, int y) { return x / y; } public static int sub(int x, int y) { return x - y; } }