- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是一名编码新手,我的类(class)最近开始学习数组。我一整天都在为这个程序作业苦苦思索。几天后才到期,但我对此感到非常沮丧。
我们必须打印出一份考试成绩列表和与之关联的相关 ID。我们必须将测试分数从最高到最低排序,但不修改数组——所以没有数组排序,或任何类似的。 “建议的解决方案”说:
找到最大的分数,跟踪它的下标。
打印此分数以及相应的 ID。
设置那个分数等于它的相反值(这使得它最小得分!)。
重复步骤 1-3,直到处理完所有 21 个分数。
停止并打印适当的消息。
我一直在努力遵守这些说明,因为本周剩下的时间里还有其他程序将依赖于此。然而,我的方法是有缺陷的,因为我的代码打印了 26 次迭代的最大分数是 110、238。我在使用这段代码时遇到了很大的麻烦,这让我很沮丧。这是我目前所拥有的:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Prog408a {
static Scanner inFile = null;
static Scanner inFile2 = null;
public static void main (String[]args) {
try {
// create scanner to read file
inFile = new Scanner(new File ("prog408a.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}
int [] ids = new int[26];
int [] scores = new int[26];
int first = 0;
int second = 0;
int counter = 0;
int max, maxid, supermax;
do {
ids [first] = inFile.nextInt();
scores [second] = inFile.nextInt();
first++;
second++;
} while (inFile.hasNextInt());
/* initial values of max, maxid */
System.out.println("ID\t\tScore");
maxid = ids[0];
supermax = scores[0];
max = scores[0];
int index=0;
while (counter < scores.length) {
for (int x = 0; x < scores.length - 1; x++) { // scroll through the indexes.
if (scores[x] > max) {
max = scores[x];
index = x; // keep the index of the biggest number.
}
max = -1 * (scores[index]);
}
System.out.println("The maximum score is " + ids[index] + ", " + scores[index]);
counter++;
}
}
}
这是数据文件中的内容。
365 265
222 223
306 262
003 559
004 560
203 224
113 243
208 242
213 229
115 257
302 242
223 230
001 599
311 256
323 245
321 245
123 253
104 239
002 513
112 239
207 228
325 246
116 246
218 243
110 238
如果帖子中有任何格式错误,请原谅。
最佳答案
while (counter < scores.length - 1) {
for (int x = 0; x < scores.length - 1; x++) { // scroll through the indexes.
if (scores[x] > max) {
max = scores[x];
index = x; // keep the index of the biggest number.
}
}
System.out.println("The maximum score is " + ids[index] + ", " + scores[index]);
max = -1 * (scores[index]);
scores[index] = -1 * (scores[index]); // change the value in the original array so it won't find the same max again
counter++;
}
输出:
ID Score
The maximum score is 1, 599
The maximum score is 4, 560
The maximum score is 3, 559
The maximum score is 2, 513
The maximum score is 365, 265
The maximum score is 306, 262
The maximum score is 115, 257
The maximum score is 311, 256
The maximum score is 123, 253
The maximum score is 325, 246
The maximum score is 116, 246
The maximum score is 323, 245
The maximum score is 321, 245
The maximum score is 113, 243
The maximum score is 218, 243
The maximum score is 208, 242
The maximum score is 302, 242
The maximum score is 104, 239
The maximum score is 112, 239
The maximum score is 110, 238
The maximum score is 223, 230
The maximum score is 213, 229
The maximum score is 207, 228
The maximum score is 203, 224
The maximum score is 222, 223
您可能想要添加一个小函数来打印 id < 100
在开始时有额外的零。
关于java - 在没有方法的情况下按顺序打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542195/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!