我的任务是创建一个包含名字,姓氏,电话号码和年龄的多维数组程序。我目前收到错误:
线程“main”中的异常java.lang.Error:未解决的编译问题: 输入无法解决
at Lab2.<init>(Lab2.java:19)
at Lab2.main(Lab2.java:7)
** Editx2,我想我已经明白了。我理想的是想用名字和姓氏删除联系人,但我无法理解。我尝试添加一个新的sysout提示输入姓氏,就像我为名字做的那样,然后添加另一个if语句,但它最终做了一些时髦的事情。
import java.util.Scanner;
public class Lab2 {
static String[][] contacts = new String[10][4];
public static void main (String [] args) {
new Lab2();
}
public Lab2() {
String[][] contacts = new String[10][4];
Scanner input = new Scanner(System.in);
System.out.println("Welcome to my Contacts Directory. Enter a selection below: ");
while(true) {
System.out.println("1: Add a contact");
System.out.println("2: Remove a contact");
System.out.println("3: Display your contacts");
System.out.println("0: Exit the Contacts Directory");
int userChoice = input.nextInt();
switch(userChoice) {
case 1:
addContacts(contacts);
break;
case 2:
removeContacts(contacts);
break;
case 3:
displayContacts(contacts);
break;
case 0:
System.out.println("You are leaving the directory! Goodbye!");
System.exit(0);
}
}
}
private static void addContacts(String[][] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("Enter First Name");
String fName = input.nextLine();
System.out.println("Enter Last Name");
String lName = input.nextLine();
System.out.println("Enter Phone Number");
String num = input.nextLine();
System.out.println("Enter Age");
String age = input.nextLine();
for (int i = 0; i < 10; i++) {
if (contacts[i][0] == null || contacts[i][0].equals(null)) {
contacts[i][0] = fName;
contacts[i][1] = lName;
contacts[i][2] = num;
contacts[i][3] = age;
boolean Inserted = true;
break;
}
}
}
private static void removeContacts(String[][] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first name of the contact you want to remove: ");
String removeContact = input.nextLine();
if (removeContact != null) {
for (int i = 0; i < contacts.length; i++) {
for (int j = 0; j < contacts[i].length; j++) {
if (removeContact.equals(contacts[i][j])) {
contacts[i][0] = null;
contacts[i][1] = null;
contacts[i][2] = null;
contacts[i][3] = null;
break;
}
}
}
}
}
private static void displayContacts(String[][] contacts) {
for(int i = 0; i < contacts.length; i++) {
for(int j = 0; j < contacts[i].length; j++) {
System.out.println(contacts[i][j] + " ");
}
System.out.println();
}
}
}
答案 0 :(得分:-1)
您的&#34;输入&#34;变量[object]不是全局变量,它是另一个函数中的范围变量。