【发布时间】:2021-12-14 04:18:49
【问题描述】:
我创建了一个程序,该程序会询问学生的姓名、年份和所修课程。这个想法是,如果我按 y(yes),它应该循环返回并询问另一个学生,但在我的情况下,如果我输入 y,它不会问我。对初学者有什么建议吗?
import java.util.Scanner;
public class Practice{
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
String studentName, another;
int year,choice;
do{
System.out.println("Student name: ");
studentName = scan.nextLine();
System.out.println("Year: ");
year = scan.nextInt();
System.out.println("\t1.BSIT \n\t2.BSCS \n\t3.BSCpE \n\t4.BSN");
System.out.print("Choice: ");
choice = scan.nextInt();
scan.nextLine();
if(choice == 1) {
System.out.println("Course is BSIT");
}
else if(choice == 2 ) {
System.out.println("Course is BSCS");
}
else if(choice == 3 ) {
System.out.println("Course is BSCpE");
}
else if(choice == 4) {
System.out.println("Course is BSN");
}
System.out.println("Another Student? type Y if yes, and N if no");
another = scan.nextLine();
}while((another == "Y") || (another == "y"));
if ((another == "N") || (another == "n"))System.out.println("You are the last student.");
}
}
【问题讨论】:
-
你能发布你的输入案例吗
标签: java loops while-loop do-while