我在过去一周遇到麻烦,试图解决如何检查Id是否已经添加到阵列中,如果有,请阻止用户输入其余数据,我将非常感谢一些帮助有这个。
我的代码:
class CustomerUse {
public static int checkId(Customer theArray[], int noOfValues,
String searchId) {
int idCheck = 0;
int step = 0;
while (step < noOfValues) {
step++;
if ((step < noOfValues) && (theArray[step].getId().equals(searchId))) {
idCheck = -1;
}
}
return idCheck;
}
public static void listAllNames(Customer[] names, int NoOfElements) {
int index;
String result;
for (index = 0; index < NoOfElements; index++) {
System.out.println(names[index]);
}
}
public static int findPlace(Customer theArray[], int balance, int noOfValues) {
int step;
int place;
step = 0;
while ((step < noOfValues) && (balance < theArray[step].getBalance())) {
step++;
}
place = step;
return place;
}
public static int addOne(Customer theArray[], int place, String Id,
Customer theObject, int noOfValues) {
int step;
if (noOfValues == 0) {
theArray[0] = theObject;
noOfValues++;
} else {
for (step = noOfValues - 1; step >= place; step--) {
theArray[step + 1] = theArray[step];
}
theArray[place] = theObject;
noOfValues++;
}
return noOfValues;
}
public static int deleteName(Customer theArray[], int noOfElements) {
String searchId;
int step;
int whichOne = 0;
System.out.println("Enter Id of customer to be deleted ");
searchId = EasyIn.getString();
step = 0;
while ((step < noOfElements)
&& !(theArray[step].getId().equals(searchId))) {
step++;
}
if (step < noOfElements) {
whichOne = step;
for (step = whichOne; step < noOfElements - 1; step++) {
theArray[step] = theArray[step + 1];
}
noOfElements--;
} else {
System.out.println(" Sorry this customer doesn't exist");
}
return noOfElements;
}
public static void main(String[] args) {
Customer empArray[];
int index;
int noOfElements;
String searchName;
Customer tempObject;
int step;
int option = 0;
int place;
String newName = "";
String newId;
int newBalance;
int checkID = 0;
empArray = new Customer[100000];
noOfElements = 0;
System.out
.println("\n 1. Add Customer \n 2. Delete Customer \n 3. List all Customers \n 4. Exit");
System.out.print("Enter Option ");
option = EasyIn.getInt();
while (option != 4) {
if (option == 1) {
System.out.print("Enter ID ");
newId = EasyIn.getString();
System.out.print("Enter name ");
newName = EasyIn.getString();
System.out.print("Enter Balance ");
newBalance = EasyIn.getInt();
System.out.println();
tempObject = new Customer(newName, newId, newBalance);
place = findPlace(empArray, newBalance, noOfElements);
noOfElements = addOne(empArray, place, newId, tempObject,
noOfElements);
checkID = checkId(empArray, noOfElements, newId);
if (checkID == -1) {
System.out.println("Sorry this ID already exists");
checkID = 0;
}
} else if (option == 2) {
noOfElements = deleteName(empArray, noOfElements);
} else if (option == 3) {
listAllNames(empArray, noOfElements);
}
System.out.print("Enter Option ");
option = EasyIn.getInt();
}
}
}
答案 0 :(得分:1)
这有效地跳过索引0处的数组条目:
while (step < noOfValues)
{
step++;
更好:
public static int checkId(Customer theArray[], int noOfValues, String searchId){
for (int step = 0; step < noOfValues; ++step ){
if( theArray[step].getId().equals(searchId) )
{
return step;
}
}
return -1;
}
未找到时返回-1,否则返回索引。
我没有检查你的代码的其余部分,只是看到应该放弃使用数组而不是List。