简体   繁体   中英

how to take the result from the loop into string with index?

I am trying to build a string, and put the result of the loop "att" into string with the index "i". So, I can sort the string and output the highest attendance school with the school number. Thanks!

import java.util.Scanner;

public class PengjuShanP1 {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.print("How many schools do you have in your district: ");
        int nos = scnr.nextInt();
        int[] nums = new int[nos];
        System.out.println();

        double ax  = 0;

        for (int i = 1; i < nos + 1; ++i) {
             System.out.println("Enter data for school " + i);
             System.out.print("   How many students are enrolled in school : ");
             int num = scnr.nextInt();



             System.out.print("   Enter the attendance for day 1: ");
             int d1 = scnr.nextInt();


             System.out.print("   Enter the attendance for day 2: ");
             int d2 = scnr.nextInt();

             System.out.print("   Enter the attendance for day 3: ");
             int d3 = scnr.nextInt();

             System.out.print("   Enter the attendance for day 4: ");
             int d4 = scnr.nextInt();

             System.out.print("   Enter the attendance for day 5: ");
             int d5 = scnr.nextInt();

             double avg = ((d1 + d2 + d3 + d4 + d5) / 5) * 100;
             double att = avg / num;
             ax = att + ax;

             System.out.println();

             System.out.println("Attendance " + att + "% for school " + i);
             System.out.println();
        }
        System.out.print(ax);
    }
}

The question is not clear and array index always start from zero instead of 1 as mentioned in the for loop. if you are looking to store att in array of String, declare String array and store att in the string array

EDIT

Given that the school id is an integer, it's possible to do this in a simple array, which is more space and time efficient than the collections API below.

Initialize a double array early on:

double[] schools = new double[nos];

Add data in the loop:

schools[i] = att;

And find the highest at the end, with printing:

double highest = 0;
int index = 0;
for (int i = 0; i < schools.length; i++)
{
  if (schools[i] > highest)
  {
    index = i;
    highest = schools[i];
  }
}
System.out.println("The school with the best attendance is school " + (index + 1) + " with " + highest + "% attendance");

The full example:

import java.util.Scanner;

public class ValuePairSortingSimpler
{
  public static void main(String[] args)
  {
    Scanner scnr = new Scanner(System.in);
    System.out.print("How many schools do you have in your district: ");
    int nos = scnr.nextInt();
    double[] schools = new double[nos];
    System.out.println();

    double ax = 0;

    for (int i = 0; i < nos; i++)
    {
      System.out.println("Enter data for school " + (i + 1));
      System.out.print("   How many students are enrolled in school : ");
      int num = scnr.nextInt();

      System.out.print("   Enter the attendance for day 1: ");
      int d1 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 2: ");
      int d2 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 3: ");
      int d3 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 4: ");
      int d4 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 5: ");
      int d5 = scnr.nextInt();

      double avg = ((d1 + d2 + d3 + d4 + d5) / 5) * 100;
      double att = avg / num;
      schools[i] = att;
      ax = att + ax;

      System.out.println();

      System.out.println("Attendance " + att + "% for school " + (i + 1));
      System.out.println();
    }
    double highest = 0;
    int index = 0;
    for (int i = 0; i < schools.length; i++)
    {
      if (schools[i] > highest)
      {
        index = i;
        highest = schools[i];
      }
    }
    System.out.println("The school with the best attendance is school " + (index + 1) + " with " + highest + "% attendance");
    System.out.print(ax);
  }
}

Original Post:

I'm not sure a string is the best route here. If I'm not mistaken, you'd like to store several pairs of values in a list or array and then sort that by one of the values. That problem is best solved using the collections API. For an example:

Define a list of key-value pairs at the beginning (before the loop). The key will be the school id and the value will be the attendance.

List<Map.Entry<Integer, Double>> schools = new ArrayList<>();

Then add the data to the list in the loop:

schools.add(Map.entry(i, att));

And at the end, sort it descending by value (highest first), then print the results.

schools.sort((a,b)->(int)Math.round(b.getValue()-a.getValue()));
System.out.println("The school with best attendance is school " + schools.get(0).getKey() + " with " + schools.get(0).getValue() + "% attendance!");

A full example follows:

  public static void main(String[] args)
  {
    Scanner scnr = new Scanner(System.in);
    System.out.print("How many schools do you have in your district: ");
    int nos = scnr.nextInt();
    List<Map.Entry<Integer, Double>> schools = new ArrayList<>();
    System.out.println();

    double ax = 0;

    for (int i = 1; i < nos + 1; ++i)
    {
      System.out.println("Enter data for school " + i);
      System.out.print("   How many students are enrolled in school : ");
      int num = scnr.nextInt();

      System.out.print("   Enter the attendance for day 1: ");
      int d1 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 2: ");
      int d2 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 3: ");
      int d3 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 4: ");
      int d4 = scnr.nextInt();

      System.out.print("   Enter the attendance for day 5: ");
      int d5 = scnr.nextInt();

      double avg = ((d1 + d2 + d3 + d4 + d5) / 5) * 100;
      double att = avg / num;

      schools.add(Map.entry(i, att));

      ax = att + ax;

      System.out.println();

      System.out.println("Attendance " + att + "% for school " + i);
      System.out.println();
    }
    System.out.print(ax);
    schools.sort((a,b)->(int)Math.round(b.getValue()-a.getValue()));
    System.out.println("The school with best attendance is school " + schools.get(0).getKey() + " with " + schools.get(0).getValue() + "% attendance!");
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM