Looping Java switch statement - java

My system is a banking system and I want my program to loop after the user has interacted with one of the processes (withdraw, view balance etc).
After user get result I want to give a chance to continue with another process:
package smartcode.atm.h.w;
import java.util.Scanner;
public class SmartCodeATMHW {
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
return;
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}

This here:
for (int i = 0; i < 3; i++) {
Around that other loop:
} while (end == false)
is simply bogus.
You want one loop that goes around your "menu"; and that loop ends when the user wants to end.
End of story.

I can see from the code you have already handled the requirement using end==false.
You need to remove the return; statement used after the switch case.
Also, for case 1 use break; instead of return;
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
break;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
You can also take input from the User whether he wants to continue or not and depending on the response, display the options again.

just put all cases into a infinite loop and when user select case 4 then system.exit(0); will terminate program otherwise it will loop.
import java.util.Scanner;
public class Test {
public static void main(String [] args){
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
while(true){
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
System.exit(0);
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}

Related

Can't get my code to repeat the questions for the game to continue

I have to write a code for my class I'm taking. It is a game based on betting on 2 colors and numbers from 1 - 36. The user has a set amount of chips already given to them which is 100. I have already written most of the code, however, I can't get my code to repeat the process. I am currently using a Do-While loop. but it just isn't working.
Here is my code:
import java.util.Scanner;
import java.lang.Math;
public class Program_8 {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int chipsNow = 100;
int userChoice;
int chipsBetted = 0;
//welcome message
welcome();
do {
int spinNum = (int)(Math.random() * 36) + 0;
userChoice = getMenuChoice(userInput);
//get user choice
if (userChoice == 1) {
int getNum = getNumber(userInput);
int getBet = getBet(userInput, chipsNow);
String determineColor = determineColor(spinNum);
System.out.println("\nSpinning Wheel....");
System.out.println("Spin Number: " + spinNum);
System.out.println("Spin Color: " + determineColor);
if (getNum == spinNum) {
chipsNow += getBet;
chipsBetted += getBet;
System.out.println("Congrats, you won!");
System.out.println("\nYou now have: " + chipsNow + " chips");
;
}
else {
chipsNow -= getBet;
System.out.println("\nSorry, you lost!");
System.out.println("You now have: " + chipsNow + " chips");
}
}
if (userChoice == 2) {
String getColor = getColor(userInput);
int getBet = getBet(userInput, chipsNow);
String determineColor = determineColor(spinNum);
System.out.println("Spinning Wheel....");
System.out.println("Spin Number: " + spinNum);
System.out.println("Spin Color: " + determineColor);
if (getColor.equals(determineColor)) {
chipsNow += getBet;
chipsBetted += getBet;
System.out.println("\nCongrats, you won!");
System.out.println("You now have: " + chipsNow + " chips");
}
else {
chipsNow -= getBet;
System.out.println("\nSorry, you lost!");
System.out.println("You now have: " + chipsNow + " chips");
}
}
}while (userChoice != 3 && chipsNow > 0);
}
//welcome message
public static void welcome() {
int chipsNow = 100;
System.out.println("############################");
System.out.println("# Welcome To Roulette #");
System.out.println("############################");
System.out.println("# Number Bets Payout: 35:1 #");
System.out.println("# Color Bets Payout: 1:1 #");
System.out.println("############################\n");
System.out.println("Chips owned: " + chipsNow + "\n");
System.out.println("1. Pick a number to bet on");
System.out.println("2. Pick a color to bet on");
System.out.println("3. Cash Out\n");
}
//get menu choice
public static int getMenuChoice(Scanner userInput) {
int getMenuChoice;
System.out.println("\nChoose an option [1-3]: ");
getMenuChoice = userInput.nextInt();
return getMenuChoice;
}
public static int getNumber(Scanner userInput) {
int getNumber;
do {
System.out.println("Enter a number to bet on [0-36]: ");
getNumber = userInput.nextInt();
}while (getNumber < 0 || getNumber > 36);
return getNumber;
}
public static String getColor(Scanner userInput) {
String getColor = "";
do{
System.out.println("Enter a color to bet on [Red or Black]: ");
getColor = userInput.next();
}while (!(getColor.equals("Red") || getColor.equals("Black")));
return getColor;
}
public static int getBet(Scanner userInput, int chipsNow) {
int getBet;
do{
System.out.println("Enter the number of chips to bet [1 - " + chipsNow + "]: ");
getBet = userInput.nextInt();
}while (getBet < 1 || getBet > chipsNow);
return getBet;
}
public static String determineColor(int spinNum) {
if (spinNum % 2 == 0) {
if (spinNum == 0) {
return "Green";
}
//even
else {
return "Red";
}
}
return "Black";
}
public static void report(int chipsNow) {
System.out.println("\nThanks for Playing!");
System.out.println("You Won a total of: " + chipsNow + " chips today");
}
}
So just looking at the code I'd:
Declare int userChoice = 0; outside the do while. This is needed for the while condition to work.
welcome(); and userChoice = getMenuChoice(userInput); should move into the do while thus it will repeat the welcome message and ask for a user choice everything the do while executes
Simplify your while loop like this while(userChoice != 3 && chipsNow > 0). This is just to make it more readable.
Lastly remove the break;s in your if(getNum == spinNum) { } else { }. A break will force the while loop to be exited regardless of whether the while condition is met. So basically if you win or lose a game your loop will exit, which I don't think is what you want. You only want the loop to exit if the chipsNow < 0 or the userChoice == 3

java loop will not end

I am having trouble on a program and I can't seem to fix the problem. I have a team roster program that gives the option of replacing a player's number and rating, but when that option is used the loop keeps going and it won't end and go back to the menu for the other options. If anyone could help point me in the right direction it would be appreciated.
import java.util.Scanner;
class PlayerRoster {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
int[] playerJerseyNumber = new int[5];
int[] playerRating = new int[5];
for (int i = 0; i < 5; i++) {
//Taking jersey number till it is in the range of 0-99
while (true) {
System.out.println("Enter player " + (i + 1)
+ "'s jersey number:");
playerJerseyNumber[i] = scanner.nextInt();
if (0 <= playerJerseyNumber[i] && playerJerseyNumber[i] <= 99) {
break;
} else {
System.out.println("Jersey number must be 0-99");
}
}
//Taking playerRating till it is in the range of 1-9
while (true) {
System.out.println("Enter player " + (i + 1) + "'s rating:");
playerRating[i] = scanner.nextInt();
if (1 <= playerRating[i] && playerRating[i] <= 9) {
break;
} else {
System.out.println("Player's ratings must be 1-9");
}
}
System.out.println("");
}
System.out.println("ROSTER");
//Displaying the rosters
for (int i = 0; i < 5; i++) {
System.out.println("Player " + (i + 1) + " -- Jersey number: "
+ playerJerseyNumber[i] + ", Rating: "
+ playerRating[i]);
}
System.out.println("");
//Printing the menu
do {
System.out.println("MENU " + "u - Update player rating");
System.out.println("a - Output players above a rating");
System.out.println("r - Replace player");
System.out.println("o - Output roster");
System.out.println("q - Quit");
System.out.println("");
System.out.println("Choose an option:");
char choice = scanner.next().charAt(0);
switch (choice) {
case 'u': {
System.out.println("Enter a jersey number:");
int playerJersey = scanner.nextInt();
System.out.println("Enter a new rating for player:");
int newRating = scanner.nextInt();
for (int i = 0; i < 5; i++) {
if (playerJerseyNumber[i] == playerJersey) {
playerRating[i] = newRating;
break;
}
}
}
break;
case 'a': {
System.out.println("Enter a rating:");
int aboveRating = scanner.nextInt();
System.out.println("ABOVE " + aboveRating);
for (int i = 0; i < 5; i++) {
if (playerRating[i] > aboveRating) {
System.out.println("Player " + (i + 1)
+ " -- Jersey number: "
+ playerJerseyNumber[i] + ", Rating: "
+ playerRating[i]);
}
}
}
break;
case 'r': {
boolean flag = true;
do {
System.out.println("Enter a jersey number:");
int newRating, playerNewJersey;
int playerJersey = scanner.nextInt();
for (int i = 0; i < 5; i++) {
if ((playerJerseyNumber[i] == playerJersey)) {
//Taking jersey number till it is in the range of 0-99
while (true) {
System.out.println("Enter a new jersey number:");
playerNewJersey = scanner.nextInt();
if (0 <= playerNewJersey && playerNewJersey <= 99) {
break;
} else {
System.out.println("Jersey number must be 0-99");
}
}
//Taking playerRating till it is in the range of 1-9
while (true) {
System.out.println("Enter a new rating for player:");
newRating = scanner.nextInt();
if (1 <= newRating && newRating <= 9) {
break;
} else {
System.out.println("Player's ratings must be 1-9");
}
}
playerJerseyNumber[i] = playerNewJersey;
playerJerseyNumber[i] = playerJersey;
playerRating[i] = newRating;
flag = false;
break;
}
}
if (!flag) {
System.out.println("Error: Invalid Jersey Number... Try Again...");
}
} while (flag);
}
break;
case 'o': {
System.out.println("ROSTER");
for (int i = 0; i < 5; i++) {
System.out.println("Player " + (i + 1)
+ " -- Jersey number: " + playerJerseyNumber[i]
+ ", Rating: " + playerRating[i]);
}
}
break;
case 'q':
break;
default:
break;
}
if (choice == 'q') break;
} while (true);
} catch (Exception e) {
}
return;
}
}
if (flag) {
System.out.println("Error: Invalid Jersey Number... Try Again...");
} }
Removing the ! solves the problem. Also, a few lines above it there is
playerJerseyNumber[i] = playerNewJersey;
playerJerseyNumber[i] = playerJersey;
I don't understand why you need to reassign it to playerJersey. This would cause the jersey number to not get updated.

How to reset the value of a double, int, float etc after an "Incorrect" input by the user

My question is in relation to my line of code resembling an ATM. The current balance being 10000, What i have written shows a message to the user that if they withdraw over the given balance that it is Insufficient balance. However the balance continues to change thus when Inquiring balance the balance is now < the allowed balance. My question is how should i go about resetting the double value for the balance when the Insufficient balance message is shown to the user?
Here is my code.
import java.util.Scanner;
class app {
public static void main(String[] args)
{
long pin = 2927942074l;
double balance = 10000.0;
int attempts = 3;
System.out.println("Please enter your pin.");
while (attempts > 0) {
Scanner keyboardpin = new Scanner(System.in);
long input = keyboardpin.nextLong();
if (input == pin) {
System.out.println("Correct");
System.out.println("Welcome to your ATM");
while (true) { // Keep printing your options unless "Quit" is chosen
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + " - Inquire Balance");
System.out.println(b + " - Withdraw");
System.out.println(c + " - Deposit");
System.out.println(d + " - Quit");
System.out.println("Please select what you want to do.");
Scanner menuselect = new Scanner(System.in);
int menuinput = menuselect.nextInt();
if (menuinput == a) {
System.out.println(balance);
continue;
}
if (menuinput == b) {
System.out.println("Please enter a withdrawal amount.");
Scanner withdrawamount = new Scanner(System.in);
double withdrawbalace = withdrawamount.nextDouble();
balance = (balance - withdrawbalace);
if (withdrawbalace > balance)
System.out.println("Insufficient balance");
if (withdrawbalace <= balance)
System.out.println("Youre new balance is " + balance);
}
if (menuinput == c) {
}
if (menuinput == d) {
break;
}
if (attempts == 0) {
System.out.println("Maximum number of attempts exceeded");
}
}
} else {
System.out.println("Wrong");
attempts--;
System.out.println("You have " + attempts + " attempts remaining.");
}
}
}
}
Just check whether the user has enough balance before subtracting the amount.
if (menuinput == b) {
System.out.println("Please enter a withdrawal amount.");
Scanner withdrawamount = new Scanner(System.in);
double withdrawbalace = withdrawamount.nextDouble();
if (withdrawbalace > balance)
System.out.println("Insufficient balance");
if (withdrawbalace <= balance)
balance = (balance - withdrawbalace);
System.out.println("Youre new balance is " + balance);
}
Your main problem has been solved in the previous answer, but your code logic has some errors. I have fixed it for you.
import java.util.Scanner;
class app {
public static void main(String[] args) {
final long pin = 2927942074L;
double balance = 10000.0;
int attempts = 3;
System.out.println("Please enter your pin.");
Scanner keyboard = new Scanner(System.in);
while (attempts > 0) {
long input = keyboard.nextLong();
if (input == pin) {
System.out.println("Correct");
System.out.println("Welcome to your ATM");
// Keep printing your options unless "Quit" is chosen
while (true) {
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + " - Inquire Balance");
System.out.println(b + " - Withdraw");
System.out.println(c + " - Deposit");
System.out.println(d + " - Quit");
System.out.println("Please select what you want to do.");
int menuInput = keyboard.nextInt();
if (menuInput == a) {
System.out.println(balance);
} else if (menuInput == b) {
System.out.println("Please enter a withdrawal amount.");
double withdrawBalance = keyboard.nextDouble();
if (balance >= withdrawBalance) {
balance -= withdrawBalance;
System.out.println("Your new balance is " + balance);
} else System.out.println("Insufficient balance");
} else if (menuInput == c) {
// Deposit code here
} else if (menuInput == d) break;
}
} else {
attempts--;
if (attempts == 0) {
System.out.println("Maximum number of attempts exceeded");
break;
}
System.out.println("Wrong");
System.out.println("You have " + attempts + " attempts remaining.");
}
}
keyboard.close();
}
}
And here are some tips...
Try to choose meaningful variable names.
If the variable has a constant value make it final
If the variable name consists of two, or more, words capitalize the first letter of the second word (eg. menuInput)
If you check the same condition for different cases you can use switch or if, else if, else
Never forget to close the objects you used to release memory
Good Luck

User to return to main menu if yes is selected

Hi guys i am having an issue with how to do this, i have googled it but its not making much sense.
i need to do this;
The program asks the user if they wish to continue.
 If Yes is selected, it will return to the Main menu.
 If No is selected, Total Amount Payable will be
displayed and then the program will terminate
int option, quantity, confirm;
float childTotal;
float adultTotal;
float seniorTotal;
final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;
char resume;
Scanner input = new Scanner(System.in);
System.out.println("1 = Child (4-6 yrs)");
System.out.println("2 = Adult (16+ yrs)");
System.out.println("3 = Senior (60+ yrs)" + "\n");
System.out.println("Enter your option:" );
option=input.nextInt();
switch (option) {
case 1:
System.out.println("Enter total No of tickets for Child:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " child tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
case 2:
System.out.println("Enter total No of tickets for Adult:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " adult tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
default:
System.out.println("Enter total No of tickets for Senior:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " senior tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
}
if (confirm !=1) {
System.out.println("Incorrect key! User to go back to main menu");
}
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y') {
} else {
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
}
Create a Boolean variable set as true.
boolean continueLoop = true;
Add your main logic into a while loop until continue is true
while(continueLoop){
//Do your code here
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y'){}
else{
//Do Code here
continueLoop=false;
}
} //End while loop.
After the while loop continue with your code. I have changed the condition of resume == y to resume !=y because if the user does not press y the code should stop iterating.
Your code would become
int option, quantity, confirm;
float childTotal;
float adultTotal;
float seniorTotal;
final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;
boolean continueLoop = true;
char resume;
Scanner input = new Scanner(System.in);
while(continueLoop){
System.out.println("1 = Child (4-6 yrs)");
System.out.println("2 = Adult (16+ yrs)");
System.out.println("3 = Senior (60+ yrs)" + "\n");
System.out.println("Enter your option:" );
option=input.nextInt();
switch (option) {
case 1:
System.out.println("Enter total No of tickets for Child:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " child tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
case 2:
System.out.println("Enter total No of tickets for Adult:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " adult tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
default:
System.out.println("Enter total No of tickets for Senior:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " senior tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
}
if (confirm !=1) {
System.out.println("Incorrect key! User to go back to main menu");
}
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y') {
}else{
continueLoop = false;
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
}
}
}

Java do while loop won't loop- Java (All int)

Code for menu options won't loop, but has no errors. I intentionally wrote it similar to my while loop that loops just fine. I have to use only integers, and my instructor did tell me to turn my inputs into variables. Most of it isn't quite finished but I'm working on that at the same time as trying to figure all this out.
//Loop to check user input
num=-1;
while (num<0)
{
//Getting user entered interger
System.out.print("Please enter a positive number: ");
num = kb.nextInt();
//Ensuring positive interger
if (num >= 0)
{
System.out.println("You've entered the number " + num + ".");
}
else
{
System.out.println("Error! The number you've entered is not a valid interger.");
}
}
copy = num;
//Printing menu loop
copy2 = -1;
do
{
//Menu making
System.out.println(" ");
System.out.println("1. Enter a new number.");
System.out.println("2. Print the number of odd digits, even digits, and zeros in the interger.");
System.out.println("3. print if the number is light or heavy.");
System.out.println("4. Print the prime numbers between 2 and the interger (inclusive).");
System.out.println("5. Quit the program.");
System.out.println(" ");
System.out.print("Please enter your menu choice: ");
choice = kb.nextInt();
copy2 =choice;
//Checking entry
if (0<copy2 && copy2<=5)
{
System.out.println("You chose option " +copy2+".");
}
//Option one
if (copy2 == 1)
{
System.out.print("Will work on soon.");
}
//Option two
if (copy2 == 2)
{
oddNum=0;
evenNum=0;
zero=0;
while (copy>0)
{
if (copy %10==0)
{
zero++;
}
else if (copy %2==1)
{
evenNum++;
}
else
{
oddNum++;
}
}
copy = copy/10;
System.out.println("Even numbers: " + evenNum+
"\nOdd numbers: "+ oddNum +
"\nZeros: " +zero );
}
//Option three
if (copy2 == 3)
{
loh = 0;
do
{
System.out.println("To check if your number is light or heavy, we need a second interger.");
System.out.print("Please enter a second positive interger: ");
loh = kb.nextInt();
if (loh >= 0)
{
numWeight = ((loh +copy)/2);
//Test num weight
System.out.println("Check: " +numWeight);
if (numWeight > copy)
{
System.out.print("The number " + copy + " compared to the number "
+ loh + " is a heavy number.");
}
else
{
System.out.print("The number " + copy + " compared to the number "
+ loh + " is a light number.");
}
}
if (loh < 0)
{
System.out.println("Error! The number you've entered is not a valid interger.");
}
}while (loh <=0);
}
//Option four
if (copy2 ==4)
{
primeNumbers = 0;
for (int i=1; i<=copy; i++)
{
int counter = 0;
for(int prime = i; prime>=1; prime--)
{
if (i%prime==0)
{
counter = counter +1;
}
}
if (counter==2)
{
primeNumbers = primeNumbers + i;
}
}
System.out.println("Prime numbers from 2-"+copy+" are: ");
System.out.println(primeNumbers);
}
//Option five
else
{
System.out.print("Error! Please enter a valid menu option.");
}
}while (copy2 <0 && copy2 >=6);
}//End main
}//End class
You should use switch case for your purpose. nested if/else isn't good idea for menu options but switch case make it more clear to understand what are you going to do.
switch (num) {
case c1:
statements // they are executed if variable == c1
System.out.println("1. Enter a new number.");
break;
case c2:
System.out.println("2. Print the number of odd digits, even digits, and zeros in the interger.");
statements // they are executed if variable == c2
break;
case c3:
case c4:
statements // they are executed if variable == any of the above c's
break;
. . .
default:
statements // they are executed if none of the above case is satisfied
break;
}

Categories