Results 1 to 16 of 16
  1. #1
    Anonym is offline Member
    Join Date
    Mar 2017
    Posts
    6
    Rep Power
    0

    Default Hey guys I need a help with my Homework

    I need a code in Java (netbeans IDE) that will
    a)prompt the user for a valid number to represent the month
    b) Based off of the input, use a switch block to print out the name of the month
    c)Ask the user if they wish to continue or end
    d)If they choose to continue, prompt them for the number again.If they wish to end print Bye and end program

    We need to use:
    Scanner
    Boolean
    Switch
    _____
    Something that I have
    public static void main(String []args){
    Scanner input=new Scanner(System.in);
    boolean Correct=false;
    System.out.println("Please enter a number between 1 and 12");
    int number=input.nextInt();
    switch(number){
    case 1: System.out.println("January");break;
    case 2: System.out.println("February");break;
    case 3: System.out.println("March");break;
    case 4: System.out.println("April");break;
    case 5: System.out.println("May");break;
    case 6: System.out.println("June");break;
    case 7: System.out.println("July");break;
    case 8: System.out.println("August");break;
    case 9: System.out.println("September");break;
    case 10: System.out.println("October");break;
    case 11: System.out.println("November");break;
    case 12: System.out.println("December");break;
    }
    }//end main
    Last edited by Anonym; 03-07-2017 at 09:50 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Hey guys I need a help with my Homework

    Show us what you got. This is not a Code Factory™ or Rent-A-Coder™.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    Anonym is offline Member
    Join Date
    Mar 2017
    Posts
    6
    Rep Power
    0

    Default Re: Hey guys I need a help with my Homework

    Quote Originally Posted by SurfMan View Post
    Show us what you got. This is not a Code Factory™ or Rent-A-Coder™.
    public static void main(String []args){
    Scanner input=new Scanner(System.in);
    boolean Correct=false;
    System.out.println("Please enter a number between 1 and 12");
    int number=input.nextInt();
    switch(number){
    case 1: System.out.println("January");break;
    case 2: System.out.println("February");break;
    case 3: System.out.println("March");break;
    case 4: System.out.println("April");break;
    case 5: System.out.println("May");break;
    case 6: System.out.println("June");break;
    case 7: System.out.println("July");break;
    case 8: System.out.println("August");break;
    case 9: System.out.println("September");break;
    case 10: System.out.println("October");break;
    case 11: System.out.println("November");break;
    case 12: System.out.println("December");break;

    }
    }//end main

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Hey guys I need a help with my Homework

    Where is your question?

    And make it a habit to post code in [code][/code] tags.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    Anonym is offline Member
    Join Date
    Mar 2017
    Posts
    6
    Rep Power
    0

    Default Re: Hey guys I need a help with my Homework

    Quote Originally Posted by SurfMan View Post
    Where is your question?

    And make it a habit to post code in [code][/code] tags.
    The question is how do I make it loop and how do I make it so it asks if a user wants to continue or not

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Hey guys I need a help with my Homework

    how do I make it loop
    Look at using a while loop. while loops are for the case where you don't know how many iterations to do before you start the loop.
    Wrap the code that is to be repeated inside of a while loop. Make sure there is code inside the loop that will change the while's condition to false so the loop can exit.

    asks if a user wants to continue
    Use a print statement to ask
    and a Scanner class method to read the response.

    Be sure to wrap all posted code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Hey guys I need a help with my Homework

    If this your first assignment? Have you really not written a program yet to use a loop?

    I suggest you do the following:

    Step 1. Write a simple program to read input from the scanner and print it out.
    Step 2. Now modify Step 1 to use an if statement to print out something different
    depending on the value (like if it is less than 10 or greater or equal to 10.
    Step 3. Now put step 2 inside a loop.
    Step 4. Now add a conditional to step 3 to exit the loop if a specific number is encountered.
    Step 5. Make certain your existing switch statements work with a specific value (e.g. number = 10)
    Step 6. Make certain that a number out of range is handled correctly (switch statements use defaults)
    Step 7. Put you switch statements inside of Step 4.
    Step 8. Do some testing, fix some minor problems that may creep up and you're done.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Anonym is offline Member
    Join Date
    Mar 2017
    Posts
    6
    Rep Power
    0

    Default Re: Hey guys I need a help with my Homework

    Okay, I got the code guys, thank you.
    But now I have another problem... My code keeps running whenever a user puts a number from 0-12 but I need a code to ask the user if he wants to continue or end after each of the number entered
    Here is my code:

    Java Code:
     public static void main(String []args){
       Scanner input=new Scanner(System.in);
       System.out.println("Please enter a number between 1 and 12");//prints a prompt 
    boolean stop =true;
    boolean play=false;
    
       while(play=true){
           int number=input.nextInt();//Scanner input for a number
        switch(number){//switch statement for number = month
            
               case 1: System.out.println("January");break;//1=January
               case 2: System.out.println("February");break;//2=February
               case 3: System.out.println("March");break;//3=March
               case 4: System.out.println("April");break;//4=April
               case 5: System.out.println("May");break;//5=May
               case 6: System.out.println("June");break;//6=June
               case 7: System.out.println("July");break;//7=July
               case 8: System.out.println("August");break;//8=August
               case 9: System.out.println("September");break;//9=September
               case 10: System.out.println("October");break;//10=October
               case 11: System.out.println("November");break;//11=November
               case 12: System.out.println("December");break;//12=December
               default: System.out.println("Not a valid number in a range"+"\nWould you like to continue?"+"\n Please enter 1 for yes,0 for no");
               int decision=input.nextInt();
               if(number>=13)
                   switch(decision){
                       case 0: System.out.println("Bye");System.exit(0);break;
                       case 1: System.out.println("You chose to continue");play=false;
                   }if(decision>1){
                       System.out.println("Invalid Number entered.Will continue.");continue;
                   }
                   }
       }
    }switch end  
    }
    Last edited by Anonym; 03-08-2017 at 02:43 AM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Hey guys I need a help with my Homework

    Line 7 is probably the culprit. That is an assignment, not a test, so it's always true. So just do while (play).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Hey guys I need a help with my Homework

    Here is what I would attempt:
    - get rid of lines 24 - 32
    - add a sanity check between lines 8 and 9
    - sanity check: if number input is any number between 1 and 12, enter the switch statement
    - sanity check: else ask the user whether they want to continue and if not, set play to false.
    - adjust all texts and prompts

  11. #11
    Anonym is offline Member
    Join Date
    Mar 2017
    Posts
    6
    Rep Power
    0

    Default Re: Hey guys I need a help with my Homework

    Quote Originally Posted by benji2505 View Post
    Here is what I would attempt:
    - get rid of lines 24 - 32
    - add a sanity check between lines 8 and 9
    - sanity check: if number input is any number between 1 and 12, enter the switch statement
    - sanity check: else ask the user whether they want to continue and if not, set play to false.
    - adjust all texts and prompts
    I am sorry for stupid question but what is the sanity?

  12. #12
    Anonym is offline Member
    Join Date
    Mar 2017
    Posts
    6
    Rep Power
    0

    Default Re: Hey guys I need a help with my Homework

    thank you:)

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Hey guys I need a help with my Homework

    Quote Originally Posted by Anonym View Post
    I am sorry for stupid question but what is the sanity?
    It's a validation of the data entered by the user.
    You want to ensure it is between 1 and 12.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Hey guys I need a help with my Homework

    Quote Originally Posted by Anonym View Post
    .. what is the sanity?
    That's a difficult question, but here is an official answer.

    A sanity check is a slang expression and it tests the user input. The programmer tries to test whether the user is playing along nicely or not.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Hey guys I need a help with my Homework

    It is, of course, different to a Sanity Clause.
    As everyone knows, there's no such thing as a Sanity Clause.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Hey guys I need a help with my Homework

    But there is a Santa Claus, right?
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Thanks guys for your help!
    By dddJewelsbbb in forum Introductions
    Replies: 1
    Last Post: 06-16-2012, 09:46 PM
  2. hi guys
    By faust83 in forum Introductions
    Replies: 1
    Last Post: 05-04-2012, 01:25 AM
  3. Guys.. Need Help. .
    By zed43 in forum New To Java
    Replies: 3
    Last Post: 01-24-2012, 04:47 AM
  4. Guys please i need help !!! :d
    By omar_6630 in forum New To Java
    Replies: 7
    Last Post: 11-23-2010, 12:55 AM
  5. hi guys
    By pvsnmurthy in forum Introductions
    Replies: 0
    Last Post: 04-16-2009, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •