Close
|
Close
Code:int daysTillWeekend = day == 0 ? 0 : 6 - day;
Code:public class MidtermYourName { public static void main(String[] args) { int day = (int) (Math.random() * 7); System.out.println("The random number here is " + day +";" + " therefore, the day is " + getName(day) + "."); if (day != 6 && day != 0) { int remaining = 6 - day; System.out.println("It will take " + remaining + " days to get to the next weekend."); } else { System.out.println("Enjoy!"); } } public static String getName(int day) { switch(day) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; } return null; } }
Thanks for that, Got the exam finished and working flawlessly
Also thanks for helping too, I like how you did that my professor would of probably gave me bonus points if I just copied what you put. But this is a learning experience and I wanted to learn in the process. Might just save what you wrote for any future help lol
Code:import java.util.Random; public class MidtermExtea { private enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday; public int untilWeekend() { if(this == Saturday || this == Sunday) return 0; return Saturday.ordinal() - this.ordinal(); } } public static void main(String[] args) { Day[] days = Day.values(); int random = new Random().nextInt(days.length); Day day = days[random]; int untilWeekend = day.untilWeekend(); System.out.println("The random number here is " + random + "; Therefore, the day is " + day.name() + "."); if(untilWeekend == 0) System.out.println("Enjoy!"); else System.out.println("It will take " + untilWeekend + " " + (untilWeekend == 1 ? "day" : "days") + " to get to the next weekend."); } }
Edit: Sorry, didn't notice you already finished your exam!
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |