62,615
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class TestCourse {
public String course;
public String[] student = new String[100];
public int count;
public void getcourse() {
Scanner keyboard = new Scanner(System.in);
System.out.println("输入课程名称: ");
course = keyboard.nextLine();
}
public void getstudent() {
Scanner keyboard = new Scanner(System.in);
System.out.println("输入选修课程 : '" + course + "'的学生姓名,输入1结束");
boolean flat = true;
int i = 0;
while (flat) {
//student[i] = new String();
student[i] = keyboard.nextLine();
count++;
if (student[i++].equals("1"))
flat = false;
}
}
public void print() {
for (int i = 0; i < count - 1; i++) {
System.out.println("选该课程" + course + "的学生:" + student[i] + " ");
}
}
public static void main(String[] args) {
TestCourse course1 = new TestCourse();
course1.getcourse();
course1.getstudent();
course1.print();
// TestCourse course2 = new TestCourse();
// course2.getcourse();
// course2.getstudent();
// course2.print();
}
}
public void getstudent() {
Scanner keyboard = new Scanner(System.in);
System.out.println("输入选修课程" + course + "的学生姓名,输入1结束");
boolean flat = true;
int i = 0;
while (keyboard.hasNext()) {
String a=keyboard.next();
if (!a.equals("1")){
student[i]=a;
i++;
count++;
}else{
i=0;
break;
}
}
}