gpt4 book ai didi

Java:查找平均线和编号线

转载 作者:行者123 更新时间:2023-11-30 10:35:38 25 4
gpt4 key购买 nike

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

非常感谢您的帮助!

文本文件内容:

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

最佳答案

天啊,有这么多方法可以解决这个问题

首先,在 Java 中,我们尝试将事物视为对象,在这种情况下,每一行实际上都是一个对象。为了让代码可以在后面扩展和调整,从一个框架开始 no

所以我们有一个新的

Public Person(
private int age;
private String lastName;
private Sring firstName;

Public Person(int age, String lName, String fName){
this.age = age;
this.lastName = lname;
this.firstName = fName;
}
//add getters here.
)

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

Array<Person> people = new ArrayList<>;

..for 循环...

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

...或者你可以使用 map ...

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

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

最后您可以处理您的对象以实现上述目标:

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

关于Java:查找平均线和编号线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com