Java 我与我正在创建的基本联系人经理之间存在问题。
我对这段代码有一些问题。我做的第一件事显然是创建了一些数组,这些数组将保存每个联系人的所有信息,因为我正在构建一个基本的联系人管理器。我的main方法只是调用menu方法来启动序列。在我的菜单上,用户可以选择他们想要做什么。无论他们选择哪个选项,都将由他们在键盘上输入的数字决定。这反过来又会激活另一种方法 我遇到的问题如下:Java 我与我正在创建的基本联系人经理之间存在问题。,java,Java,我对这段代码有一些问题。我做的第一件事显然是创建了一些数组,这些数组将保存每个联系人的所有信息,因为我正在构建一个基本的联系人管理器。我的main方法只是调用menu方法来启动序列。在我的菜单上,用户可以选择他们想要做什么。无论他们选择哪个选项,都将由他们在键盘上输入的数字决定。这反过来又会激活另一种方法 我遇到的问题如下: 按下“1”键(查看所有联系人)后,计算机会对我上次在“2”中输入的任何联系人吐出100个空值或重复100次 虽然是的,我确实希望我的菜单在一个动作发生后重复,但它也会在瞬间
import java.io.IOException;
导入java.util.Scanner;
``公共级主枢纽{
//需要的全局变量
静态字符串[]名称=新字符串[100];
静态字符串[]编号=新字符串[100];
静态字符串[]电子邮件=新字符串[100];
静态字符串[]地址=新字符串[100];
静态字符串[]生日=新字符串[100];
静态字符串[]昵称=新字符串[100];
静态int x;
公共静态整数计数器=0;
静态扫描仪输入=新扫描仪(系统输入);
公共静态void main(字符串[]args)引发IOException{
menu();//调用menu方法启动程序
}
//保存菜单的所有信息
公共静态无效菜单()引发IOException{
int choice=-1;//从-1开始,不与索引号竞争
while(choice!=7){//while循环以从用户处获取选择
//告诉用户一切是什么,简单的输出消息
System.out.println(“\t主菜单\n”);
System.out.println(“输入您要执行的操作的相应编号。\n”);
System.out.println(“1.查看所有联系人\n2.添加联系人\n3.编辑联系人\n4.删除联系人\n5.保存联系人列表\n6.加载联系人列表\n7.退出”);
choice=Integer.valueOf(in.nextLine()).intValue();//这允许读取用户输入并用于进行选择
开关(选择){
案例1:viewAllContacts();//如果用户输入1,则调用方法查看所有联系人
break;//停止循环
案例2:addContact();//如果用户输入2,则调用方法添加联系人
break;//停止循环
案例3:editContact();//如果用户输入3,则调用方法查看联系人并选择一个进行编辑
break;//停止循环
案例4:deleteContact();//如果用户输入4,则调用方法查看联系人并选择要删除的联系人
break;//停止循环
案例5:saveContact();//如果用户输入5,则调用方法将当前联系人列表保存到文本文件中
break;//停止循环
案例6:loadContact();//如果用户输入6,则调用方法加载文本文件以将联系人输入到数组中
break;//停止循环
案例7:System.out.println(“您正在退出”);//如果用户输入7,告诉用户他要离开
break;//停止循环
默认值:System.out.println(“这不是选项之一”);//如果用户没有输入上述数字之一,它将告诉用户没有选项
}
}
}
//保存信息,一旦调用,用户将能够看到其所有联系人
公共静态无效viewAllContacts(){
while(counter0){//如果计数器大于0,打印联系人列表
System.out.println(“全名:“+name[x]);
System.out.println(“编号:“+Number[x]);
System.out.println(“电子邮件地址:“+email[x]);
System.out.println(“家庭地址:“+地址[x]);
System.out.println(“生日:+生日[x]);
System.out.println(“昵称:“+昵称[x]);
System.out.println(“”;//空格,这样联系人列表就更漂亮了
计数器++;
}否则{
System.out.println(“您的列表中还没有联系人。”);//否则告诉用户没有联系人
}
}
}
//允许用户添加联系人和所有信息
public static void addContact()引发IOException{
//只要计数器小于101,就可以添加联系人
if(counter问题是使用x
作为数组的索引,但是x
从未递增
当您递增计数器时
试一试
同样,如果您添加了一个联系人
整理你的索引。看,我也这么想,但无论我走哪条路,无论是计数器还是x,我始终都会得到整个“null”。如果你有一个变量保存最大插入记录量,则不会。此外,你还可以测试并跳过记录。如果为null,计数器在读取结束时应重置为0。
import java.io.IOException;
import java.util.Scanner;
``public class MainHub {
// global variables that will be needed
static String[] name = new String[100];
static String[] number = new String[100];
static String[] email = new String[100];
static String[] address = new String[100];
static String[] birthday = new String[100];
static String[] nickname = new String[100];
static int x;
public static int counter = 0;
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws IOException{
menu(); // call menu method to start program
}
//Holds all the information for the menu
public static void menu() throws IOException{
int choice = -1; //start at -1 as to not compete with an index number
while (choice != 7){ //while loop to get a choice from user
//telling the user what everything is, simple output message
System.out.println("\t Main Menu\n");
System.out.println("Enter the corrosponding number for what you want to do.\n");
System.out.println("1.View all contacts\n2.Add contact\n3.Edit contact\n4.Delete contact\n5.Save contact list\n6.Load contact list\n7.Exit");
choice = Integer.valueOf(in.nextLine()).intValue(); //this allows the user input to be read and used to make a choice
switch(choice){
case 1: viewAllContacts(); //if user inputs 1, call method to view all the contacts
break; //stop the loop
case 2: addContact(); //if user inputs 2, call method to add a contact
break; //stop the loop
case 3: editContact(); // if user inputs 3, call method to view contacts and choose one to edit
break; //stop the loop
case 4: deleteContact(); // if user inputs 4, call method to view contacts and choose one to delete
break; //stop the loop
case 5: saveContact(); // if user inputs 5, call method to save current contact list into a text file
break; //stop the loop
case 6: loadContact(); // if user inputs 6, call method to load a text file to input contacts into array
break; //stop the loop
case 7: System.out.println("You are exiting"); // if user inputs 7, tell user he is leaving
break; //stops the loop
default: System.out.println("That is not one of the options."); // if user doesn't input one of above numbers, it will tell user not an option
}
}
}
//holds information, once called upon the user will be able to see all their contacts
public static void viewAllContacts(){
while(counter<100){ //while the counter has less than 101 it will print out the full list of contacts
if(counter>0){ //if counter is greater than 0 print list of contacts
System.out.println("Full name: " +name[x]);
System.out.println("Number: " +number[x]);
System.out.println("E-mail Address: " +email[x]);
System.out.println("Home Address: " +address[x]);
System.out.println("Birthday: " +birthday[x]);
System.out.println("Nickname: " +nickname[x]);
System.out.println(" "); //space so that way the contact list is a bit prettier
counter++;
}else{
System.out.println("There are no contacts in your list yet."); //else tell user there is no contacts
}
}
}
//lets the user add a contact and all the information
public static void addContact() throws IOException{
//as long as the counter is less than 101 you can add contacts
if(counter<100){
System.out.println("Enter Contact's Full Name: "); //allows user to add a name to contact
name[x] = in.nextLine(); //whatever is typed will be added into name variable
System.out.println("Enter Contact's Number: "); //allows user to add a number to contact
number[x] = in.nextLine(); //whatever is typed will be added into number variable
System.out.println("Enter Contact's E-mail Address: "); //allows user to add an E-mail address to contact
email[x] = in.nextLine(); //whatever is typed will be added into email variable
System.out.println("Enter Contact's Home Address: "); //allows user to add a home address to contact
address[x] = in.nextLine(); //whatever is typed will be added into address variable
System.out.println("Enter Contact's Birthday: "); //allows user to add a birthday to contact
birthday[x] = in.nextLine(); //whatever is typed will be added into birthday variable
System.out.println("Enter Contact's Nickname: "); //allows user to add a nickname to contact
nickname[x] = in.nextLine(); //whatever is typed will be added into nickname variable
counter++; //adds 1 to counter to allow space for next contact in arrays
}else{
System.out.println("Sorry, the contact list is full."); //if counter is 101 it will print the contact list is full
}
System.out.println("Your contact has been added");
}
System.out.println("Full name: " +name[counter]);