我在使用这段代码时遇到了一些问题。我做的第一件事显然是创建了一些数组,这些数组将保存每个联系人的所有信息,因为我正在建立一个基本的联系人管理器。我的主要方法只是调用我的menu方法来启动序列。在我的菜单中,用户可以选择他们想要做什么。他们选择的选择取决于他们在键盘上输入的数字。反过来,将激活不同的方法。
我遇到的问题如下:
按“1”查看所有联系人后,计算机会向“2”中吐出100个空值或100次重复输入,添加联系人。
虽然是的,我确实希望我的菜单在动作发生后重复播放,它会立即完成。例如,一旦我按下“1”后所有重复发生,它就会直接回到主菜单,并且很难以这种方式读取所有内容。
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");
}
答案 0 :(得分:3)
问题在于您使用x
作为数组的索引,但x
永远不会递增。
正在递增counter
试
System.out.println("Full name: " +name[counter]);
同样添加了contact
整理索引。