Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to go back in Menu when i choose [Y/N] While - Switch Code

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post how to go back in Menu when i choose [Y/N] While - Switch Code

    import java.util.Scanner;

    public class DecimalMenu{

    public static void main(String[]args){
    Scanner scan = new Scanner(System.in);
    int num1;
    String decision;

    System.out.println("DECIMAL MENU");
    System.out.println("[1]Decimal to Binary");
    System.out.println("[2]Decimal to Octal ");
    System.out.println("[3]Decimal to Hexadecimal");
    System.out.println("[4]Back to Main Menu");

    Scanner myScanner=new Scanner(System.in);
    System.out.println("Enter Your Choice: ");
    num1=scan.nextInt();
    boolean yn = true;
    while(yn)
    {

    if(num1==1){

    System.out.print("Enter a Decimal number : ");
    String decimal=myScanner.nextLine();
    int num=Integer.parseInt(decimal,10);
    String binary=Integer.toBinaryString(num);
    System.out.println("Binary value is : "+binary);
    }
    if(num1==2){

    System.out.print("Enter a Decimal number : ");
    String decimal=myScanner.nextLine();
    int num=Integer.parseInt(decimal,10);
    String octal=Integer.toOctalString(num);
    System.out.println("Octal value is : "+octal);
    }
    if(num1==3){

    System.out.print("Enter a Decimal number : ");
    String decimal=myScanner.nextLine();
    int num=Integer.parseInt(decimal,10);
    String hexa=Integer.toHexString(num);
    System.out.println("Hexadecimal value is : "+hexa);
    }
    if(num1==4){

    }
    System.out.println("INPUT ANOTHER [Y/N]: ");
    decision = myScanner.nextLine();


    switch(decision)
    {
    case "Y":
    yn = true;
    break;
    }
    switch(decision)
    {
    case "N":
    yn = true;
    break;

    }
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to go back in Menu when i choose [Y/N] While - Switch Code

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Also make sure the code is properly formatted and indented to show nesting logic. The ending }s should NOT be in the same column. A pair of {}s should be in the same column.

    What happens when the code is executed? I see a while loop for going back. Is there a problem with it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] GUI button not linking back to code
    By StaticHS in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 1st, 2014, 04:16 AM
  2. How can I create an option menu with while and switch statements?
    By DABBISH in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 16th, 2013, 06:36 AM
  3. Replies: 1
    Last Post: September 15th, 2013, 10:01 PM
  4. Switch problem. How do I return to my menu?
    By jonny007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2012, 12:54 PM
  5. Replies: 7
    Last Post: November 26th, 2011, 07:29 AM

Tags for this Thread