gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:24:23 26 4
gpt4 key购买 nike

我正在尝试构建一个字符串,并将循环结果放入将“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);
}
}

最佳答案

编辑

鉴于学校 ID 是一个整数,可以在一个简单的数组中执行此操作,这比下面的集合 API 更节省空间和时间。

尽早初始化一个double数组:

double[] schools = new double[nos];

在循环中添加数据:

schools[i] = att;

并在最后找到最高的,并打印:

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");

完整示例:

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);
}
}

原帖:

我不确定字符串是这里的最佳路线。如果我没记错的话,您希望将多对值存储在列表或数组中,然后按其中一个值进行排序。使用集合 API 可以最好地解决这个问题。举个例子:

在开头(循环之前)定义键值对列表。键是学校 ID,值是出勤率。

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

然后将数据添加到循环中的列表中:

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

最后,按值降序排序(最高的在前),然后打印结果。

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!");

完整示例如下:

  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!");
}

关于java - 如何将循环的结果放入带有索引的字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58923560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com