import java.util.Scanner;
public class Lab101{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Class Limit:");
int x = sc.nextInt();
for (char i=1; i<=x; i++) {
System.out.println("Enter Student Name: ");
sc.next().charAt(0);
}
System.out.println("Class Is Full");}}
Enter Class Limit:
3
Enter Student Name:
Input
Enter Student Name:
Name
Enter Student Name:
Here
Class Is Full
我现在已经解决了我的问题。但后来我发现了一个新问题!
Enter Class Limit:
3
Enter Student Name:
Input Name
Enter Student Name:
Enter Student Name:
Here
Class Is Full
一旦我进入了一个空间。它被计为一行中的两个输入并跳过第二行输入并前进到第三行。如何让它在一行中接受空间并将其计为一个,以便我可以像这样......
Enter Class Limit:
3
Enter Student Name:
Input Name Here
Enter Student Name:
Input Name Here
Enter Student Name:
Input Name Here
Class Is Full
答案 0 :(得分:2)
您不存储(或稍后显示)输入。我想你想做两件事。另外,如果您使用int
阅读nextInt()
,则必须使用nextLine()
。您可以使用Arrays.toString(Object[])
来显示学生姓名(所以String
s)。像,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Class Limit:");
int x = sc.nextInt();
sc.nextLine(); // <-- there's a newline.
String[] students = new String[x];
for (int i = 0; i < x; i++) {
System.out.printf("Enter Student Name %d: ", i + 1);
System.out.flush();
students[i] = sc.nextLine().trim();
}
System.out.println("Class Is Full");
System.out.println(Arrays.toString(students));
}