I'm programming a very basic menu calculator that
displays a menu
gets the users choice and performs an action based on that choice
display the results
loop back to the menu until the user enters 10 to exit the loop
What has confused me is using a switch statement in a while loop. Entering 10 is supposed to exit the program and it does, but only at first. The second time it shows the menu and asks for input, 10 does not exit the program.
any other advice is also very welcomed
public class MenuCalculator
{
public static void main(String[] args)
{
showMenu();
}
public static void showMenu()
{
Scanner input = new Scanner(System.in);
//menu display
System.out.println("Calculator Menu");
System.out.println("---------------");
System.out.println("1. Add two integers. ");
System.out.println("2. Add two doubles. ");
System.out.println("3. Subtract two integers. ");
System.out.println("4. Subtract two doubles. ");
System.out.println("5. Multiply two integers. ");
System.out.println("6. Multiply two doubles. ");
System.out.println("7. Divide two integers. ");
System.out.println("8. Divide two douvles. ");
System.out.println("9. Compute a factorial product of a number (n!) ");
System.out.println("10. Exit. ");
System.out.println(" ");
System.out.println("Your choice?");
int userChoice = input.nextInt();
while (userChoice < 10)
{
switch (userChoice)
{
case 1:
System.out.println("Enter the first number * : ");
int addInt1 = input.nextInt();
System.out.println("Enter the 2nd number: ");
int addInt2 = input.nextInt();
System.out.println(add(addInt1, addInt2));
break;
case 2:
System.out.println("Enter the first number !: ");
double plusDub1 = input.nextDouble();
System.out.println("Enter the 2nd number: #");
double plusDub2 = input.nextDouble();
System.out.println(addDbl(plusDub1, plusDub2));
break;
case 3:
System.out.println("Enter the first number: #");
int subInt1 = input.nextInt();
System.out.println("Enter the 2nd number: ");
int subInt2 = input.nextInt();
System.out.println(subtract(subInt1, subInt2));
break;
case 4:
System.out.println("Enter the first number: $");
double subDub1 = input.nextDouble();
System.out.println("Enter the 2nd number: ");
double subDub2 = input.nextDouble();
System.out.println(subtractDbl(subDub1, subDub2));
break;
case 5:
System.out.println("Enter the first number: %");
int multInt1 = input.nextInt();
System.out.println("Enter the 2nd number: ");
int multInt2 = input.nextInt();
System.out.println(multiply(multInt1, multInt2));
break;
case 6:
System.out.println("Enter the first number: ^");
double multDub1 = input.nextDouble();
System.out.println("Enter the 2nd number: ");
double multDub2 = input.nextDouble();
System.out.println(multiplyDbl(multDub1, multDub2));
break;
case 7:
System.out.println("Enter the first number: &");
int divInt1= input.nextInt();
System.out.println("Enter the 2nd number: ");
int divInt2= input.nextInt();
System.out.println(divide(divInt1, divInt2));
break;
case 8:
System.out.println("Enter the first number: -");
double divDub1= input.nextDouble();
System.out.println("Enter the 2nd number: ");
double divDub2= input.nextDouble();
System.out.println(divideDbl(divDub1, divDub2));
break;
case 9:
System.out.println("Enter a number: ");
int factoNum= input.nextInt();
System.out.println(factorialProduct(factoNum));
break;
default:
}//end switch
showMenu();
}//end while
}
public static int add(int n1, int n2)
{
//declare variables
int sum;
// sum and return
sum = n1 + n2;
return sum;
}
public static double addDbl(double n1, double n2)
{
//declaration
double sum;
// sum and return
sum = n1 + n2;
return sum;
}
public static int subtract(int n1, int n2)
{
int total;
total = n1 - n2;
return total;
}
public static double subtractDbl(double n1, double n2)
{
double total;
total = n1 - n2;
return total;
}
public static int multiply(int n1, int n2)
{
int total;
total = n1 * n2;
return total;
}
public static double multiplyDbl(double n1, double n2)
{
double total;
total = n1 * n2;
return total;
}
public static int divide(int n1, int n2)
{
int total;
total = n1 / n2;
return total;
}
public static double divideDbl(double n1, double n2)
{
double total;
total = n1 / n2;
return total;
}
public static int factorialProduct(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * factorialProduct(n - 1);
}
}
}
You call showMenu from showMenu, which is recursion, and you definitely don't need this here. What you need instead is a while loop around getting the mode and executing the operation. I would shape the code like this (warning - pseudocode):
public static void main() {
while (true) {
// display your menu
userChoice = nextInt()
switch (userChoice) {
case 1:
...
case 2:
...
...
case 10:
return; // exits from main
}
}
}
Related
I cannot quite figure out how to make my basic financial calculator be able to run a new set of numbers without closing the program. What I currently have allows me to run one set of numbers, and when I get to "Would you like to continue?", when I press 1 it simply will print "Would you like to continue?" however many times I press 1. Here is what I have so far:
package Register;
import java.util.Scanner;
public class Register {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Register myRegister = new Register();
System.out.println("Welcome to the Electricity Bill calculator.");
System.out.print("Enter amount of electricity (kW) used in the daytime: ");
float num1 = scan.nextFloat();
System.out.print("Enter amount of electricity (kW) used in the evening: ");
float num2 = scan.nextFloat();
System.out.print("Enter rate for daytime: ");
float num3 = scan.nextFloat();
System.out.print("Enter rate for evening: ");
float num4 = scan.nextFloat();
float day1 = num1 * num3;
float night2 = num2 * num4;
float total = day1 + night2;
{
System.out.println("Electricity Bill: $" + total);
}
System.out.println("");
boolean keepLooping = true;
while (keepLooping) {
System.out.print("Would you like to continue? Press 1 continue or 0 to exit.");
int answer = scan.nextInt();
if(answer == 0) {
keepLooping = false;
} else {
keepLooping = true;
}
}
}
}
You have used while loop around asking choice statements only. So use while loop at the beginning in main method as below:
import java.util.Scanner;
public class Register{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Register myRegister = new Register();
boolean keepLooping = true;
while(keepLooping) {
System.out.println("Welcome to the Electricity Bill calculator.");
System.out.print("Enter amount of electricity (kW) used in the daytime: ");
float num1 = scan.nextFloat();
System.out.print("Enter amount of electricity (kW) used in the evening: ");
float num2 = scan.nextFloat();
System.out.print("Enter rate for daytime: ");
float num3 = scan.nextFloat();
System.out.print("Enter rate for evening: ");
float num4 = scan.nextFloat();
float day1 = num1 * num3;
float night2 = num2 * num4;
float total = day1 + night2;
System.out.println("Electricity Bill: $" + total);
System.out.println("");
System.out.print("Would you like to continue? Press 1 continue or 0 to exit.");
int answer = scan.nextInt();
if(answer == 0) {
keepLooping = false;
} else {
keepLooping = true;
}
}
}
}
I've made a basic calculator program and I'm getting this exception:
java.util.InputMismatchException
java.util.Scanner.next(Unknown Source)
The code runs just fine but when exception occurs it doesn't allows the user to input using Scanner. What am I doing wrong and how can I fix it?
package string;
import java.util.Scanner;
import java.lang.Exception;
public class Calculator {
double sum(double a,double b)
{
double c =a+b;
return c;
}
double subtract(double a,double b)
{
double c= a-b;
return c;
}
double multiply(double a,double b)
{
double c=a*b;
return c;
}
double divide(double a,double b)
{
double c=a/b;
return c;
}
public static void main(String[] args) {
Calculator f= new Calculator();
int choice;
int z;
Scanner s1 =new Scanner(System.in);
do{
try{
System.out.println("Welcome To Mini Calculator: Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
System.out.print("Please Enter Your Choice Number: ");
choice = s1.nextInt();
System.out.println();
switch(choice){
case 1:
System.out.print("Please Enter The First Number: ");
double x= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double y= s1.nextDouble();
double u = f.sum(x,y);
System.out.println();
System.out.println("The Sum Of Two Numbers is: " + u);
break;
case 2:
System.out.print("Please Enter The First Number: ");
double q= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double w= s1.nextDouble();
double i= f.subtract(q,w);
System.out.println();
System.out.println("The Substraction Of Two Numbers is: "+i );
break;
case 3:
System.out.print("Please Enter The First Number: ");
double e= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double r= s1.nextDouble();
double o= f.multiply(e, r);
System.out.println();
System.out.println("The Multiplication Of Two Numbers " + o);
break;
case 4:
System.out.print("Please Enter The First Number: ");
double t= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double k= s1.nextDouble();
double p= f.divide(t,k);
System.out.println();
System.out.println("The Divison of Two Numbers is: "+ p);
break;
default:System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}
}
catch(Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
}
System.out.println();
System.out.println("Do You Want To perform Another Functionality?");
System.out.println("Press 1 to Continue and Press 2 to Terminate The Program");
z= s1.nextInt(); // Issue comes here. It runs fine without exception. When exception occurs in above code ,it doesn't take input and shows another exception
}
while(z==1);
System.out.println();
System.out.println("Thank You For Using Calculator");
s1.close();
}
}
When you enter a wrong input, it goes in the catch but the input is still here, so z= s1.nextInt(); throws another exception which is not catched and it crashes
So you need to read the input in the catch, to clear the scanner :
} catch (Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
s1.nextLine();
}
Also, you have a lot of code duplicate, and variable names which means nothing, this is not very good compare to standards, I would suggestsomething like this to replace your whole switch{ ... }
System.out.println();
System.out.print("Please Enter The First Number: ");
double numb1 = s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double numb2 = s1.nextDouble();
double res;
String operation = "";
switch (choice) {
case 1:
res = f.sum(numb1, numb2);
operation = "Sum";
break;
case 2:
res = f.subtract(numb1, numb2);
operation = "Substraction";
break;
case 3:
res = f.multiply(numb1, numb2);
operation = "Multiplication";
break;
case 4:
res = f.divide(numb1, numb2);
operation = "Divison";
break;
default:
res = 0;
System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}
System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: " + res);
Can you find the source of error in this?
package calc;
import java.util.Scanner;
public class Calc {
Scanner scan = new Scanner(System.in);
public void add() {
System.out.println("Enter 1st number");
int s1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int s2 = scan.nextInt();
scan.nextLine();
int sum = s1 + s2;
System.out.println("The sum is: " + sum);
}
public void diff() {
System.out.println("Enter 1st number");
int d1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int d2 = scan.nextInt();
scan.nextLine();
int diff = d1 - d2;
System.out.println("The difference is: " + diff);
}
public void prod() {
System.out.println("Enter 1st number");
int p1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int p2 = scan.nextInt();
scan.nextLine();
int prod = p1 + p2;
System.out.println("The product is: " + prod);
}
public void quo() {
System.out.println("Enter 1st number");
int q1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int q2 = scan.nextInt();
scan.nextLine();
int quo = q1 + q2;
System.out.println("The quotient is: " + quo);
}
public static void main(String[] args) {
do {
Calc op = new Calc();
Scanner scan = new Scanner(System.in);
char ans = 0;
System.out.println("Calculator");
System.out.println("1.Addition\n" + "2.Subtraction\n" + "3.Multiplication\n" + "4.Division\n" + "Enter operation number:");
int n1 = scan.nextInt();
scan.nextLine();
switch (n1) {
case 1:
op.add();
break;
case 2:
op.diff();
break;
case 3:
op.prod();
break;
case 4:
op.quo();
break;
default:
System.out.println("Invalid input");
break;
}
System.out.println("Try again? [Y/N]");
ans = scan.nextLine().charAt(0);
} while (ans == 'Y' || ans == 'y');
}
}
and then netbeans has this auto correct that resulted into this:
package calc;
import java.util.Scanner;
public class Calc {
private static char ans;
it added a "private static char ans;" and I would like to understand more how did that fix my code. Thanks
ans is defined within the do{ ... } while() loop but it must be defined outside, to make it available for condition in the while.
So do:
char ans = 0;
do {
Calc op = new Calc();
Scanner scan = new Scanner(System.in);
ans = 0;
so I'm learning JAVA by myself and I have only the basic knowledge about programming languages in general. I wrote this simple calculator program to try and apply what I've been learning this far, but the problem is that it doesn't print the age, instead it prints a 0 and I don't know why:
this is the class
public class userinput {
private String name;
private int age;
public tuna (String name, int age){
name = "dina";
age = 3;
}
public void simpleMessage2(){
System.out.println("hello " + name + " ready to use our calculator?");
}
public void setName(String Uname){
name = Uname;
}
public void setAge(int uage){
uage = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public void printname(){
System.out.printf("your name is %s", getName());
System.out.println();
}
public void printage(){
System.out.println("your age is");
System.out.println(getAge());
System.out.println();
}}
and this is the main class:
import java.util.Scanner;
class calc {
public static void main (String args[]) {
String name1;
int age1;
Scanner bucky = new Scanner(System.in);
int choice, num1, num2, sum;
System.out.println("Hey, enter your name");
name1 = bucky.nextLine();
System.out.println("Hey, enter your age");
age1 = bucky.nextInt();
tuna objc1 = new userinput(name1, age1);
objc1.setName(name1);
objc1.printname();
System.out.println();
objc1.setAge(age1);
objc1.printage();
System.out.println();
System.out.println("this is a basic calculator, select from the menu:");
System.out.println("Enter 1 for summation");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for module");
System.out.println("Enter 0 to exit");
choice = bucky.nextInt();
while (choice != 0) {
switch(choice){
case 1:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sum is equal to: ");
sum = num1 + num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 2:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sub is equal to: ");
sum = num1 - num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 3:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mul is equal to: ");
sum = num1 * num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 4:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the div is equal to: ");
sum = num1 / num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 5:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mod is equal to: ");
sum = num1 % num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
default:
System.out.println("Invalid entry, please try again");
choice = bucky.nextInt();
break;
}
System.out.println("Bye!");
}
}
}
Where is the tuna class?
tuna objc1 = new userinput(name1, age1);
the userinput's Constructor:
public userinput(String n, int a){
name = n;
age = a;
}
I was just learning
the first error you don't have tuna class change the error to
tuna objc1 = new userinput(name1,age1);
to
userinput objc1 = new userinput();
then i change your code like that the method you use it in first class
and the second class to make operation
the first class
import java.util.Scanner;
public class userinput {
private String name;
private int age;
public void simpleMessage2(){
System.out.println("hello " + name + " ready to use our calculator?");
}
public void setName(String Uname){
name = Uname;
}
public void setAge(int uage){
age= uage; }
public void printname(){
System.out.printf("your name is %s", getName());
System.out.println();
}
public void printage(){
System.out.printf("your age is %s", getAge());
System.out.println();}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
the second class
class calc {
public static void main (String args[]) {
String name1;
int age1;
Scanner bucky = new Scanner(System.in);
int choice, num1, num2, sum;
System.out.println("Hey, enter your name");
name1 = bucky.nextLine();
System.out.println("Hey, enter your age");
age1 = bucky.nextInt();
userinput objc1 = new userinput();
objc1.setName(name1);
objc1.printname();
System.out.println();
objc1.setAge(age1);
objc1.printage();
System.out.println();
objc1.simpleMessage2();
System.out.println();
System.out.println("this is a basic calculator, select from the menu:");
System.out.println("Enter 1 for summation");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for module");
System.out.println("Enter 0 to exit");
choice = bucky.nextInt();
while (choice != 0) {
switch(choice){
case 1:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sum is equal to: ");
sum = num1 + num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 2:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sub is equal to: ");
sum = num1 - num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 3:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mul is equal to: ");
sum = num1 * num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 4:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the div is equal to: ");
sum = num1 / num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 5:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mod is equal to: ");
sum = num1 % num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
default:
System.out.println("Invalid entry, please try again");
choice = bucky.nextInt();
break;
}
}
System.out.println("Bye!");}
}
I hope i help you
Use a while loop to keep asking the user to enter the order of
numbers until the user does give two numbers in the right order (first
smaller than second
Hi! i'm a beginner in java and I have this code but I can't loop the "error" message. it just prints 2 times
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
int num1, num2;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please type two numbers:");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
if (num1 < num2) {
int counter = num1;
while (counter <= num2) {
System.out.print(counter + " ");
counter = counter + 1;
}
}
else {
System.out.println("Error: the first number must be smaller than the second");
System.out.print("Please type two numbers: ");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
}
}
}
int num1,num2;
while (num1>=num2) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type two numbers");
System.out.printn("first number must be smaller than the second:)";
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
}
int num1,num2;
while (num1>=num2) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type two numbers:");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
if(num1>=num2) {
System.out.println("Error: First number must be smaller than the second.");
}
}