import java.util.Scanner; public class Login { private static String username = "张三"; private static String password = "123456"; public static void main(String[] args) throws ToomanyErrorsException, NamePasswordMismatchException { Scanner scanner = new Scanner(System.in); int count = 0; System.out.println("请输入用户名: "); String username = scanner.next(); while (true) { System.out.println("请输入密码: "); String password = scanner.next(); if ("登录成功".equals(login(username, password))) { System.out.println("登录成功"); break; } count++; if (count==3){ throw new ToomanyErrorsException("密码输入次数已达上限!!!"); } } } public static String login(String username, String password) throws NamePasswordMismatchException { if (!Login.username.equals(username)) { System.out.println("该用户名尚未注册,请核对后输入"); return "用户名有误"; } else { if (!Login.password.equals(password)) { try { throw new NamePasswordMismatchException("密码输入有误"); } catch (Exception e) { e.printStackTrace(); } } else { return "登录成功"; } return "密码有误"; } } }补全两个自定义异常类
时间: 2023-12-20 10:07:03 浏览: 152
以下是两个自定义异常类的补全代码:
1. ToomanyErrorsException
```
public class ToomanyErrorsException extends Exception {
public ToomanyErrorsException(String message) {
super(message);
}
}
```
2. NamePasswordMismatchException
```
public class NamePasswordMismatchException extends Exception {
public NamePasswordMismatchException(String message) {
super(message);
}
}
```
这两个异常类都继承了Java内置的Exception类,并实现了自己的构造函数。在这个例子中,ToomanyErrorsException用于处理密码输入错误次数过多的情况,而NamePasswordMismatchException则用于处理用户名和密码不匹配的情况。通过这两个自定义异常类,我们可以更好地处理程序中出现的异常情况,使程序更加健壮和稳定。
相关问题
import java.util.Scanner; public class Login { private static String username = "张三"; private static String password = "123456"; public static void main(String[] args) throws ToomanyErrorsException, NamePasswordMismatchException { Scanner scanner = new Scanner(System.in); int count = 0; System.out.println("请输入用户名: "); String username = scanner.next(); while (true) { System.out.println("请输入密码: "); String password = scanner.next(); if ("登录成功".equals(login(username, password))) { System.out.println("登录成功"); break; } count++; if (count==3){ throw new ToomanyErrorsException("密码输入次数已达上限!!!"); } } } public static String login(String username, String password) throws NamePasswordMismatchException { if (!Login.username.equals(username)) { System.out.println("该用户名尚未注册,请核对后输入"); return "用户名有误"; } else { if (!Login.password.equals(password)) { try { throw new NamePasswordMismatchException("密码输入有误"); } catch (Exception e) { e.printStackTrace(); } } else { return "登录成功"; } return "密码有误"; } } }哪里有问题
代码中存在以下问题:
1. 异常处理不恰当。在 `login` 方法中,当密码输入有误时,应该抛出 `NamePasswordMismatchException` 异常。但是,在异常处理代码中,只是简单地打印了堆栈信息,没有处理异常。应该将异常向上抛出,由 `main` 方法中的异常处理代码来处理。
2. 代码中的变量名冲突。在 `main` 方法中,定义了一个名为 `username` 的局部变量,与类中的静态变量 `username` 名称相同,会导致局部变量覆盖类中的静态变量。应该使用不同的变量名,或者使用 `this.username` 来引用类中的静态变量。
3. 没有处理用户名输入有误的情况。当输入的用户名与预设的用户名不一致时,应该提示用户该用户名尚未注册,并要求重新输入用户名。
4. 在 `login` 方法中,当密码输入有误时,返回的字符串为 "密码有误",但是实际上应该抛出 `NamePasswordMismatchException` 异常。
下面是修改过后的代码:
```java
import java.util.Scanner;
public class Login {
private static String username = "张三";
private static String password = "123456";
public static void main(String[] args) throws ToomanyErrorsException, NamePasswordMismatchException {
Scanner scanner = new Scanner(System.in);
int count = 0;
System.out.println("请输入用户名: ");
String inputUsername = scanner.next();
while (!username.equals(inputUsername)) {
System.out.println("该用户名尚未注册,请核对后输入");
System.out.println("请输入用户名: ");
inputUsername = scanner.next();
}
while (true) {
System.out.println("请输入密码: ");
String inputPassword = scanner.next();
if ("登录成功".equals(login(inputUsername, inputPassword))) {
System.out.println("登录成功");
break;
}
count++;
if (count == 3) {
throw new ToomanyErrorsException("密码输入次数已达上限!!!");
}
}
}
public static String login(String username, String password) throws NamePasswordMismatchException {
if (!Login.password.equals(password)) {
throw new NamePasswordMismatchException("密码输入有误");
} else {
return "登录成功";
}
}
}
```
用Java编写一个《学生信息管理系统》里面有功能1.系统需求分析、初始化信息模块(学生类、用户类); 2.用户登录界面模块; 3. 学生信息管理主菜单界面模块; 4.学生信息添加模块; 5.学生信息修改模块; 6.学生信息删除模块; 7.学生信息查询模块; 8. 用户信息查询模块; 9. 用户密码修改模块;
好的,以下是一个简单的学生信息管理系统的Java代码:
1. 学生类和用户类
```java
public class Student {
private String name;
private String id;
private String gender;
private String major;
private double score;
// 构造函数
public Student(String name, String id, String gender, String major, double score) {
this.name = name;
this.id = id;
this.gender = gender;
this.major = major;
this.score = score;
}
// getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
public class User {
private String username;
private String password;
// 构造函数
public User(String username, String password) {
this.username = username;
this.password = password;
}
// getter和setter方法
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
2. 用户登录界面模块
```java
import java.util.Scanner;
public class Login {
private String username;
private String password;
public void login() {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
username = input.nextLine();
System.out.println("请输入密码:");
password = input.nextLine();
if (checkUser(username, password)) {
System.out.println("登录成功!");
} else {
System.out.println("用户名或密码错误!");
}
}
private boolean checkUser(String username, String password) {
// 这里可以根据具体情况自定义用户的账号密码,例如:
return username.equals("admin") && password.equals("123456");
}
}
```
3. 学生信息管理主菜单界面模块
```java
import java.util.Scanner;
public class MainMenu {
private Scanner input = new Scanner(System.in);
public void showMenu() {
while (true) {
System.out.println("请选择要执行的操作:");
System.out.println("1. 学生信息添加");
System.out.println("2. 学生信息修改");
System.out.println("3. 学生信息删除");
System.out.println("4. 学生信息查询");
System.out.println("5. 用户信息查询");
System.out.println("6. 用户密码修改");
System.out.println("7. 退出程序");
int choice = input.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
modifyStudent();
break;
case 3:
deleteStudent();
break;
case 4:
queryStudent();
break;
case 5:
queryUser();
break;
case 6:
modifyPassword();
break;
case 7:
System.out.println("程序已退出!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
private void addStudent() {
// 添加学生信息的代码
}
private void modifyStudent() {
// 修改学生信息的代码
}
private void deleteStudent() {
// 删除学生信息的代码
}
private void queryStudent() {
// 查询学生信息的代码
}
private void queryUser() {
// 查询用户信息的代码
}
private void modifyPassword() {
// 修改密码的代码
}
}
```
4. 学生信息添加模块
```java
import java.util.ArrayList;
import java.util.Scanner;
public class AddStudent {
private Scanner input = new Scanner(System.in);
private ArrayList<Student> students;
public AddStudent(ArrayList<Student> students) {
this.students = students;
}
public void add() {
System.out.println("请输入学生姓名:");
String name = input.nextLine();
System.out.println("请输入学生学号:");
String id = input.nextLine();
System.out.println("请输入学生性别:");
String gender = input.nextLine();
System.out.println("请输入学生专业:");
String major = input.nextLine();
System.out.println("请输入学生成绩:");
double score = input.nextDouble();
Student student = new Student(name, id, gender, major, score);
students.add(student);
System.out.println("学生信息添加成功!");
}
}
```
5. 学生信息修改模块
```java
import java.util.ArrayList;
import java.util.Scanner;
public class ModifyStudent {
private Scanner input = new Scanner(System.in);
private ArrayList<Student> students;
public ModifyStudent(ArrayList<Student> students) {
this.students = students;
}
public void modify() {
System.out.println("请输入要修改的学生学号:");
String id = input.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入修改后的学生姓名(原值为" + student.getName() + "):");
String name = input.nextLine();
System.out.println("请输入修改后的学生性别(原值为" + student.getGender() + "):");
String gender = input.nextLine();
System.out.println("请输入修改后的学生专业(原值为" + student.getMajor() + "):");
String major = input.nextLine();
System.out.println("请输入修改后的学生成绩(原值为" + student.getScore() + "):");
double score = input.nextDouble();
student.setName(name);
student.setGender(gender);
student.setMajor(major);
student.setScore(score);
System.out.println("学生信息修改成功!");
return;
}
}
System.out.println("未找到该学生,请重新输入!");
}
}
```
6. 学生信息删除模块
```java
import java.util.ArrayList;
import java.util.Scanner;
public class DeleteStudent {
private Scanner input = new Scanner(System.in);
private ArrayList<Student> students;
public DeleteStudent(ArrayList<Student> students) {
this.students = students;
}
public void delete() {
System.out.println("请输入要删除的学生学号:");
String id = input.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
students.remove(student);
System.out.println("学生信息删除成功!");
return;
}
}
System.out.println("未找到该学生,请重新输入!");
}
}
```
7. 学生信息查询模块
```java
import java.util.ArrayList;
import java.util.Scanner;
public class QueryStudent {
private Scanner input = new Scanner(System.in);
private ArrayList<Student> students;
public QueryStudent(ArrayList<Student> students) {
this.students = students;
}
public void query() {
System.out.println("请选择要查询的方式:");
System.out.println("1. 查询所有学生信息");
System.out.println("2. 按学号查询学生信息");
System.out.println("3. 按姓名查询学生信息");
System.out.println("4. 按专业查询学生信息");
System.out.println("5. 按成绩查询学生信息");
int choice = input.nextInt();
switch (choice) {
case 1:
showAll();
break;
case 2:
queryById();
break;
case 3:
queryByName();
break;
case 4:
queryByMajor();
break;
case 5:
queryByScore();
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
private void showAll() {
System.out.println("所有学生信息如下:");
for (Student student : students) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getGender() + " " +
student.getMajor() + " " + student.getScore());
}
}
private void queryById() {
System.out.println("请输入要查询的学生学号:");
String id = input.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getGender() + " " +
student.getMajor() + " " + student.getScore());
return;
}
}
System.out.println("未找到该学生,请重新输入!");
}
private void queryByName() {
System.out.println("请输入要查询的学生姓名:");
String name = input.nextLine();
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getGender() + " " +
student.getMajor() + " " + student.getScore());
}
}
}
private void queryByMajor() {
System.out.println("请输入要查询的学生专业:");
String major = input.nextLine();
for (Student student : students) {
if (student.getMajor().equals(major)) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getGender() + " " +
student.getMajor() + " " + student.getScore());
}
}
}
private void queryByScore() {
System.out.println("请输入要查询的学生成绩(格式如60-80):");
double minScore = input.nextDouble();
double maxScore = input.nextDouble();
for (Student student : students) {
if (student.getScore() >= minScore && student.getScore() <= maxScore) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getGender() + " " +
student.getMajor() + " " + student.getScore());
}
}
}
}
```
8. 用户信息查询模块
```java
public class QueryUser {
private User user;
public QueryUser(User user) {
this.user = user;
}
public void query() {
System.out.println("用户名:" + user.getUsername());
}
}
```
9. 用户密码修改模块
```java
import java.util.Scanner;
public class ModifyPassword {
private Scanner input = new Scanner(System.in);
private User user;
public ModifyPassword(User user) {
this.user = user;
}
public void modify() {
System.out.println("请输入旧密码:");
String oldPassword = input.nextLine();
if (!oldPassword.equals(user.getPassword())) {
System.out.println("密码错误!");
return;
}
System.out.println("请输入新密码:");
String newPassword = input.nextLine();
user.setPassword(newPassword);
System.out.println("密码修改成功!");
}
}
```
最后,在主函数中调用以上模块即可:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// 初始化用户和学生信息
User user = new User("admin", "123456");
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("张三", "1001", "男", "计算机科学与技术", 89.5));
students.add(new Student("李四", "1002", "女", "软件工程", 92.0));
students.add(new Student("王五", "1003", "男", "物联网工程", 85.5));
// 用户登录
Login login = new Login();
login.login();
// 显示主菜单
MainMenu menu = new MainMenu();
menu.showMenu();
}
}
```
阅读全文