获取并显示数组列表中的最高分数和名称。

huangapple go评论67阅读模式
英文:

Getting & displaying the highest score & names from an arraylist

问题

import java.util.*;

public class test3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name, gender;
        int score;
        ArrayList<String> nameArray = new ArrayList<String>();
        ArrayList<Integer> scoreArray = new ArrayList<Integer>();
        ArrayList<String> genderArray = new ArrayList<String>();

        while (true) {
            System.out.print("Enter name: ");
            name = input.next();
            if (name.equalsIgnoreCase("Q")) {
                break;
            } else {
                System.out.print("Enter Score: ");
                score = input.nextInt();
                System.out.print("Gender: ");
                gender = input.next();
                System.out.println("");
                genderArray.add(gender);
                scoreArray.add(score);
                nameArray.add(name);
            }
        }

        // Find the highest score and its index
        int highestScore = Integer.MIN_VALUE;
        int highestScoreIndex = -1;

        for (int i = 0; i < scoreArray.size(); i++) {
            if (scoreArray.get(i) > highestScore) {
                highestScore = scoreArray.get(i);
                highestScoreIndex = i;
            }
        }

        // Display the highest score and corresponding information
        if (highestScoreIndex != -1) {
            String highestScoreName = nameArray.get(highestScoreIndex);
            String highestScoreGender = genderArray.get(highestScoreIndex);
            System.out.println("Highest score is " + highestScoreName + ", with a score of " +
                               highestScore + ", is a " + highestScoreGender + ".");
        }
    }
}
英文:

i am new to programming, and to java. I couldnt figure out how arraylist works.<br>
Here are my current code:

import java.util.*;
public class test3
{
public static void main(String[] args) 
{
Scanner input = new Scanner(System.in);
String name,gender;
int score;
ArrayList&lt;String&gt; nameArray = new ArrayList&lt;String&gt;();
ArrayList&lt;Integer&gt; scoreArray = new ArrayList&lt;Integer&gt;();
ArrayList&lt;String&gt; genderArray = new ArrayList&lt;String&gt;();
while (true)
{
System.out.print(&quot;Enter name: &quot;);
name =  input.next();
if (name.equalsIgnoreCase(&quot;Q&quot;))
{
break;
}
else
{
System.out.print(&quot;Enter Score: &quot;);
score = input.nextInt();   
System.out.print(&quot;Gender: &quot;);
gender = input.next();
System.out.println(&quot;&quot;);
genderArray.add(gender);       
scoreArray.add(score);
nameArray.add(name);
}
}
///After Quitting loop, how to display the highest score &amp; the names?
}
}

After quitting the loop, i need the program to get and display the highest score along with the names and gender. I couldnt figure out how to do this with arraylist.

Example of what i want the code to output

> Enter name: Alfred<br> Enter Score: 60<br> Gender: Male<br>
>
> Enter name: Tina<br> Enter Score: 86<br> Gender: Female<br>
>
> Enter name: Ben <br> Enter Score: 95 <br> Gender: Male <br>
>
> Enter name: q<br> Highest score is Ben, with a score of 95, is a Male.

Sorry if i dont explain my question well, if anyone could help i would be gratefull!
Thanks

答案1

得分: 2

以下是翻译好的内容:

最好创建一个类来存储每个人的信息

class Person {
    String name;
    Integer score;
    String gender;
    // 构造函数,获取器,设置器
}

然后创建一个 Person 的 ArrayList

ArrayList<Person> list = new ArrayList<>();

然后在循环中输入后可以添加到列表中

list.add(new Person(name, score, gender));

您可以使用带有比较器的 Collections.max 来获取最高分的人员信息

Person maxScoredPerson = Collections.max(list, Comparator.comparing(Person::getScore));
英文:

It's better to create a class to store information for every person

class Person {
String name;
Integer score;
String gender;
// constructor, getter, setter
}

And create an ArrayList of Person

ArrayList&lt;Person&gt; list = new ArrayList&lt;&gt;();

Then you can add in the list after taking input in loop

list.add(new Person(name, score, gender));

You can use Collections.max with a comparator to get the max scored Person information

Person maxScoredPerson = Collections.max(list, Comparator.comparing(Person::getScore));

答案2

得分: 1

另一种可能的方法是这样做。

找到最大元素的索引,假设输入是并行输入的,获取最大元素的索引,然后从其他列表中获取相应的对象:

int max = Collections.max(scoreArray);
int index = scoreArray.indexOf(max);
gender = genderArray.get(index);
name = nameArray.get(index);

System.out.println("最高分是" + name + ",得分为" + max + ",是" + gender);
英文:

Another possible way of doing it.

Find the index of the maximum element, assuming input is entered in parallel, get the index of the maximum element and then get the corresponding objects from other lists:

int max = Collections.max(scoreArray);
int index = scoreArray.indexOf(max);
gender = genderArray.get(index);
name = nameArray.get(index);
System.out.println(&quot;Highest score is &quot;+name+&quot;, with a score of &quot;+max+&quot;, is a &quot;+gender);

答案3

得分: 1

你的实现存在一个根本性问题,关于一个人的信息分散在三个不同的列表中。这个示例只对练习有用。真实世界的实现应该将相关细节组织在一个类中,如下所示。

class Person {
   String name;
   Integer score;
   String gender;
   // ......
}

对于这种特殊情况,你可以找到max的索引,然后使用该索引来找到其他信息。

///退出循环后,如何显示最高分和姓名?

Integer maxScore = Collections.max(scoreArray);
Integer index = scoreArray.indexOf(maxScore);
gender = genderArray.get(index);
name = nameArray.get(index);
System.out.println("最高分是" + name + ",得分为" + maxScore + ",是一个" + gender);

或者
如果你想自己实现(不使用Collections.max()),可以添加一个方法来找到最大值,如下:

private int getMax(List<Integer> list) {
  int max = list.get(0);
  for (int i = 0; i < list.size(); i++) {
    if (max < list.get(i)) {
      max = list.get(i);
    }
  } 
  return max;
}
英文:

Your implementation has a fundamental problem, information about a person is scattered in 3 different lists. This example will only be useful for practice. The real world implementation should organize related details in a class as the following.

class Person {
String name;
Integer score;
String gender;
......
}

For this particular case, you can find the index of max and use the index to find the others.
> ///After Quitting loop, how to display the highest score & the names?

Integer maxScore = Collections.max(scoreArray);
Integer index = scoreArray.indexOf(maxScore );
gender = genderArray.get(index);
name = nameArray.get(index);
System.out.println(&quot;Highest score is&quot; + name + &quot;, with a score of &quot; +maxScore  + &quot;, is a &quot; +  gender);

OR
If you would like to do it by your self(not to use Collections.max()), add a method to find the max as:

private int getMax(List&lt;Integer&gt; list) {
int max = list.get(0);
for(int i = 0; i &lt; list.size(); i++) {
if(max &lt; list.get(i)) {
max = list.get(i);
}
} 
return max;
}

huangapple
  • 本文由 发表于 2020年10月25日 13:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64520634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定