如何将循环结果带入带有索引的字符串中?

问题描述 投票:0回答:1

我正在尝试构建一个字符串,并放入循环结果“ att”转换为索引为“ i”的字符串。因此,我可以对字符串进行排序,并输出带有学校编号的出勤率最高的学校。谢谢!

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);
    }
}
java loops
1个回答
0
投票

问题尚不清楚,数组索引总是从零开始,而不是for循环中提到的1。如果您希望将att存储在String数组中,请声明String数组并将att存储在string数组中

© www.soinside.com 2019 - 2024. All rights reserved.