我正在编写一个菜单,将姓名存储到数组中,然后给出添加/删除学生和搜索特定学生的选项,但我似乎找不到如何设置数组,以便在其他选项中使用它,例如,我的代码只允许我在使用选项1时输入姓名,但当我选择选项3在数组中搜索姓名时,它不会将这些姓名保留在数组中,它只是在数组中的每个位置显示为null。
我遇到的另一个问题是如何删除学生,显然,如果名称在数组的末尾,这将非常容易,但如果名称在中间,我如何能够删除该名称,将所有其他名称下移一个槽,以便在数组中没有间隙?
import java.util.Scanner;
public class Lab10Ex2 {
public static void main(String[] args) {
int choice = 0;
int[] stringArray = {};
do{
String[] stringarray = new String[20];
System.out.println("----------------MENU---------------");
System.out.println();
System.out.println("1. Add Students");
System.out.println("2. Delete Students");
System.out.println("3. Search for student");
System.out.println("4. Print all Students");
System.out.println("5. exit");
Scanner scanchoice = new Scanner(System.in);
choice = scanchoice.nextInt();
if (choice ==1){
Scanner scannames = new Scanner(System.in);
System.out.println("Please enter the student names into the array");
int i = 0;
for(i = 0; i<stringarray.length; i++){
String temp =scannames.nextLine();
stringarray[i]=temp.toLowerCase();
if(i==(stringarray.length-1)){
System.out.println("successfully filled up array fully");
System.out.println();
}
}
}
if(choice==2){
}
if(choice==3){
for(int p = 0; p<stringarray.length; p++){
System.out.println(stringarray[p]);
}
int x=0;
Scanner scannames1 = new Scanner(System.in);
System.out.println("Enter name of student you want to search for: ");
System.out.println();
String search=scannames1.nextLine();
String searchName=search.toLowerCase();
for(int p=0;p<20;p++){
if(searchName.equals(stringarray[p])){
x=1;
}
else{
x=0;
}
}
if(x==1){
System.out.println("We have a match in our database for "+ searchName);
}
else if (x==0){
System.out.println("No match for "+ searchName);
}
}
if (choice ==4){
System.out.println("List of names:");
for(int p = 0; p<stringarray.length; p++){
System.out.println(stringarray[p]);
}
}
}while(choice!=5);
}
}发布于 2012-05-11 00:34:02
以下是更正后的代码:
import java.util.Scanner;
public class Lab10Ex2 {
/**
* @param args
*/
public static void main(String[] args) {
int choice = 0;
int[] stringArray = {};
String[] stringarray = new String[20];
do{
System.out.println("----------------MENU---------------");
System.out.println();
System.out.println("1. Add Students");
System.out.println("2. Delete Students");
System.out.println("3. Search for student");
System.out.println("4. Print all Students");
System.out.println("5. exit");
Scanner scanchoice = new Scanner(System.in);
choice = scanchoice.nextInt();
if (choice ==1){
Scanner scannames = new Scanner(System.in);
System.out.println("Please enter the student names into the array");
System.out.println();
int i = 0;
for(i = 0; i<stringarray.length; i++){
String temp =scannames.nextLine();
stringarray[i]=temp.toLowerCase();
if(i==(stringarray.length-1)){
System.out.println("successfully filled up array fully");
System.out.println();
}
}
}
if(choice==2){
}
if(choice==3){
for(int p = 0; p<stringarray.length; p++){
System.out.println(stringarray[p]);
}
int x=0;
Scanner scannames1 = new Scanner(System.in);
System.out.println("Enter name of student you want to search for: ");
System.out.println();
String search=scannames1.nextLine();
String searchName=search.toLowerCase();
for(int p = 0; p < stringarray.length ;p++){
if(searchName.equals(stringarray[p])){
x=1;
break;
}
else{
x=0;
}
}
if(x==1){
System.out.println("We have a match in our database for "+ searchName);
}
else if (x==0){
System.out.println("No match for "+ searchName);
}
}
if (choice ==4){
System.out.println("List of names:");
for(int p = 0; p<stringarray.length; p++){
System.out.println(stringarray[p]);
System.out.println();
}
}
}while(choice!=5);
}
}你做错了的事情:
在do while循环中实例化数组的
如果希望在删除后避免浪费数组中的空间,请使用ArrayList。如果你使用简单的数组来绑定它,你可以这样做:
将null放在索引中,您可以从中deleted.Create一个临时数组,该数组的大小与包含所有空值的原始数组的大小相同。将原始数组中的所有元素复制到临时数组中,但跳过为空的元素。将原始数组指向新数组。
并避免硬编码数组长度!
发布于 2012-05-11 00:16:24
int choice = 0;
int[] stringArray = {};
do{
String[] stringarray = new String[20];删除int[] stringArray行(您在任何地方都不会引用它)。
将String[] stringarray向上移动到循环之外。
至于删除,您要么自己编写代码(在数组中将删除项上移一个),要么使用Java提供的集合类之一(而不是本机数组),这些集合类将为您处理删除。
发布于 2012-05-11 00:19:16
do{
String[] stringarray = new String[20];在Do { ... }循环的每次迭代中,您都会重新创建clearing数组变量,从而将其清除。如果您将其移到循环之外,您的学生条目将得到维护。
至于删除,如果不需要使用字符串数组,我建议使用ArrayList。它将允许您轻松地删除特定的条目,而不必担心其他条目。否则,是的,最简单的做法是将所有其他条目向下移动一个插槽,以避免间隙
https://stackoverflow.com/questions/10538042
复制相似问题