如何在Java中打印拆分字符串的值的名称?

huangapple 未分类评论52阅读模式
英文:

How to print the name of a value from a split string in java?

问题

以下是已翻译的内容:

public static void main(String[] args) {
    Scanner lukija = new Scanner(System.in);
    
    int oldest = -1;
    String oldestName = ""; // 添加一个变量来保存最年长者的名字
    while (true) {
        String mjono = lukija.nextLine();
        if (mjono.equals("")) {
            break;
        }
        String[] pieces = mjono.split(",");
        int age = Integer.valueOf(pieces[1]);
        if (age > oldest) {
            oldest = age;
            oldestName = pieces[0]; // 更新最年长者的名字
        }
    }
    System.out.println("The name of the oldest: " + oldestName); // 打印最年长者的名字
}

请注意,我已根据您提供的要求进行了翻译,只返回了翻译好的代码部分。如果您需要进一步解释或帮助,请随时提问。

英文:

I'm supposed to write a program that asks for people's name and age and then prints the oldest person's name. (Sorry some parts of the programming are not in english, but I hope someone understands enough to help). It's an assignment and I have to do it by splitting the string.

The print should be like this:

James,2
Mary,2
Jessica,1
Jennifer,5
Gabriel,10

The name of the oldest: Gabriel

I know how to print the highest age, but not the name of it. This is how i've done it:

public static void main(String[] args) {
    Scanner lukija = new Scanner(System.in);
    
    int oldest = -1;
    while (true) {
        String mjono = lukija.nextLine();
        if (mjono.equals("")) {
            break;
        }
        String[] pieces = mjono.split(",");
        int age = Integer.valueOf(pieces[1]);
        if (age > oldest) {
            oldest = age;
        }
    }
    System.out.println("The oldest age: " + oldest);

答案1

得分: 0

以下是翻译好的代码部分:

让我们假设我们有以下输入

    James,2
    Mary,2
    Jessica,1
    Jennifer,5
    Gabriel,10

我们可以逐行读取然后通过以下类来获得年龄最大的人

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.*;

    public class OldestPerson {
        public static class Person {
            private String name;
            private int age;
    
            Person(String name, int age) {
                this.name = name;
                this.age = age;
            }
    
            public int getAge() {
                return this.age;
            }
    
            public String getName() {
                return this.name;
            }
        }
    
        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line;
            PriorityQueue<Person> queue = new PriorityQueue<Person>((a, b) -> b.getAge() - a.getAge());
            while ((line = reader.readLine()) != null) {
                String[] tmp = line.split(",");
                queue.add(new Person(tmp[0], Integer.parseInt(tmp[1])));
            }
            if (!queue.isEmpty()) {
                Person oldestPerson = queue.poll();
                System.out.println(oldestPerson.getName());
            }
        }
    }

另一种方法是直接比较人的年龄

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line;
        String oldestPerson = "";
        int age = Integer.MIN_VALUE;
        while ((line = reader.readLine()) != null) {
            String[] tmp = line.split(",");
            if (age < Integer.parseInt(tmp[1])) {
                age = Integer.parseInt(tmp[1]);
                oldestPerson = tmp[0];
            }
        }
        System.out.println(oldestPerson);
    }
英文:

Let's say we have the input of:

James,2
Mary,2
Jessica,1
Jennifer,5
Gabriel,10

We can read line by line and then get the oldest person by the following class:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;

public class OldestPerson {
    public static class Person {
        private String name;
        private int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public int getAge() {
            return this.age;
        }

        public String getName() {
            return this.name;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line;
        PriorityQueue&lt;Person&gt; queue = new PriorityQueue&lt;Person&gt;((a, b) -&gt; b.getAge() - a.getAge());
        while ((line = reader.readLine()) != null) {
            String[] tmp = line.split(&quot;,&quot;);
            queue.add(new Person(tmp[0], Integer.parseInt(tmp[1])));
        }
        if (!queue.isEmpty()) {
            Person oldestPerson = queue.poll();
            System.out.println(oldestPerson.getName());
        }
    }
}

Another way to do that is simply comparing the person age directly:

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String line;
    String oldestPerson = &quot;&quot;;
    int age = Integer.MIN_VALUE;
    while ((line = reader.readLine()) != null) {
        String[] tmp = line.split(&quot;,&quot;);
        if (age &lt; Integer.parseInt(tmp[1])) {
            age = Integer.parseInt(tmp[1]);
            oldestPerson = tmp[0];
        }
    }
    System.out.println(oldestPerson);
}

huangapple
  • 本文由 发表于 2020年4月11日 04:51:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61148503.html
匿名

发表评论

匿名网友

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

确定