程序没有调用正确的方法,一直循环回到第一个菜单。 (Java)
Program does not call proper method, keeps looping back to first menu. (Java)
好的,我有一个充当联系人列表的小程序。它由一个二维数组构成,需要用户输入以填充它总共 10 个联系人;因此,数组为 10 行 4 列,由名字、姓氏、phone 号码和年龄组成。
有一个菜单让用户可以选择 1) 添加联系人,2) 删除联系人,3) 显示联系人,以及 4) 退出程序。我知道这可能看起来像一种粗暴的蛮力方法,但要求我们不要使用我们目前所学的任何东西,其中不包括 ArrayList、LinkedList、Collections、HashMaps 等方法。
我的问题是程序在我删除联系人之前工作正常。当我再次获得菜单时,我想重新添加联系人,它只是循环回到菜单。它必须是某种小语法错误,但我不确定在哪里。我将展示代码以及输出,以了解它从哪里开始出现问题。
*注意:我本身并不是要代码,而是要让我意识到哪里出错的建议和指导。
代码:
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
new ContactList ();
}
public ContactList () {
Scanner input = new Scanner(System.in);
String[][] contactList = new String[10][4];
System.out.println("Welcome to your Contact organizer.");
System.out.println("You can store " + contactList.length + " contacts. " );
System.out.println("Please enter your choice from the list below: ");
System.out.println();
while(true){
System.out.println("1: Add a Contact"); //the user options
System.out.println("2: Remove a Contact");
System.out.println("3: Display your Contacts");
System.out.println("4: Exit this program");
int userSelection = input.nextInt();
switch(userSelection) {
case 1:
addContact(contactList);
break;
case 2:
removeContact(contactList);
break;
case 3:
displayContacts(contactList);
break;
case 4:
System.out.println("Goodbye.");
System.exit(0);
}
}
}
private void addContact(String contactList[][]) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < contactList.length; i++) {
if(contactList[i][0] == null) {
System.out.print("Enter the contact's first name: ");
contactList[i][0] = input.nextLine().trim(); //.trim() eliminates any accidental white spaces
System.out.print("Enter the contact's last name: ");
contactList[i][1] = input.nextLine().trim();
System.out.print("Enter the contact's phone number: ");
contactList[i][2] = input.nextLine().trim();
System.out.print("Enter the contact's age: ");
contactList[i][3] = input.nextLine().trim();
break;
}
}
}//end add method
private void removeContact(String contactList[][]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name of contact you wish to remove: ");
String deleteFirst = input.nextLine().trim();
System.out.println("Enter the last name of contact you wish to remove: ");
String deleteLast = input.nextLine().trim();
for(int i = 0; i < contactList.length; i++) {
for(int j = 0; j < contactList[i].length; j++) {
if(deleteFirst.equals(contactList[i][0]) && deleteLast.equals(contactList[i][1])) {
System.out.println("Contact successfully removed. \n \n");
contactList[i][0] = null;
contactList[i][1] = null;
contactList[i][2] = null;
contactList[i][3] = null;
break;
}
}
}
}//end remove method
private void displayContacts(String contactList[][]) {
for(int i = 0; i < contactList.length; i++) {
for(int j = 0; j < contactList[i].length; j++) {
if(contactList[i][j] == null) {
contactList[i][j] = (" "); //if user displays before all contacts are entered, it will display blank rather than null null null, etc.
}
System.out.print(contactList[i][j] + " ");
}
System.out.println();
}
}//end display method
}//end class
我知道 removeContact 方法可能看起来很糟糕,但这是我知道如何删除整行的唯一方法。我一直在想我应该用那种方法创建一个新数组,但我还不知道怎么做。
这是输出:
Welcome to your Contact organizer.
You can store 10 contacts.
Please enter your choice from the list below:
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
Enter the contact's first name: John
Enter the contact's last name: Doe
Enter the contact's phone number: 1234567
Enter the contact's age: 34
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
Enter the contact's first name: Joe
Enter the contact's last name: Bob
Enter the contact's phone number: 4567890
Enter the contact's age: 30
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
3
John Doe 1234567 34
Joe Bob 4567890 30 --contacts display just fine so far
--I know there are a lot of spaces since technically there is
still data here, albeit blank spaces. Perhaps I need to make it a
new array each time something is added? I'll have to figure out
how to do that. For now, it looked far better than:
null null null null
null null null null
etc...
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
2
Enter the first name of contact you wish to remove:
Joe
Enter the last name of contact you wish to remove:
Bob
Contact successfully removed.
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
3
John Doe 1234567 34 --output displays contact removed
1: Add a Contact --and this is where is begins to mess up.
2: Remove a Contact Why now?
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
3
John Doe 1234567 34 --I can still display contacts. ????
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
4
Goodbye. --and I can exit. I just can no longer add.
Process finished with exit code 0
我在这里发现了一个问题:
if(contactList[i][j] == null) {
contactList[i][j] = (" "); //if user displays before all contacts are entered, it will display blank rather than null null null, etc.
}
我将其更改为仅在联系人不为空时才显示联系人的位置,如下所示:
if(contactList[i][j] != null) {
System.out.print(contactList[i][j] + " ");
}
我现在可以return到我的菜单并添加另一个联系人。我希望输出没有留下空的 space,但我会自己解决这个问题,因为它与我原来的 post 无关。
虽然我不知道为什么会出现这个问题(因为我没有收到任何答复或评论,只有毫无意义的反对票),但我至少解决了我的问题。
我唯一的猜测是,当我将联系人分配给 null,然后将该 null 值分配给 whitespace (" ") 时,程序认为它在技术上仍然是满的,因为数组现在充满了 unicode spaces ("\u0020") 的值。因此,我对输出美学的尝试在程序中造成了中断。
好的,我有一个充当联系人列表的小程序。它由一个二维数组构成,需要用户输入以填充它总共 10 个联系人;因此,数组为 10 行 4 列,由名字、姓氏、phone 号码和年龄组成。 有一个菜单让用户可以选择 1) 添加联系人,2) 删除联系人,3) 显示联系人,以及 4) 退出程序。我知道这可能看起来像一种粗暴的蛮力方法,但要求我们不要使用我们目前所学的任何东西,其中不包括 ArrayList、LinkedList、Collections、HashMaps 等方法。 我的问题是程序在我删除联系人之前工作正常。当我再次获得菜单时,我想重新添加联系人,它只是循环回到菜单。它必须是某种小语法错误,但我不确定在哪里。我将展示代码以及输出,以了解它从哪里开始出现问题。 *注意:我本身并不是要代码,而是要让我意识到哪里出错的建议和指导。
代码:
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
new ContactList ();
}
public ContactList () {
Scanner input = new Scanner(System.in);
String[][] contactList = new String[10][4];
System.out.println("Welcome to your Contact organizer.");
System.out.println("You can store " + contactList.length + " contacts. " );
System.out.println("Please enter your choice from the list below: ");
System.out.println();
while(true){
System.out.println("1: Add a Contact"); //the user options
System.out.println("2: Remove a Contact");
System.out.println("3: Display your Contacts");
System.out.println("4: Exit this program");
int userSelection = input.nextInt();
switch(userSelection) {
case 1:
addContact(contactList);
break;
case 2:
removeContact(contactList);
break;
case 3:
displayContacts(contactList);
break;
case 4:
System.out.println("Goodbye.");
System.exit(0);
}
}
}
private void addContact(String contactList[][]) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < contactList.length; i++) {
if(contactList[i][0] == null) {
System.out.print("Enter the contact's first name: ");
contactList[i][0] = input.nextLine().trim(); //.trim() eliminates any accidental white spaces
System.out.print("Enter the contact's last name: ");
contactList[i][1] = input.nextLine().trim();
System.out.print("Enter the contact's phone number: ");
contactList[i][2] = input.nextLine().trim();
System.out.print("Enter the contact's age: ");
contactList[i][3] = input.nextLine().trim();
break;
}
}
}//end add method
private void removeContact(String contactList[][]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name of contact you wish to remove: ");
String deleteFirst = input.nextLine().trim();
System.out.println("Enter the last name of contact you wish to remove: ");
String deleteLast = input.nextLine().trim();
for(int i = 0; i < contactList.length; i++) {
for(int j = 0; j < contactList[i].length; j++) {
if(deleteFirst.equals(contactList[i][0]) && deleteLast.equals(contactList[i][1])) {
System.out.println("Contact successfully removed. \n \n");
contactList[i][0] = null;
contactList[i][1] = null;
contactList[i][2] = null;
contactList[i][3] = null;
break;
}
}
}
}//end remove method
private void displayContacts(String contactList[][]) {
for(int i = 0; i < contactList.length; i++) {
for(int j = 0; j < contactList[i].length; j++) {
if(contactList[i][j] == null) {
contactList[i][j] = (" "); //if user displays before all contacts are entered, it will display blank rather than null null null, etc.
}
System.out.print(contactList[i][j] + " ");
}
System.out.println();
}
}//end display method
}//end class
我知道 removeContact 方法可能看起来很糟糕,但这是我知道如何删除整行的唯一方法。我一直在想我应该用那种方法创建一个新数组,但我还不知道怎么做。
这是输出:
Welcome to your Contact organizer.
You can store 10 contacts.
Please enter your choice from the list below:
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
Enter the contact's first name: John
Enter the contact's last name: Doe
Enter the contact's phone number: 1234567
Enter the contact's age: 34
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
Enter the contact's first name: Joe
Enter the contact's last name: Bob
Enter the contact's phone number: 4567890
Enter the contact's age: 30
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
3
John Doe 1234567 34
Joe Bob 4567890 30 --contacts display just fine so far
--I know there are a lot of spaces since technically there is
still data here, albeit blank spaces. Perhaps I need to make it a
new array each time something is added? I'll have to figure out
how to do that. For now, it looked far better than:
null null null null
null null null null
etc...
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
2
Enter the first name of contact you wish to remove:
Joe
Enter the last name of contact you wish to remove:
Bob
Contact successfully removed.
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
3
John Doe 1234567 34 --output displays contact removed
1: Add a Contact --and this is where is begins to mess up.
2: Remove a Contact Why now?
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
3
John Doe 1234567 34 --I can still display contacts. ????
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
1
1: Add a Contact
2: Remove a Contact
3: Display your Contacts
4: Exit this program
4
Goodbye. --and I can exit. I just can no longer add.
Process finished with exit code 0
我在这里发现了一个问题:
if(contactList[i][j] == null) {
contactList[i][j] = (" "); //if user displays before all contacts are entered, it will display blank rather than null null null, etc.
}
我将其更改为仅在联系人不为空时才显示联系人的位置,如下所示:
if(contactList[i][j] != null) {
System.out.print(contactList[i][j] + " ");
}
我现在可以return到我的菜单并添加另一个联系人。我希望输出没有留下空的 space,但我会自己解决这个问题,因为它与我原来的 post 无关。
虽然我不知道为什么会出现这个问题(因为我没有收到任何答复或评论,只有毫无意义的反对票),但我至少解决了我的问题。 我唯一的猜测是,当我将联系人分配给 null,然后将该 null 值分配给 whitespace (" ") 时,程序认为它在技术上仍然是满的,因为数组现在充满了 unicode spaces ("\u0020") 的值。因此,我对输出美学的尝试在程序中造成了中断。