我的程序在生成if语句后跳过用户输入



我在一个程序的类中遇到了一个问题,该程序跳过了一些变量。该程序的想法是,我想使用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

不要混合使用nextLine和任何其他扫描仪方法。只使用nextLine,或者根本不使用nextLine。如果你想读取整行的价值(你通常会这样做!(,请始终对你的扫描仪执行以下操作:

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

然后调用CCD_ 2来读取一行。

注意:常见的建议是调用额外的nextLine((来"清除缓冲区"。这是个坏建议;如果用户使用空格键,它将不起作用。而且,你知道,在大多数键盘上,它是一个相当大的键。以上内容没有这些奇怪的警告,而且要简单得多。

相关内容

  • 没有找到相关文章

最新更新