0

名前と年齢を交互に切り替えるリストがテキストファイルにあります。9つの名前と9つの年齢があります。

プログラムを表示する必要があります

人(ここに名前を挿入)の年齢が最も高い:(ここに年齢を挿入)。

これまでの私のコードは次のとおりです。

     public class Names 
     {


public static void main(String[] args) throws Exception
{

      // get textfile
      Scanner input = new Scanner( new File("names_and_ages.txt") );


final String[] names= new String[9];
final int[] ages= new int[9];
int counter = 0;

while (input.hasNext()) //while not end-of-file
{
    names[counter]=input.next();  
    ages[counter]=Integer.parseInt(input.next());
    counter++;

}
highestAge(names, ages);
input.close();
}//end of main

public static void highestAge (String[] name, int[] age)
{
    String name;
int count = 0;
int oldest = highestAge[count];
for ( int number: highestAge)
{
    if (number > oldest)
        {
        oldest = number;
        }
}
System.out.print("Person " + name + " has the highest age: " + oldest );
}//end of Size method

}//end of class

名前を年齢と一致させるために私はただ見ることができないすべてがコンパイルされます。ヘルプ?

4

4 に答える 4

1

Personクラスを作成した場合は、2つの配列を管理する手間を省くことができます。

これはあなたのPersonクラスになります:

class Person {
    String name;
    int age;
}
于 2012-10-08T22:42:19.687 に答える
1
public static void highestAge(String[] names, int[] ages)
{
    //SET SENSIBLE DEFAULTS TO oldest AND index
    int index = 0;
    int oldest = ages[0];
    //Looping based on index
    for (int i = 0;  i < ages.length; i++)
    {
        if (ages[i] > oldest)
        {
           index = i;
           oldest = ages[i];
        }
}
System.out.print("Person " + names[index] + " has the highest age: " + ages[index] );
}//end of Size method
于 2012-10-08T22:43:03.810 に答える
0

あなたはそう遠くないです。最も古い年齢の「インデックス」を維持する必要があります。これを使用して、2つのアレイ間を参照できます。

public static void highestAge (String[] names, int[] ages)
{
    String name
    int oldest = 0;
    int oldestIndex = -1;
    for (int index = 0; index < ages.length; index++) 
    {
        if (ages[index] > oldest)
        {
            oldest = number;
            oldestIndex = index;
        }
    }
    System.out.print("Person " + names[oldestIndex] + " has the highest age: " + oldest );
}//end of Size method

また、あなたの例がどのようにコンパイルされるのかわかりません。配列highestAgeがどこで宣言されているのかわかりません:P

個人的にPersonは、名前と年齢をフィールドとして持つクラスを作成します...

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

次に、ループの反復ごとに新しいPersonものを作成し、それらを人々の配列に追加します(Person[] people = new Person[...])(実際には、の実装を使用しますが、java.util.listそれは私です)

あなたは突然あなたのためにたくさんの創造性を開きます。

あなたはisOlderThen(Person person)人々を一緒に比較する方法を提供することができます。

if (person.isOlderThen(oldest)) {
    oldest = person;
}

Arrays.sort実際には、とを使用して、各人の年齢に基づいて配列を並べ替えるだけです。Comparator

于 2012-10-08T22:42:54.067 に答える
0

1)クラスを作成する

class person
{
   String name;
   int    age;
}

2)配列を1つだけ作成します:Person [] people

3)メソッドhighestAgeを書き直します

Person getOldest( Person[] persons )
{
   Person oldest = null;
   for( Person person : persons )
   {
      if( oldest == null || person.age > oldest.age )
      {
         oldest = person;
      }
   }
   return oldest;
}
于 2012-10-08T22:43:13.277 に答える