JAVA string arrangement again."T"

Asked 2 years ago, Updated 2 years ago, 45 views

package com.javalec.ex;

import java.io.StreamCorruptedException; import java.util.Scanner; import java.util.Stack;

public class ArrayEx { public static void main(String[] args) {

 String[] name = {"Yoona", "Taeyeon", "Seohyun", "Yuri", "Tiffany"};
    int[] height = new int[5];
    int total = 0;
    int max = 0;
    int maxindex = 0;
    int min = 0;
    int minindex = 0;
    String[] height1 = new String[5];

    Scanner scanner = new Scanner(System.in);
    for (int i = 0; i < height.length; i++) {
        System.out.print(name[i] + " key: ");
        height[i] = scanner.nextInt();
        total += height[i];
    }
    System.out.println ("The average height of people is ""+(total/name.length)");

    for (int i = 0; i < height.length; i++) {
        if(height[i] > max) {
            max = height[i];
            maxindex = i;
        }
    }
    for (int i = 0; i < height.length; i++) {
        if(height[i] == max) {
            height1[i] = name[i];
        }
    }
    for (int i = 0; i < height1.length; i++) {
        System.out.println ("The largest person is "" + height1[i] + ""));
    }

    min = max;
    for (int i = 0; i < height.length; i++) {
        if(height[i] < min) {
            min = height[i];
            minindex = i;
        }
    }
    System.out.println ("The smallest person is " + name[minindex] + ");


}

}

If you do this...

Enter Yoona's key. : : 170

Enter Taeyeon's key. : : 180

Enter Seohyun's key. : : 180

Enter Yuri's key. : : 160

Please enter Tiffany's key. : : 175

The average height of a person is 173.

The largest person is null. ←

The biggest person is Taeyeon.

The biggest person is Seohyun.

The largest person is null.

The largest person is null.

The smallest person is Yuri.

Question)

The size of the string array height1 is set to 5, so non-redundant people come out as null values.

Not like this, even if it overlaps, Taeyeon and Seohyun will come out.

How do I make sure that only 3 people come out if the keys of 3 people overlap?

Should I do it?

java string array

2022-09-21 20:09

1 Answers

Try to print it out using an if statement.


2022-09-21 20:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.