I got an assignment today and my teacher at community college isn't helpful at all, he couldn't even help us without comparing it to our code to his. My assignment is to check if a person is 21 and old enough to enter the bar. If not they cant enter, and if they are they can. However, I need to write a program that will also check what month they were born in case they are exactly 21. Finally I need to make the program loop back and allow the person to type another birthday in so they don't need to keep running the program over and over, also when they are done it should say something like press x to exit the program. Here is what I came up with so far.. any advice is helpful as this is due tomorrow! import java.util.*; //necessary for Scanner class public class BarEntry2 { public static void main(String[] args){ testTheAgeGivenTheYear() ; } public static void testTheAgeGivenTheYear() { Scanner console = new Scanner(System.in); console.nextLine(); int currentYear = 2011; int birthYear = console.nextInt() ; int age = currentYear - birthYear; int currentMonth = 7 ; boolean haveMorePatrons = true; if (age >=21) { System.out.println("You may enter the bar!") ; } else { System.out.println("You may NOT enter the bar!") ; } if (age =21) { System.out.println("Please enter the month you were born!") ; } if (currentMonth >=8) { System.out.println("You may enter the bar!") ; } System.out.println("Do you want to test anymore patrons: (Y/N) ? "); while (haveMorePatrons) { System.out.println("please enter your birth year: ") ; System.out.println("You are " + age); String ans = console.nextLine(); if (ans.equals("n") || ans.equals("N")) haveMorePatrons = false ; System.out.println ("Good Bye") ; } } }
Code: import java.util.*; //necessary for Scanner class public class BarEntry2 { public static void main(String[] args){ testTheAgeGivenTheYear() ; } public static void testTheAgeGivenTheYear() { Scanner console = new Scanner(System.in); console.nextLine(); int currentYear = 2011; int birthYear = console.nextInt() ; int age = currentYear - birthYear; int currentMonth = 7 ; boolean haveMorePatrons = true; if (age >=21) { System.out.println("You may enter the bar!") ; } else { System.out.println("You may NOT enter the bar!") ; } if (age =21) { System.out.println("Please enter the month you were born!") ; } if (currentMonth >=8) { System.out.println("You may enter the bar!") ; } System.out.println("Do you want to test anymore patrons: (Y/N) ? "); while (haveMorePatrons) { System.out.println("please enter your birth year: ") ; System.out.println("You are " + age); String ans = console.nextLine(); if (ans.equals("n") || ans.equals("N")) haveMorePatrons = false ; System.out.println ("Good Bye") ; } } }
Code: import java.util.*; public class BarEntry2 { public static void main(String[] args){ testTheAgeGivenTheYear() ; } public static void testTheAgeGivenTheYear() { System.out.println("What year where you born in?") ; if (age >=21) { System.out.println("Come on in :)!") ; } else { System.out.println("Sorry you're to young :(!") ; } Scanner console = new Scanner(System.in); int currentYear = 2011; int currentMonth = 7 ; int birthMonth = console.nextInt() ; int birthYear = console.nextInt() ; int age = currentYear - birthYear; boolean CheckAdditionalAges = true; } } I'm so lost
Ok I got a lot done so i'm hoping for some help to get the final touches in. Code: import java.util.*; public class BarEntryHW { public static void main(String[] args){ testTheAgeGivenTheYear() ; } public static void testTheAgeGivenTheYear() { Scanner console = new Scanner(System.in); int currentYear = 2011; boolean checkAdditionalAges = true; while (checkAdditionalAges) { System.out.println("What year where you born in? Format:XXXX") ; int birthYear = console.nextInt() ; console.nextLine() ; int age = currentYear - birthYear; System.out.println("You are " + age ) ; if (age >21) { System.out.println("Come on in :)") ; } else { System.out.println("You're to young") ; } if (age == 21) { System.out.println("Sorry, You might be allowed in, what month where you born? Format:X") ; } int birthMonth = console.nextInt() ; console.nextLine() ; if (birthMonth <=7) { System.out.println("You may enter") ; } else { System.out.println("You are only 20, You may not enter :(") ; } System.out.println("Do you want to test any additional ages: (Y/N) ? ") ; String ans = console.nextLine() ; if (ans.equals("n") || ans.equals("N")) { checkAdditionalAges = false; System.exit(0); } else { (ans.equals("y") || ans.equals("Y")) checkAdditionalAges = true ; } } }
See the reason for why your posts are being edited and that link will help you format your posts better
when you get your first homework, make sure you try it your own because you wont be learning anythnig unless :S