java编写一个学生和教师数据输入和显示程序
时间: 2024-05-18 19:18:39 浏览: 21
好的,您可以参考以下代码:
```java
import java.util.Scanner;
public class StudentAndTeacherInputAndDisplayProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请选择用户类型:");
System.out.println("1.学生");
System.out.println("2.教师");
int userType = input.nextInt();
if (userType == 1) {
System.out.println("请输入学生姓名:");
String name = input.next();
System.out.println("请输入学生年龄:");
int age = input.nextInt();
System.out.println("请输入学生性别(男/女):");
String gender = input.next();
System.out.println("请输入学生学号:");
String studentId = input.next();
System.out.println("学生信息如下:");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("性别:" + gender);
System.out.println("学号:" + studentId);
} else if (userType == 2) {
System.out.println("请输入教师姓名:");
String name = input.next();
System.out.println("请输入教师年龄:");
int age = input.nextInt();
System.out.println("请输入教师性别(男/女):");
String gender = input.next();
System.out.println("请输入教师工号:");
String teacherId = input.next();
System.out.println("教师信息如下:");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("性别:" + gender);
System.out.println("工号:" + teacherId);
} else {
System.out.println("输入有误,请重新运行程序。");
}
}
}
```
以上代码可以实现输入学生或教师的个人信息并打印输出。输入过程中可以根据提示输入相应的信息,最后会输出输入的信息,以确认输入无误。请根据您的具体需求进行相应的修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)