Java:查找平均线和编号线

时间:2016-12-08 01:55:18

标签: java regex loops java.util.scanner

我一直在努力弄清楚如何在这组数据中找到所有年龄段的平均值,并对每一行进行编号。到目前为止我所做的只是让它变得难以理解。我的错误通常围绕着转换,我对如何解决这个问题感到难过。

我真的很感激帮助!

文本文件内容:

75 Fresco, Al
67 Dwyer, Barb
55 Turner, Paige
108 Peace, Warren
46 Richman, Mary A.
37 Ware, Crystal
83 Carr, Dusty
15 Sledd, Bob
64 Sutton, Oliver
70 Mellow, Marsha
29 Case, Justin
35 Time, Justin
8 Shorts, Jim
20 Morris, Hugh
25 Vader, Ella
76 Bird, Earl E.

我工作的代码是:

import java.io.*;

public class Ex2 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("people.txt"));
        while (input.hasNext()) 
        {
            String[] line = input.nextLine().replace(",", "").split("\\s+");
            String age = line[0];
            String lastName = line[1];
            String firstName = "";
            //take the rest of the input and add it to the last name
            for(int i = 2; 2 < line.length && i < line.length; i++)
                firstName += line[i] + " ";

            System.out.println(firstName + lastName + " " + age);
        }
    }
}

代码我使用失败:

import java.io.*;
import java.util.*;

public class Ex2 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("people.txt"));
        while (input.hasNext()) 
        {
            String[] line = input.nextLine().replace(",", "").split("\\s+");
            String age = line[0];
            String lastName = line[1];
            String firstName = "";
            //take the rest of the input and add it to the last name
            for(int i = 2; 2 < line.length && i < line.length; i++)
                firstName += line[i] + " ";

            for (int count = 1; line.length; count++)
                System.out.println((count+1)+ " " + line[count] + firstName + lastName + " " + age);

            int sum = 0;
            for (int j = 0; j < line.length; j++)
                sum = sum + line[j]
                        double average = sum / line.length;
            System.out.println();
            System.out.println("The average age is: " + average);
        }
    }
}

输出:

Al Fresco 75
Barb Dwyer 67
Paige Turner 55
Warren Peace 108
Mary A. Richman 46
Crystal Ware 37
Dusty Carr 83
Bob Sledd 15
Oliver Sutton 64
Marsha Mellow 70
Justin Case 29
Justin Time 35
Jim Shorts 8
Hugh Morris 20
Ella Vader 25
Earl E. Bird 76

目标输出:

1. Al Fresco 75
2. Barb Dwyer 67
3. Paige Turner 55
4. Warren Peace 108
5. Mary A. Richman 46
...

The average age is: NUMBER

2 个答案:

答案 0 :(得分:1)

哦,善良可以处理这么多方法

首先在Java中,我们尝试将事物视为对象,在这种情况下,每一行实际上都是一个对象。为了允许稍后扩展和调整代码,请从框架号

开始

所以我们有一个新的

Array<Person> people = new ArrayList<>;

所以现在我们有了一个对象  我们可以创建一个

的函数
String[] line = input.nextLine().replace(",", "").split("\\s+");
Person p = new Person(Integer.parseint(line[0]],line[1],line[2])
people.add(p);

.. for loop ...

Map<Integer,Person> people = new ListMap<>;

... 或者,您可以使用地图 ...

Integer total = 0
int i = 0
foreach(Person p: people){
   Integer total = total + p.getAge();
   i++
   System.out.Println(i + "." + p.getFirst() +" "+ p.getLast()+" "+ p.getAge())
}
Float average = Float.parseInt(total) / i;
System.out.Println("The average age is " + average); 

... 然后做 people.add(p.getAge()中,p) .... 地图可以进行各种排序和过滤,具体取决于您选择使用的地图类型。

最后,您可以使用您的对象来实现上述目标:

int  number;
Console.WriteLine("what is the value of 3 + 8?");
number = int.Parse(Console.ReadLine());
if (number == 11)
{
    Console.WriteLine("well done");
    Console.WriteLine("press enter once");
    Console.ReadLine();
}
else
{
    Console.WriteLine("its 11 u idiot!!!");
    Console.ReadLine();
}

Console.WriteLine("what is the value of 23132-23131?");
number = int.Parse(Console.ReadLine());

if (number == 1)
{
    Console.WriteLine("oh yeah!");
    Console.ReadLine();
}
else
{
    Console.WriteLine("u r such a noob!");
    Console.ReadLine();
}

答案 1 :(得分:0)

您可以使用以下代码。我已经做了一些修改,可以计算平均值。在while循环中不需要使用额外的for循环。您可以计算定义两个临时变量的平均年龄(counttotalAge)。然后在循环应用程序中可以增加计数以识别人数,并且在同一循环中,您可以将年龄添加到totalAge变量中。 (请注意,年龄应该投入Double)。然后在完成循环后,您可以通过将totalAge除以count来获得年龄。

我注意到在你的代码中,在while循环中添加了平均打印部分。它应该在while循环之外是不正确的。否则,您的程序将在每行之后打印平均线。

通过以下代码我添加了内联注释,我在那里做了更换器。

import java.io.*;
import java.util.Scanner;

public class Ex2 {
public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("people.txt"));
    // to get number of people
    int count=0;
    //to get total age
    double totalAge=0;
    while (input.hasNext()) 
    {
        String[] line = input.nextLine().replace(",", "").split("\\s+");
        String age = line[0];
        String lastName = line[1];
        String firstName = "";
        //take the rest of the input and add it to the last name
        for(int i = 2; 2 < line.length && i < line.length; i++)
            firstName += line[i] + " ";

        //Convert age string into double and add it to totalAge
        totalAge=totalAge+Double.parseDouble(age);
        System.out.println(++count+" "+firstName + lastName + " " + age);
    }
    double average = totalAge / count;
    System.out.println("The average age is: " + average);
}
}

您可以获得给定输入文件的以下输出。

1 Al Fresco 75
2 Barb Dwyer 67
3 Paige Turner 55
4 Warren Peace 108
5 Mary A. Richman 46
6 Crystal Ware 37
7 Dusty Carr 83
8 Bob Sledd 15
9 Oliver Sutton 64
10 Marsha Mellow 70
11 Justin Case 29
12 Justin Time 35
13 Jim Shorts 8
14 Hugh Morris 20
15 Ella Vader 25
16 Earl E. Bird 76
The average age is: 50.8125