每次我在do-while循环结束时键入Y时,它都不会让我键入“输入要任命的姓名”我尝试更改c = input.next();到c = input.nextLine();但仍然没有工作。为什么?请帮助
import java.util.Scanner;
import APP.*;
public class AppointmentTest
{
public static void main(String[] args)
{
Appointment app = new Appointment();
String m, y;
double st, et;
//char c;
String c;
Scanner input = new Scanner (System.in);
do
{
System.out.print("Enter name for the appointment :");
m = input.nextLine();
app.setName(m);
System.out.print("Enter day appointment :");
y = input.nextLine();
app.setDay(y);
System.out.print("Enter start time :");
st = input.nextDouble();
app.setStartTime(st);
System.out.print("Enter end time :");
et = input.nextDouble();
app.setEndTime(et);
System.out.println("Duration of appointment :");
System.out.print("Do you have any other appointment? (Y/N) :");
//c = input.next().charAt(0);
c = input.next();
}while (c != "N" && c != "n");
}
}
[运算符!=
或==
检查参考;不是内容。您需要使用.equals
或..equalsIgnoreCase
(用于不区分大小写的比较)。
执行以下操作:
c = input.nextLine();
} while (!c.equalsIgnoreCase("N"));
也替换
st = input.nextDouble();
with
st = Double.parseDoublei(input.nextLine());
et = input.nextDouble()
同样适用。检查Scanner is skipping nextLine() after using next() or nextFoo()?了解更多信息。