package javaapplication2;
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args) {
student s1=new student();
System.out.print("请输入学生s1的姓名\n"); Scanner sc=new Scanner(System.in);
s1.name=sc.nextLine();
System.out.print("请输入学生s1的学号\n"); s1.sno=sc.nextInt();
System.out.print("请输入学生s1的专业\n"); s1.subject=sc.nextLine();
System.out.print("请输入学生s1的选修课程\n");
s1.course=sc.nextLine();
System.out.print("请输入想要进行的操作:1:获得学号 2:修改学号 3:获得所修课程 4:修改所修课程");
int a;
a=sc.nextInt();
switch(a) {
case 1:s1.getsno();break;
case 2:s1.modifysno();break;
case 3:s1.getcourse();break;
case 4:s1.modifycourse();break; default:System.out.print("您的输入有误"); }
s1.print();
}
}
class student{
String name;
int sno;
String subject;
String course;
void getsno() {
System.out.print("请输入想要获得学号的学生姓名:");
String n;
Scanner reader=new Scanner(System.in); n=reader.nextLine();
System.out.print("学号为:"+sno+"\n"); }
void modifysno() {
System.out.print("请输入想要修改学号的学生姓名:");
String n;
Scanner reader=new Scanner(System.in);
n=reader.nextLine();
int sno;
System.out.print("请输入修改后的学号:"); sno=reader.nextInt();
System.out.print("修改后的学号为:"+sno+"\n");
}
void getcourse() {
System.out.print("请输入想要获得课程的学生姓名:");
String n;
Scanner reader=new Scanner(System.in); n=reader.nextLine();
System.out.print(n+"所选修的课程为:"+course+"\n");
}
void modifycourse() {
System.out.print("请输入想要修改课程的学生姓名:");
String n;
Scanner reader=new Scanner(System.in);
n=reader.nextLine();
String course;
System.out.print("请输入修改后的课程名称:");
course=reader.nextLine();
System.out.print("修改后的课程为:"+course+"\n");
}
void print() {
System.out.print("学生的基本信息为:"); System.out.print(name+"\n"+sno+"\n"+subject+"\n"+course+"\n");
}}
为什么不能输入学生专业,而是输入学生专业和选修课程的提示语句一起出来,只允许输入一次,这是什么问题?
在学生类里,打印函数所要输出的学号和课程是修改后的学号和课程,怎样把修改后的学号和课程这两个变量从修改学号和修改课程的函数里拿到打印函数里进行输出?谢谢!