我在弄清楚如何处理这段代码时遇到了问题。我试图提示用户输入一个人的详细信息(如姓名、年龄和电话号码),然后输出所有个人信息。每当输入另一个人的信息时,就会输出手头的人的信息以及当前人的信息。
应该是这样的:
我的输入:Adam,252345678,法国
输出:Adam,252345678,法国
我的第二次输入:Bob,222345678,澳大利亚
第二次输出:
Adam, 25, 12345678, France
Bob, 22, 12345678, Australia
以此类推,说10次,这将是第十次创建包含所有细节的表格。有什么想法可以让我每次都创建一个包含更多细节的表格吗?我试过用这种方法来存储变量,但循环数组可能更好?我是Java的新手,因为我最近才学会它。任何例子都将不胜感激。非常感谢!
Scanner scan = new Scanner(System.in);
scan.useDelimiter(",");
String sName = scan.next();
System.out.println(header);
String sLast = scan.next();
double sData1 = scan.nextDouble();
double sData2 = scan.nextDouble();
double sData3 = scan.nextDouble();
int sData4 = scan.nextInt();
当我回到电脑上时,我会完全检查。感谢您的快速回复。
我认为最好的方法之一是创建一个person类,然后将每个人添加到列表中,在每个条目之后打印整个列表。您会发现创建Person类并为其实现toString()方法最简单:
public class TestCode {
public static void main (String args []) {
Scanner input = new Scanner(System.in);
List<Person> peopleList = new ArrayList<Person>();
for(int i = 0; i < 10; i++){
System.out.println("enter info: ");
String name = input.next();
String age = input.next();
String phone = input.next();
String location = input.next();
peopleList.add(new Person(name, Integer.parseInt(age), Integer.parseInt(phone), location));
for(Person p: peopleList)
System.out.println(p.toString());
}
}
}
class Person{
String name;
int age;
int phoneNumber;
String location;
Person(String n, int a, int p, String l){
this.name = n;
this.age = a;
this.phoneNumber = p;
this.location = l;
}
public String toString(){
return(name + " " + age + " " + phoneNumber + " " + location);
}
}
类似这样的东西,尽管这个例子不包括从你的输入中删除逗号。如果选择采用此方法,则应在将值存储到People类之前处理从输入中删除逗号的问题。
步骤1。创建一个类来保存关于一个人的数据,包括最终字段、合适的构造函数和toString()
方法:
public class Person {
final String name, country;
final int age, phone;
public Person(String name, int age, int phone, String country) {
this.name = name;
this.age = age;
this.phone = phone;
this.country = country;
}
public String toString() {
return name + ", " + age + ", " + phone + ", " + country;
}
// getters omitted for brevity
}
步骤2:创建这些东西的列表:
List<Person> people = new ArrayList<Person>();
步骤3:扫描4个单独的值,然后创建一个人,然后将其添加到列表中:
String name = scanner.next(); // etc for the other values
Person person = new Person(name, age, phone, country);
people.add(person);
准备打印时:
for (Person person : people) {
System.out.println(person);
}
您可以使用scan.nextLine()获取每个人的信息,然后使用List存储所有人的信息。最后,打印列表中存储的所有人员信息。
通过这种方式,您可以一次输入一个人的信息,例如,在控制台中输入Adam,2512345678,France。
下面是一个例子:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
List<String> personInformation = new ArrayList<String>();
String line = null;
while (!"exit".equals(line = scan.nextLine())) {
personInformation.add(line);
printPersonInformation(personInformation);
}
}
private static void printPersonInformation(List<String> personInformation) {
System.out
.println("All the person information we have now is as follows:");
for (String personInfo : personInformation) {
System.out.println(personInfo);
}
}
您可以使用两个扫描仪,一个用来分隔行,另一个用逗号分隔,然后将正在扫描的数据构建为列表列表,然后对其进行迭代以打印出来。像这样的东西:
private static void processRecords() throws FileNotFoundException
{
List<List<Object>> recordList = new ArrayList<List<Object>>();
// scan data line by line
Scanner scanner = new Scanner(new File("/tmp/foo.txt"));
while(scanner.hasNextLine())
{
List<Object> record = new ArrayList<Object>();
String line = scanner.nextLine();
//remove all spaces, since nextDouble and nextInt
//will choke on them:
line = line.replaceAll(" ", "");
//create a scanner just for this line
Scanner recordScanner = new Scanner(line);
recordScanner.useDelimiter(",");
String sData1 = recordScanner.next();
double sData2 = recordScanner.nextDouble();
double sData3 = recordScanner.nextDouble();
String sData4 = recordScanner.next();
record.add(sData1);
record.add(sData2);
record.add(sData3);
record.add(sData4);
// add the record to the list.
recordList.add(record);
//print it out
printHeader(recordList);
}
}
private static void printHeader(List<List<Object>> recordList)
{
//print it out
for (List<Object> recordToPrint : recordList)
{
for (Object objToPrint : recordToPrint)
{
//print your records.
System.out.print(objToPrint);
System.out.print(",");
}
System.out.println();
}
}
注意:我已经更改了代码以匹配您的数据-您提供的代码示例与您提供的数据不完全匹配。