0

我在弄清楚如何处理这段代码时遇到了问题。我试图提示用户提供一个人的详细信息(例如姓名、年龄和电话号码),然后输出所有人员信息。每当输入另一个人的信息时,就会输出之前那个人的信息以及当前那个人的信息。

应该是这样的:

我的意见:Adam,25 岁​​,12345678,法国

输出:Adam, 25, 12345678, France

我的第二次输入:Bob, 22, 12345678, Australia

第二次输出:

               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();

当我回到我的电脑上时会全面检查。感谢您的快速回复。

4

4 回答 4

1

我认为解决这个问题的最佳方法之一是可能创建一个人员类,然后将每个人添加到一个列表中,在每个条目之后打印整个列表。你会发现创建一个 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 类之前处理从输入中删除逗号。

于 2013-08-26T04:08:30.413 回答
0

您可以使用两个扫描仪,一个用于分隔行,另一个用逗号分隔,然后将您正在扫描的数据构建为列表列表,然后对其进行迭代以将其打印出来。像这样的东西:

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();
  }
}

注意:我已更改代码以匹配您的数据 - 您提供的代码示例与您提供的数据不太匹配。

于 2013-08-26T03:53:24.970 回答
0

您可以使用 scan.nextLine() 获取每个人的信息,然后使用 List 存储所有人员信息。最后,打印存储在 List 中的所有人员信息。

这样就可以一次输入一个人的信息,比如在控制台输入Adam, 25, 12345678, 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);
    }
}
于 2013-08-26T04:03:43.723 回答
0

步骤 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);
}
于 2013-08-26T04:09:48.543 回答