[英]Java Array Not Printing (BlueJ being used)
我目前正在嘗試完成要求使用Java編程的大學任務。 任務如下。
我必須對輸入文件進行分類以進行“釣魚比賽”。 該文件首先為每行給出一個整數(1-7之間),然后是一個實數。 棘手的部分是尚未將這些數字累加或按順序累加。 這意味着我必須使程序讀取整數,證明其所需的位置,然后在該位置添加以下權重。
這是文件外觀的一個想法
3 36.2
6 27.8
7 10.3
我對如何完成整個任務有一般的想法,但是我陷入了一個問題,那就是打印每個競爭對手的總重量。
這是我到目前為止編寫的代碼。 如果您能告訴我是什么給我帶來了問題,將不勝感激。 (它沒有給出錯誤代碼,而是我正在使用的軟件BlueJ,只是不斷嘗試運行該程序)
import java.io.*; import java.util.Scanner; public class Project7 { public static void main(String[] args) throws FileNotFoundException { Scanner inF = new Scanner(new File("inputpro7.txt")); int [] arrayNum = new int[7]; double [] arrayWeight = new double[7]; int place = inF.nextInt(); double weight = inF.nextDouble(); for (int m = 0; m < 7; m++) { arrayNum[m] = m + 1; } while (inF.hasNextLine()) { if (place == 1) arrayWeight[0] += weight; else if (place == 2) arrayWeight[1] += weight; else if (place == 3) arrayWeight[2] += weight; else if (place == 4) arrayWeight[3] += weight; else if (place == 5) arrayWeight[4] += weight; else if (place == 6) arrayWeight[5] += weight; else arrayWeight[6] += weight; } for (int k = 0; k < 7; k++) System.out.printf("%6d %6.2f%n", arrayNum[k], arrayWeight[k]); inF.close(); } }
您的代碼中的問題在While循環中。 您正在連續讀取輸入文件的第一行。 While循環內沒有已讀取的nextline語句。 即使您放置該語句(閱讀下一行),您也需要在代碼中進行一些修改。 以下是修改后的代碼。
public class ExampleCode {
public static void main(String[] args) throws FileNotFoundException {
Scanner inF = new Scanner(new File("Input.txt"));
int[] arrayNum = new int[7];
double[] arrayWeight = new double[7];
int place;
double weight;
for (int m = 0; m < 7; m++) {
arrayNum[m] = m + 1;
}
while (inF.hasNextLine()) {
place = inF.nextInt();
weight = inF.nextDouble();
if (place == 1)
arrayWeight[0] += weight;
else if (place == 2)
arrayWeight[1] += weight;
else if (place == 3)
arrayWeight[2] += weight;
else if (place == 4)
arrayWeight[3] += weight;
else if (place == 5)
arrayWeight[4] += weight;
else if (place == 6)
arrayWeight[5] += weight;
else
arrayWeight[6] += weight;
}
for (int k = 0; k < 7; k++)
System.out.printf("%6d %6.2f%n", arrayNum[k], arrayWeight[k]);
inF.close();
}
}
解決此類問題“處理輸入文件並基於相似的鍵值聚合輸入”的最佳方法是使用HashMap。 以下是可能的解決方案。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class ExampleCode {
public static void main(String[] args) throws FileNotFoundException
{
Scanner inF = new Scanner(new File("Input.txt"));
Map<String, Double> table = new HashMap<String, Double>();
String line = "";
String token[] = new String[2];
//Process till end of file
while (inF.hasNextLine())
{
//Read each lines
line = inF.nextLine();
//Split line based on separator i.e " " character
//use these tokens as Key-Value pairs
token = line.split(" ");
//If table already has the entry add the value to same key entry
if(table.containsKey(token[0])){
Double value = table.get(token[0]) + new Double(token[1]);
table.put(token[0], value);
} else { //If key not found, insert the new entry
table.put(token[0], new Double(token[1]));
}
}
for(Entry entry : table.entrySet()){
System.out.println("Key:"+entry.getKey()+" == Value:"+entry.getValue());
}
//close the file
inF.close();
}
}
Sample Input file:
2 9.25
4 8.5
1 1.5
2 4.215
5 1.4555
6 8.989
7 1
4 8.049
1 3.567
Output :
Key:1 == Value:5.067
Key:2 == Value:13.465
Key:4 == Value:16.549
Key:5 == Value:1.4555
Key:6 == Value:8.989
Key:7 == Value:1.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.