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 10 of 10

Thread: Java while loops and switches

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java while loops and switches

    Hi,
    I'm a beginner at Java programming and I was wondering if anyone could help / explain how to use a while loop in a task. The task is to create a menu which is appears constantly after the user has made an input. The user should enter instructions in the form of the numbers 1 to 4 as follows:
    1 - Reroll the dice
    2-get the value
    3-show maximum
    4-show minimum
    Once the user has entered a number and an instruction is made the menu must re-appear asking the user to make a choice again. This must happen repeatedly
    Example:
    User enters: 4
    Program displays: "The minimum value is: n"
    Then the menu reappears asking the user to make another choice
    I understand I need to use a while loop (and maybe a switch) but I’m unsure how to make this happen.
    I'm not really good at explaining things so I apologize if anything is unclear or just confusing.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java while loops and switches

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java while loops and switches

    You need to make an attempt and write some code. When you get stuck post the code here using code tags. Include full error messages. Ask a specific question.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java while loops and switches

    //Asks the user to input a number for dices sides
    System.out.println("How many sides should the dice have?") ;
    int numberOfSides ;
    Scanner scanner = new Scanner (System.in);
    numberOfSides = scanner.nextInt() ;
    Dice2 d= new Dice2 (numberOfSides);
     
    // The user can only enter 4,6,8,12,20 or 100
    //Any other number should terminate the program
    if ( numberOfSides == 4 || numberOfSides == 6 || numberOfSides == 8 || numberOfSides == 12 ||numberOfSides == 20 ||numberOfSides == 100 ){
    [B]once the number of sides is declared this menu should appear [/B]
    System.out.println("Please make a choice:") ;
    System.out.println("1 - reroll the dice") ;
    System.out.println("2 - get the value") ;
    System.out.println("3 - show the maximum") ;
    System.out.println("4 - show the minimum") ;
    } else {
    System.exit(0);
    } // closing else statement
     
    //Storing the users choice and current value of the dice
    int choice ;
    choice = scanner.nextInt() ;
    int currentValue;
    currentValue = d.getValue() ;
     
    [B]This is suppose to be a while loop which makes the menu re-appear after the user has made a choice[/B]
    while (choice == 1 || choice == 2 || choice == 3 || choice == 4) {
    System.out.println("Please make a choice:") ;
    System.out.println("1 - reroll the dice") ;
    System.out.println("2 - get the value") ;
    System.out.println("3 - show the maximum") ;
    System.out.println("4 - show the minimum") ;
    choice++;
     
    //after the user enters a choice either one of the cases become true... any other number should terminate the program
    switch (choice) {
    case 1: d.reroll();
    break;
    case 2: System.out.println("The current value is " + currentValue);
    break;
    case 3: int maximum = Math.max(currentValue, numberOfSides);
    break;
    case 4: int minimum = Math.min(currentValue, numberOfSides);
    System.out.println("The minimum is " + minimum) ;
    break;

    I did make a loop using the if statement however after submitting the lectures feedback was not to use to many if statements

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java while loops and switches

    Ok, well this might get messy, but it might also help you find the problem yourself. For each line of code in your main, add a comment which explains the purpose of the line. It sounds to me like you've made a logic mistake somewhere, so following this strategy might allow you to find your mistake.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java while loops and switches

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    The code needs to test that the user has entered a valid response.
    You could use an if statement
    or have a default: case in the switch statement.


    BTW You should ALWAYS code a default case in a switch to catch any unexpected values and print out a message saying the value was unexpected.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java while loops and switches

    while (condition) {
    statement;
    }

    Basically if the condition is true then the statements are executed until the condition becomes false.

    Example:

    int num = 5;
    while (num <= 5) {
    num++;
    System.out.println("*");
    }
    5 is smaller than 5 4 times and equal to 5 once. This means the statement will be executed 5 times.

    Scanner scan = new Scanner (System.in);
    String example;
    System.out.print("Enter a word: ");
    example = scan.nextLine();
     
    switch (example) {
     
    case "no":
    System.out.println("This is just an example.");
    break;
     
    case "yes":
    System.out.println("This is just an example.");
    break;
    }

    So basically it will execute the tasks of the case matched.

    Edit: you can also use default while using the switch (basically means anything but the above cases).

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java while loops and switches

    This means the statement will be executed 5 times.
    @D.K Did you test that code? How many times did the execute the statement in loop?


    you can also use default
    You should ALWAYs use the default to catch unexpected input.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java while loops and switches

    Quote Originally Posted by Norm View Post
    @D.K Did you test that code? How many times did the execute the statement in loop?



    You should ALWAYs use the default to catch unexpected input.
    Opps, forgot that was an infinite loop XD.

    Edit: if you add num++; in the while loop that will fix the infinite loop (increments num by 1 each time).

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java while loops and switches

    Quote Originally Posted by D.K View Post
    Opps, forgot that was an infinite loop XD.
    Not an infinite loop either. It will execute once, num is 5 which is <= 5, enters loop, increments to 6 which is no longer <= 5 exits loop.
    Improving the world one idiot at a time!

Similar Threads

  1. Switches
    By Jfrogz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2013, 04:35 PM
  2. newbie start java program with switches
    By fortune2k in forum Java Theory & Questions
    Replies: 4
    Last Post: November 1st, 2010, 05:13 PM
  3. Java Loops help please?
    By Sarah-Perkin in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2009, 02:52 PM
  4. switches and else if
    By silverspoon34 in forum Collections and Generics
    Replies: 1
    Last Post: November 11th, 2009, 04:09 PM
  5. How to Use different Java loops
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM