我是一名初学Java程序员,我正在编写一个方法,该方法接受一个名为id的整数值,并根据存储在链表中的对象中的整数值进行检查。这意味着它需要整数输入。但是,我还希望用户在提示符下键入x并使方法停止并返回到程序主菜单。问题是虽然我不知道如何从一个输入中做到这一点。这是我目前的代码,尽我所能。如果您需要程序的其余部分来帮助我添加它。我只是把事情做得很少,以帮助发现问题。
我在运行时也会遇到问题。即使输入现有Id,它也不会返回正数。此外,我当前的实现意味着用户必须输入两次,一次满足while条件,另一次实际搜索链表。我相信有更好的方法,但是我的知识还没有延伸到那么远。
以下是提示用户输入将在链接列表中搜索匹配项的整数的方法,它是Client类的一部分。任何帮助表示赞赏。
public void getClient() {
System.out.println("Please enter client Id");
char choice;
int searchid = In.nextInt();
boolean exists = false;
while ((choice = In.nextChar()) !='x' ){
for (Person i:clients)
if (i.getId() == searchid){
exists = true;
System.out.println("found client");
}
if (exists == false)
System.out.println("No such client");
}
}
这是管理输入的类
import java.util.*;
public class In
{ private static Scanner in = new Scanner(System.in);
public static String nextLine()
{ return in.nextLine(); }
public static char nextChar()
{ return in.nextLine().charAt(0); }
public static int nextInt()
{ int i = in.nextInt();
in.nextLine();
return i; }
}
如果有帮助,这里是Client类
import java.io.*;
import java.util.*;
public class Clients implements Serializable
{ private LinkedList<Person> clients = new LinkedList<Person>();
private int id = 1;
public Clients()
{ clients.add(new Person("Ben", id++));
clients.add(new Person("Jerry", id++));
}
public void makePerson(){
clients.add(createPerson());
}
public void removeClient() {
String searchname = readName();
boolean removed = false;
for (Person i:clients)
if (i.matches(searchname)){
clients.remove(i);
removed = true;
}
if (removed == false)
System.out.println(" No such client");
}
public void getClient() {
System.out.println("Please enter client Id");
char choice;
int searchid = In.nextLine();
Integer.parseInt(choice);
boolean exists = false;
while ((choice = In.nextChar()) !='x' ){
for (Person i:clients)
if (i.getId() == searchid){
exists = true;
System.out.println("found client");
}
if (exists == false)
System.out.println(" No such client");
}
}
public Person createPerson()
{
String name = readName();
Person p = new Person(name, id++);
System.out.println(" Hi " + p.getName() + ", " + "you are Car Owner" + p.getId());
return p;
}
private String readName() {
System.out.print(" Name: ");
return In.nextLine();
}
public void showClients() {
for (Person i: clients)
System.out.println(i);
}
}
这里也是Person类
import java.io.*;
import java.text.*;
public class Person implements Serializable
{ private String name;
private int id;
private double cash = 100.00;
private int start = 0;
private int end = 0;
private double charge = 0;
public Person(String name, int id)
{ this.name = name;
this.id = id + 100; }
public boolean uses(int stop)
{ return getsOn(stop) || getsOff(stop); }
public boolean getsOn(int stop)
{ return start == stop; }
public boolean getsOff(int stop)
{ return end == stop; }
public String handle()
{ return name + id; }
public double readCash() {
return this.cash;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
String result = " " + this.name + " (Scuba" + this.id + ") has " + formatted(this.cash);
return result;
}
private String formatted(double amount)
{ DecimalFormat twoD = new DecimalFormat("$###,##0.00");
return twoD.format(amount); }
public boolean matches(String name)
{ return this.name.equals(name) || name.equals(this.name + this.id); }
}