用户输入“n”后如何保持程序循环

时间:2018-04-08 03:09:56

标签: java string while-loop int drjava

我一直在编写这个程序,用户输入的值代表一周中的某一天。然后它会显示星期几以及早上闹钟响起的时间。然后它询问用户是否希望关闭程序,如果不然,它将再次启动。当用户输入“n”时,我无法循环并重新开始。

 public class alarmClock {

   public static void main(String[] args) {

     String endProgram = "y";
     while (endProgram == "y") {
       int weekday = readInt("What day of the week is it");
       weekday(weekday);
       String endProgram = readString("Do you want to end the program y/n");
     }
     System.out.print(endProgram);
   }

   public static String readString(String prompt) {
     System.out.println(prompt);
     java.util.Scanner keyboard = new java.util.Scanner(System.in);
     return keyboard.nextLine();
   }

   public static int readInt(String prompt) {
     System.out.println(prompt);
     java.util.Scanner keyboard = new java.util.Scanner(System.in);
     return keyboard.nextInt();
   }

   public static void weekday(int weekday) {
     if (weekday == 1) {
       System.out.println("On monday the alarm will ring at 7am");
     } else if (weekday == 2) {
       System.out.println("On tuesday the alarm will ring at 7am");
     } else if (weekday == 3) {
       System.out.println("On wednesday the alarm will ring at 7am");
     } else if (weekday == 4) {
       System.out.println("On thursday the alarm will ring at 7am");
     } else if (weekday == 5) {
       System.out.println("On friday the alarm will ring at 7am");
     } else if (weekday == 6) {
       System.out.println("On saturay the alarm will ring at 7am");
     } else if (weekday == 7) {
       System.out.println("On Sunday the alarm will ring at 7am");
     }
   }
 }

0 个答案:

没有答案