-2

我在我的一个课程中遇到了一个问题,该程序跳过了一些变量。该程序的想法是我想使用 bufferedreader 将变量写入文件。用户注册并登录(这是在另一个类中完成的),然后他们选择他们想要发布的事件类型,然后选择他们插入所需的信息。我在这里分享的方法是在用户选择“在线活动”的情况下。

public void OnlineEvent()
    {
        try{
        File file = new File("Student&EventInfo.txt");
        FileWriter fw = new FileWriter(file);
        bw = new BufferedWriter(fw);
        
        System.out.println("What Is The Name/Title Of Your Event?");
        Title = sc.nextLine();
        bw.write("Title Of Event: " + Title);
        bw.write("\n");
        System.out.println("What Is The Description Of Your Event?");
        Description = sc.nextLine();
        bw.write("Description Of Event: " + Description);
        bw.write("\n");
        System.out.println("What Is The URL Code For The Online Event?");
        URL = sc.nextLine();
        bw.write("Event's URL Code: " + URL);
        bw.write("\n");
        System.out.println("At What Time Will The Event Be Held?");
        Time = sc.nextLine();
        bw.write("Time Of Event: " + Time);
        bw.write("\n");
        System.out.println("When Is The Date Of The Event?");
        Date = sc.nextLine();
        bw.write("Date Of Event" + Date);
        bw.write("\n");
        System.out.println("Will A Booking Be Required For This Event?");
        Booking = sc.nextLine();
        bw.write("Booking Required? :" + Booking);
        bw.write("\n");
        System.out.println("Will There Be Repeats This Event?");
        System.out.println("1. Yes");
        System.out.println("2. No");   
        b = sc.nextInt();
        
        //Execution In Case The User Says No
        if(b == 2)
        {
            b = b - 2;
            bw.write("There Will Be No Repeats For This Event");
            System.out.println("Do You Have Any Restrictions/Limitations To The Event?");
            System.out.println("(!)Eg: No Loud Music or Maximum Of 30 Participants");
            System.out.println("1. Yes");
            System.out.println("2. No");
            b = sc.nextInt();
            
            if(b == 1)
            {
                b = b - 1;
                System.out.println("What Restrictions/Limitations Will This Event Have?");
                Restrictions = sc.nextLine();
                bw.write("There Will Be The Following Restrictions:" + Restrictions);
                bw.write("\n");
                bw.close();        
            }
            
            if(b == 2)
            {
                b = b - 2;
                bw.write("There Will Be No Restrictions/Limitations For This Event");
                bw.write("\n");
                bw.close();
            }
        }
          
        //The Regular Execution If The User Says Yes
        if(b == 1)
        {
            b = b - 1;
            bw.write("This Event Will Have Repeats.");
            bw.write("\n");
            System.out.println("At What Time Do You Intend To Repeat This Event?");
            RETime = sc.nextLine();
            bw.write("Event Repeat Time: " + RETime);
            bw.write("\n");
            System.out.println("On What Date Will This Event Be Repeated?");
            REDate = sc.nextLine();
            bw.write("Event Repeat Date: " + REDate);
            bw.write("\n");
            System.out.println("Do You Have Any Restrictions/Limitations To The Event?");
            System.out.println("(!)Eg: No Loud Music or Maximum Of 30 Participants");
            System.out.println("1. Yes");
            System.out.println("2. No");
            b = sc.nextInt();
            
            if(b == 1)
            {
                b = b - 1;
                System.out.println("What Restrictions/Limitations Will This Event Have?");
                Restrictions = sc.nextLine();
                bw.write("There Will Be The Following Restrictions:" + Restrictions);
                bw.write("\n");
                bw.close();        
            }
            
            //Execution In Case The User Says No
            if (b == 2)
            {
                b = b - 2;
                bw.write("There Will Be No Restrictions/Limitations For This Event");
                bw.write("\n");
                bw.close();
            }
              
            }//End Of If Statement
        }catch(Exception ex)
            {
                System.out.println("The Error is" + ex);
            }
    }//End Of Method
4

1 回答 1

-1

不要nextLine与任何其他扫描仪方法混合使用。要么只使用 nextLine,要么根本不使用 nextLine。如果您想阅读整行的价值(而且您通常会这样做!),请始终对您的扫描仪执行此操作:

Scanner s = new Scanner(...);
s.useDelimiter("\r?\n");

然后调用.next()读取一行。

注意:常见的建议是调用额外的 nextLine() 来“清除缓冲区”。这是个坏建议;如果用户曾经使用过空格键,它就不起作用。而且,您知道,它在大多数键盘上都是相当大的键。上面没有这样奇怪的警告,而且要简单得多。

于 2020-11-30T14:22:54.810 回答