gpt4 book ai didi

java - 使用 While 与 If 防止数组中出现重复的整数 ID 号

转载 作者:行者123 更新时间:2023-11-30 05:22:55 25 4
gpt4 key购买 nike

我创建了一个程序,可以添加、查看、更新和删除学生数组。为了防止用户输入重复的ID号,我使用了以下代码:

public static void addStud() {
int numID, year;
String userName, course;

int addMore;

do {

System.out.println("1. Enter Student ID: ");
numID = sc.nextInt();
sc.nextLine();
for(int x = 0; x < count; x++){
if(numID == stud[x].getNumID()) {
System.out.println("The Student ID: " +numID+ " already exist.\nEnter New Student ID: ");
numID = sc.nextInt();
sc.nextLine();
x = 0;
}
}
System.out.println("2. Enter Student Name");
userName = sc.nextLine();
System.out.println("3. Enter Student Course");
course = sc.nextLine();
System.out.println("4. Enter Student Year");
year = sc.nextInt();

stud[count] = new Student(numID, year, userName, course);
++count;

System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);

}

但是,由于某种原因(一次又一次)输入 ID 号 1-5,程序会接受重复的 ID [1]。我所做的是将 if(numID == Stud[x].getNumID()) 更改为 while(numID == Stud[x].getNumID()) 它解决了我的问题。

我不擅长调试,我只是想知道出了什么问题?为什么它与 while 一起工作而不与 if 一起工作?

贝娄是我的完整程序。尝试输入 ID 号 1 - 5 并重复,如果使用 IF 语句,将接受重复的 ID 号。

import java.util.Scanner;

公共(public)类StudentArray {

static Scanner sc = new Scanner(System.in);
static Student[] stud = new Student[100];
static int count = 0;

public static void main(String[] args) {

while (true) {
int select;
System.out.println("1. Add Student Record");
System.out.println("2. View Student Record");
System.out.println("3. Update Student Record");
System.out.println("4. Delete Student Record");
System.out.println("0. Exit");
select = sc.nextInt();

switch (select) {
case 1:
addStud();
break;
case 2:
viewStud();
break;
case 3:
updateStud();
break;
case 4:
deleteStud();
break;
case 0:
return;
default:
System.out.println("Invalid Option");
}
}

}

public static void addStud() {
int numID, year;
String userName, course;

int addMore;

do {

System.out.println("1. Enter Student ID: ");
numID = sc.nextInt();
sc.nextLine();
for(int x = 0; x < count; x++){
if(numID == stud[x].getNumID()) { // change if to while prevents duplicate
System.out.println("The Student ID: " +numID+ " already exist.\nEnter New Student ID: ");
numID = sc.nextInt();
sc.nextLine();
x = 0;
}
}
System.out.println("2. Enter Student Name");
userName = sc.nextLine();
System.out.println("3. Enter Student Course");
course = sc.nextLine();
System.out.println("4. Enter Student Year");
year = sc.nextInt();

stud[count] = new Student(numID, year, userName, course);
++count;

System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);

}

public static void viewStud() {

while(true) {
int select;

System.out.println("1. View Record by ID number ");
System.out.println("2. View Record by Course ");
System.out.println("3. View Record by Course and Year ");
System.out.println("4. View All ");
System.out.println("0. Return Main Menu ");
select = sc.nextInt();
sc.nextLine();

switch (select) {

case 1:
int view1;
System.out.println("Please enter Student ID Number: ");
view1 = sc.nextInt();
viewArray(view1);
break;
case 2:
String view2;
System.out.println("Please enter Student Course: ");
view2 = sc.nextLine();
viewArray(view2);
break;
case 3:
String view3;
int view4;
System.out.println("Please enter Student Course: ");
view3 = sc.nextLine();
System.out.println("Please enter Student Year: ");
view4 = sc.nextInt();
viewArray(view3, view4);
break;
case 4:
viewArray();
break;
case 0:
return;
default:
System.out.println("Invalid Option");

}

}
}
public static void viewArray(){
System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
for (Student student : stud) {
if (student != null) {
System.out.println(student.getNumID()+"\t\t\t\t"+student.getUserName()+ "\t\t\t\t"+student.getCourse()+"\t\t\t\t"+ student.getYear());
}
}
}
public static void viewArray(int key){
boolean isExist = false;
int temp = 0;

for(int x = 0; x < count; ++x){
if(key == stud[x].getNumID()){
temp = x;
isExist = true;
break;
}

}

if(isExist){
System.out.println("1. Student ID: " + stud[temp].getNumID());
System.out.println("2. Student Name: " + stud[temp].getUserName());
System.out.println("3. Student Course: " + stud[temp].getCourse());
System.out.println("4. Student Year: " + stud[temp].getYear() +"\n");
}
else
System.out.println("The Student ID: " +key+ " is invalid");

}
public static void viewArray(String key){
boolean isExist = false;

for(int x = 0; x < count; ++x){
if(key.equalsIgnoreCase(stud[x].getCourse())){
System.out.println("1. Student ID: " + stud[x].getNumID());
System.out.println("2. Student Name: " + stud[x].getUserName());
System.out.println("3. Student Course: " + stud[x].getCourse());
System.out.println("4. Student Year: " + stud[x].getYear() +"\n");
isExist = true;
}

}

if(isExist == false){
System.out.println("The Student Course: " +key+ " is invalid");
}

}
public static void viewArray(String course, int year){
boolean isExist = false;

for(int x = 0; x < count; ++x){
if(course.equalsIgnoreCase(stud[x].getCourse()) && year == stud[x].getYear()){
System.out.println("1. Student ID: " + stud[x].getNumID());
System.out.println("2. Student Name: " + stud[x].getUserName());
System.out.println("3. Student Course: " + stud[x].getCourse());
System.out.println("4. Student Year: " + stud[x].getYear() +"\n");
isExist = true;
}

}

if(isExist == false){
System.out.println("The Student Course: " +course+ " and Year: "+year+" is invalid");
}

}
public static void updateStud(){
int numID, temp = 0;
boolean flag = false;
System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
for (Student student : stud) {
if (student != null) {
System.out.println(student.getNumID()+"\t\t\t\t"+student.getUserName()+ "\t\t\t\t"+student.getCourse()+"\t\t\t\t"+ student.getYear());
}
}

System.out.println("Enter Student ID to update: ");
numID = sc.nextInt();
sc.nextLine();

for(int x = 0; x < count && flag == false; x++){
if (numID == stud[x].getNumID()){
temp = x;
flag = true;
}
}
if(flag) {
System.out.println("Enter Student Name: ");
stud[temp].setUserName(sc.nextLine());
System.out.println("Enter Student Course");
stud[temp].setCourse(sc.nextLine());
System.out.println("Enter Student Year");
stud[temp].setYear(sc.nextInt());
System.out.println("The Student ID: " + numID + " record has been updated");
}
else
System.out.println("The Student ID: " +numID+ " is invalid");
}
public static void deleteStud() {
int numID, temp = 0;
boolean flag = false;
if (count > 0){ // check if array is empty
System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
for (Student student : stud) {
if (student != null) {
System.out.println(student.getNumID()+"\t\t\t\t"+student.getUserName()+ "\t\t\t\t"+student.getCourse()+"\t\t\t\t"+ student.getYear());
}
}

System.out.println("Enter Student ID to delete: ");
numID = sc.nextInt();
sc.nextLine();

for(int x = 0; x < count && flag == false; x++){
if (numID == stud[x].getNumID()){
temp = x; // get the index
flag = true; // stops the loop if id is found
}
}
for( ; temp < count -1; temp++){
stud[temp]=stud[temp+1];
}
stud[count-1] = null;
--count;
}
else {
System.out.println("Cannot delete [Array is Empty]");
}

}

}

我的学生类(class):

public class Student {

private int numID, year;
private String userName, course;

public Student(int numID, int year, String userName, String course) {

this.numID = numID;
this.year = year;
this.userName = userName;
this.course = course;

}


public int getNumID() {
return numID;
}

public void setNumID(int numID) {
this.numID = numID;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}

}

最佳答案

如果您使用 for 解决问题,则不需要 while 循环环形。假设您输入用户 ID 1 至 5,然后再次输入 1。 for 的第一次迭代循环会发现1 == stud[0].getNumID() ,所以你会要求提供新的学生证。然后你会得到一个新的输入并重置 x0

现在,x将增加到1通过x++循环的一部分,因此如果您输入 1同样,循环永远不会比较 1stud[0].getNumID() ,自 x==1 。因此重复的输入将被接受。

可以通过将 x 重置为 -1 来修复此问题:

for(int x = 0; x < count; x++){
if(numID == stud[x].getNumID()) {
System.out.println("The Student ID: " +numID+ " already exist.\nEnter New Student ID: ");
numID = sc.nextInt();
sc.nextLine();
x = -1;
}
}

当然,有更好的方法来检查重复的 ID(这会更具可读性和更高效)。使用HashSet<Integer>存储所有使用过的 ID,并使用 Set检查新的 ID 是否已被使用。

例如:

System.out.println("1. Enter Student ID: ");
numID = sc.nextInt();
sc.nextLine();
Set<Integer> ids = new HashSet<>();
while (!ids.add(numID)) { // add returns false if numID is already in the Set
System.out.println("The Student ID: " +numID+ " already exist.\nEnter New Student ID: ");
numID = sc.nextInt();
sc.nextLine();
}
...

关于java - 使用 While 与 If 防止数组中出现重复的整数 ID 号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59234363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com