Java-语法和逻辑错误,我找不到答案
Java程序运行不正常,我不知道问题出在哪里 还有构造函数,我要添加它 他们有很多问题,但我不知道如何解决,所以如果有人愿意帮忙,我会非常感激 //头文件Java-语法和逻辑错误,我找不到答案,java,arrays,Java,Arrays,Java程序运行不正常,我不知道问题出在哪里 还有构造函数,我要添加它 他们有很多问题,但我不知道如何解决,所以如果有人愿意帮忙,我会非常感激 //头文件 import java.util.*; import java.io.*; public class Phonedir { public static void main(String [] args) { String First_name, Last_name, Phone_Num; String First,
import java.util.*;
import java.io.*;
public class Phonedir
{
public static void main(String [] args)
{
String First_name, Last_name, Phone_Num;
String First, Last, Num;
int choice=0;
Scanner in = new Scanner(System.in);
LinkedList<T> listOfTs = new LinkedList<T>();
System.out.println("A Program to keep a Phone Directory:");
//looping menu
while(true)
{
Menu();
//enter the choice
if(choice==0)
System.out.println("No choice selected");
else
System.out.println(listOfTs.get(choice-1));
System.out.print("Enter the choice ");
String command = in.next().trim();
//show all entries
if(command.equalsIgnoreCase("a"))
{
System.out.println();
System.out.println(First_name+" "+Last_name+" "+Phone_Num);
//for loop
for(int i=0; i<listOfTs.size(); i++)
System.out.println(listOfRecords.get(i));
}
//Delete choice
else if(command.equalsIgnoreCase("d"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.println("Deleted: "+listOfRecords.remove(choice-1));
choice=0; //update choice counter of reference of linked list
}
}
//change first name of choice Record
else if(command.equalsIgnoreCase("f"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New first name: ");
First = in.next().trim();
listOfRecords.get(choice-1).changeFirst(First);
}
}
//Change last name of selected record
else if(command.equalsIgnoreCase("l"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New last name: ");
Last = in.next().trim();
listOfRecords.get(choice-1).changeLast(Last);
}
}
//add new contact
else if(command.equalsIgnoreCase("n"))
{
System.out.print("Enter first name: ");
First_name = in.next().trim();
System.out.print("Enter last name: ");
Last_name = in.next().trim();
//check in valid phone number input
//if phone number does not equal 10 then reprompt
while(true)
{
System.out.print("Enter phone number");
Phone_Num = in.next().trim();
if(Phone_Num.length()!=10)
System.out.println("Please enter correct phone number!");
else
break;
}
listOfRecords.add(new Record(First_name, Last_name, Phone_Num));
choice = listOfRecords.size();
}
//Change phone number
else if(command.equalsIgnoreCase("p"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("Enter new phone number: ");
Num = in.next().trim();
listOfRecords.get(choice-1).changePhone(Num);
}
}
//Exit
else if(command.equalsIgnoreCase("q"))
{
System.out.println("Quit");
System.exit(1);
}
else
if(command.equalsIgnoreCase("s"))
{
String testName;
int test=0;
System.out.print("Enter first name: ");
First_name = in.next().trim();
for(int i=0; i<listOfRecords.size(); i++)
{
testName = listOfRecords.get(i).getFirst();
if(testName.equals(First_name))
{
choice=i+1;
test++;
}
}
//print if no match found
if(test==1);
System.out.println("No Record found");
}
else
System.out.println("Choice not correct");
}
}
//print menu
static void Menu()
{
System.out.println();
System.out.println("\t(a) Show all records");
System.out.println("\t(d) Delete the current record");
System.out.println("\t(f) Change the first name in the current record");
System.out.println("\t(l) Change the last name in the current record");
System.out.println("\t(n) Add a new record");
System.out.println("\t(p) Change the phone number in the current record");
System.out.println("\t(q) Quit");
System.out.println("\t(s) Select a record from the record list to become current record");
}
}
}
//change first name of choice Record
else if(command.equalsIgnoreCase("f"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New first name: ");
First = in.next().trim();
listOfRecords.get(choice-1).changeFirst(First);
}
}
//Change last name of selected record
else if(command.equalsIgnoreCase("l"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New last name: ");
Last = in.next().trim();
listOfRecords.get(choice-1).changeLast(Last);
}
}
//add new contact
else if(command.equalsIgnoreCase("n"))
{
System.out.print("Enter first name: ");
First_name = in.next().trim();
System.out.print("Enter last name: ");
Last_name = in.next().trim();
//check in valid phone number input
//if phone number does not equal 10 then reprompt
while(true)
{
System.out.print("Enter phone number");
Phone_Num = in.next().trim();
if(Phone_Num.length()!=10)
System.out.println("Please enter correct phone number!");
else
break;
}
listOfRecords.add(new Record(First_name, Last_name, Phone_Num));
choice = listOfRecords.size();
}
//Change phone number
else if(command.equalsIgnoreCase("p"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("Enter new phone number: ");
Num = in.next().trim();
listOfRecords.get(choice-1).changePhone(Num);
}
}
//Exit
else if(command.equalsIgnoreCase("q"))
{
System.out.println("Quit");
System.exit(1);
}
else
if(command.equalsIgnoreCase("s"))
{
String testName;
int test=0;
System.out.print("Enter first name: ");
First_name = in.next().trim();
for(int i=0; i<listOfRecords.size(); i++)
{
testName = listOfRecords.get(i).getFirst();
if(testName.equals(First_name))
{
choice=i+1;
test++;
}
}
//print if no match found
if(test==1);
System.out.println("No Record found");
}
else
System.out.println("Choice not correct");
}
}
//print menu
static void Menu()
{
System.out.println();
System.out.println("\t(a) Show all records");
System.out.println("\t(d) Delete the current record");
System.out.println("\t(f) Change the first name in the current record");
System.out.println("\t(l) Change the last name in the current record");
System.out.println("\t(n) Add a new record");
System.out.println("\t(p) Change the phone number in the current record");
System.out.println("\t(q) Quit");
System.out.println("\t(s) Select a record from the record list to become current record");
}
}
import java.util.*;
导入java.io.*;
公共类Phonedir
{
公共静态void main(字符串[]args)
{
字符串First_name、Last_name、Phone_Num;
字符串First,Last,Num;
int-choice=0;
扫描仪输入=新扫描仪(系统输入);
LinkedList listOfTs=新建LinkedList();
System.out.println(“保存电话目录的程序:”);
//循环菜单
while(true)
{
菜单();
//输入选项
如果(选项==0)
System.out.println(“未选择选项”);
其他的
System.out.println(listOfTs.get(choice-1));
系统输出打印(“输入选项”);
String命令=in.next().trim();
//显示所有条目
if(command.equalsIgnoreCase(“a”))
{
System.out.println();
System.out.println(名字+姓氏+电话号码);
//for循环
对于(int i=0;i我已经更正了您的代码。我能够成功地执行并插入少量记录
下面是两个类Phonedir.java
和Record.java
Phonedir.java
package stackoverflow;
import java.util.*;
import java.io.*;
public class Phonedir
{
//print menu
static void Menu()
{
System.out.println();
System.out.println("\t(a) Show all records");
System.out.println("\t(d) Delete the current record");
System.out.println("\t(f) Change the first name in the current record");
System.out.println("\t(l) Change the last name in the current record");
System.out.println("\t(n) Add a new record");
System.out.println("\t(p) Change the phone number in the current record");
System.out.println("\t(q) Quit");
System.out.println("\t(s) Select a record from the record list to become current record");
}
public static void main(String [] args)
{
String First_name, Last_name, Phone_Num;
String First, Last, Num;
int choice=0;
Scanner in = new Scanner(System.in);
LinkedList<Record> listOfRecords = new LinkedList<Record>();
System.out.println("A Program to keep a Phone Directory:");
//looping menu
while(true)
{
Menu();
//enter the choice
if(choice==0)
System.out.println("No choice selected");
else
System.out.println(listOfRecords.get(choice-1));
System.out.print("Enter the choice ");
String command = in.next().trim();
//show all entries
if(command.equalsIgnoreCase("a"))
{
System.out.println();
//for loop
for(int i=0; i<listOfRecords.size(); i++) {
Record record = listOfRecords.get(i);
System.out.println(record.getFirstName() +" "+ record.getLastName() +" "+ record.getPhone());
}
}
//Delete choice
else if(command.equalsIgnoreCase("d"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.println("Deleted: "+listOfRecords.remove(choice-1));
choice=0; //update choice counter of reference of linked list
}
}
//change first name of choice Record
else if(command.equalsIgnoreCase("f"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New first name: ");
First = in.next().trim();
listOfRecords.get(choice-1).setFirstName(First);
}
}
//Change last name of selected record
else if(command.equalsIgnoreCase("l"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New last name: ");
Last = in.next().trim();
listOfRecords.get(choice-1).setLastName(Last);
}
}
//add new contact
else if(command.equalsIgnoreCase("n"))
{
System.out.print("Enter first name: ");
First_name = in.next().trim();
System.out.print("Enter last name: ");
Last_name = in.next().trim();
//check in valid phone number input
//if phone number does not equal 10 then reprompt
while(true)
{
System.out.print("Enter phone number");
Phone_Num = in.next().trim();
if(Phone_Num.length()!=10)
System.out.println("Please enter correct phone number!");
else
break;
}
listOfRecords.add(new Record(First_name, Last_name, Phone_Num));
choice = listOfRecords.size();
}
//Change phone number
else if(command.equalsIgnoreCase("p"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("Enter new phone number: ");
Num = in.next().trim();
listOfRecords.get(choice-1).setPhone(Num);
}
}
//Exit
else if(command.equalsIgnoreCase("q"))
{
System.out.println("Quit");
System.exit(1);
}
else if(command.equalsIgnoreCase("s"))
{
String testName;
int test=0;
System.out.print("Enter first name: ");
First_name = in.next().trim();
for(int i=0; i<listOfRecords.size(); i++)
{
testName = listOfRecords.get(i).getFirstName();
if(testName.equals(First_name))
{
choice=i+1;
test++;
}
}
}
//change first name of choice Record
else if(command.equalsIgnoreCase("f"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New first name: ");
First = in.next().trim();
listOfRecords.get(choice-1).setFirstName(First);
}
}
//Change last name of selected record
else if(command.equalsIgnoreCase("l"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New last name: ");
Last = in.next().trim();
listOfRecords.get(choice-1).setLastName(Last);
}
}
//add new contact
else if(command.equalsIgnoreCase("n"))
{
System.out.print("Enter first name: ");
First_name = in.next().trim();
System.out.print("Enter last name: ");
Last_name = in.next().trim();
//check in valid phone number input
//if phone number does not equal 10 then reprompt
while(true)
{
System.out.print("Enter phone number");
Phone_Num = in.next().trim();
if(Phone_Num.length()!=10)
System.out.println("Please enter correct phone number!");
else
break;
}
listOfRecords.add(new Record(First_name, Last_name, Phone_Num));
choice = listOfRecords.size();
}
//Change phone number
else if(command.equalsIgnoreCase("p"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("Enter new phone number: ");
Num = in.next().trim();
listOfRecords.get(choice-1).setPhone(Num);
}
}
//Exit
else if(command.equalsIgnoreCase("q"))
{
System.out.println("Quit");
System.exit(1);
}
else
if(command.equalsIgnoreCase("s"))
{
String testName;
int test=0;
System.out.print("Enter first name: ");
First_name = in.next().trim();
for(int i=0; i<listOfRecords.size(); i++)
{
testName = listOfRecords.get(i).getFirstName();
if(testName.equals(First_name))
{
choice=i+1;
test++;
}
}
//print if no match found
if(test==1);
System.out.println("No Record found");
}
else
System.out.println("Choice not correct");
}
}
}
package stackoverflow;
public class Record {
private String firstName;
private String lastName;
private String phone;
public Record(String firstName, String lastName, String phone){
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
我还不能发表评论,所以我会回答这个问题
您的代码示例可读性不强。也许您可以尝试将其缩短
你有很多课外代码。检查你的家长
类型T未在LinkedList listOfTs=new LinkedList();
我在代码中看到多个static void Menu()
方法。请为每个方法使用唯一的名称
将代码分成更小的部分
当你说“运转不正常”请具体说明。错误的具体原因是什么?或者与代码的预期行为相比,代码的行为是什么。没有人会在不知道他们在寻找什么的情况下查看您的代码。将您的程序缩减到一个更小的版本,使其正常工作,然后添加功能。您在第一个e中就吃了太多埃弗特。