使用数组和输出的特定输出的名称和年龄应用程序

时间:2016-12-16 10:06:52

标签: java arrays loops

对于我的大学实验室工作,我们必须完成4项任务。我目前在9月6日中完成了大部分工作,但我很难完成它的最后部分。这是我们必须做的事情的描述:

  

编写一个定义两个数组的程序 - 一个字符串和一个整数,大小均为10。   然后,您的程序应该要求用户输入代表人名的字符串,
  和表示其年龄的整数。它应该继续这样做,直到用户
为止   输入'完成'
而不是名称,或直到数组已满(即10对名称和年龄
  已进入)。然后它应该打印出名称和年龄以及名称   最年轻和最老的。

     

提示:一个棘手的部分是确保一旦你输入'完成'完成输入名字,
  你的程序不会问你名字'完成'的人的年龄 - 小心   关于这个。

我已经用粗体文字突出了我所遇到的问题。下面是我目前的代码,但我不确定如何正确完成粗体文本。

import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;

public class nameAge {


public static void main(String[] args){
 String[] name = new String[10];

     int[] age = new int[10];

    Scanner in = new Scanner(System.in);

    String NAME_REQUEST = ("Please enter name");
    String AGE_REQUEST = ("Please enter age");

    System.out.println("Please enter the name of a person and then their age. Do this for up to 10 people and once finished, type 'done'");
    name[0] = in.nextLine();
    System.out.println(AGE_REQUEST);
    age[0] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[1] = in.next();
    System.out.println(AGE_REQUEST);
    age[1] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[2] = in.next();
    System.out.println(AGE_REQUEST);
    age[2] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[3] = in.next();
    System.out.println(AGE_REQUEST);
    age[3] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[4] = in.next();
    System.out.println(AGE_REQUEST);
    age[4] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[5] = in.next();
    System.out.println(AGE_REQUEST);
    age[5] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[6] = in.next();
    System.out.println(AGE_REQUEST);
    age[6] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[7] = in.next();
    System.out.println(AGE_REQUEST);
    age[7] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[8] = in.next();
    System.out.println(AGE_REQUEST);
    age[8] = in.nextInt();
    System.out.println(NAME_REQUEST);
    name[9]= in.next();
    System.out.println(AGE_REQUEST);
    age[9] = in.nextInt();
    System.out.println(NAME_REQUEST);


    int size = name.length;
    int sizeN = age.length;

    for (int i=0; i < size; i++) {
        System.out.println("Name: " + name[i]);
        System.out.println("Age: " + age[i]);
    }
    int smallest = age[0];
    int largetst = age[0];
    for(int i=1; i< age.length; i++)
            {
                    if(age[i] > largetst)
                            largetst = age[i];
                    else if (age[i] < smallest)
                            smallest = age[i];

            }

            System.out.println("Largest Number is : " + largetst);
            System.out.println("Smallest Number is : " + smallest);
    }
}

2 个答案:

答案 0 :(得分:0)

你必须看看java中的循环文档 这段代码可以帮到你

public static void main(String[] args) {
    int youngest =0,older=0;
     String[] name = new String[10];
         int[] age = new int[10];
         String NAME_REQUEST = ("Please enter name");
         String AGE_REQUEST = ("Please enter age");

    for(int i=0 ; i< 10;i++){
       Scanner in = new Scanner(System.in);
       System.out.println(NAME_REQUEST); 
    String tmpName = in.nextLine();
    if(tmpName.equalsIgnoreCase("done"))
    break;
    name[i] = tmpName;
    System.out.println(AGE_REQUEST);
    age[i] = in.nextInt();
    if(age[i] > age[older])
    older = i;
    if(age[i] < age[youngest])
    youngest = i;

    }

     System.out.println("OLDER is  : " + name[older]);
     System.out.println("Younger : " + name[youngest]);
}

答案 1 :(得分:-1)

试试这个,我已经测试过了,它运行正常。希望有所帮助。快乐的编码。

package com.pearson.nextgen.aggregatedsessionservice;

import java.util.Scanner;

public class NameAgeTest {

    public static void main(String[] args) {
        String[] name = new String[10];
        int[] age = new int[10];
        Scanner in = new Scanner(System.in);

        String NAME_REQUEST = "Please enter name";
        String AGE_REQUEST = "Please enter age";

        int count = 0;
        while (count < 10) {
            System.out.println(NAME_REQUEST);

            String nameInput = in.next();
            if (nameInput.equalsIgnoreCase("done")) {
                break;
            }
            name[count] = nameInput;
            System.out.println(AGE_REQUEST);
            age[count] = in.nextInt();

            count++;
        }

        int[] minAndMaxIndex = findMinAndMaxIndex(age, count);
        System.out.println("Youngest Person: " + name[minAndMaxIndex[0]]);
        System.out.println("Oldest Person: " + name[minAndMaxIndex[1]]);

    }

    private static int[] findMinAndMaxIndex(int[] inputArray, int count) {
        int min, max = 0;
        int minIndex = 0, maxIndex = 0;
        max = min = inputArray[0];

        for (int i = 0; i < count; i++) {
            if (inputArray[i] > max)
                maxIndex = i;
            else if (inputArray[i] < min)
                minIndex = i;

        }
        return new int[] { minIndex, maxIndex };
    }

}