gpt4 book ai didi

java - 如何对从 JAVA 中的 .txt 文件读取的数组进行排序

转载 作者:行者123 更新时间:2023-12-01 23:05:02 25 4
gpt4 key购买 nike

这是我的代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Scanner;
import java.util.Arrays;

public class runsCreated {

public static void main(String[] args) throws FileNotFoundException {
String firstName = "";
String lastName = "";
int hits = 0;
int walks = 0;
int caughtStealing = 0;
int stolenBases = 0;
int atBats = 0;
int totalBases = 0;
double runsCreated = 0.0;
double[] runsCreatedList = new double[40];
double[] sortedList = new double[40];
String[] nameList = new String[40];
Scanner console = new Scanner(System.in);

//Asks user for input file name
System.out.println("Input File Name:");
String inputFileName = "//Users//Mitchell//Desktop//baseballinput.txt"; //console.next();


//inputFile will take input from stored input file
File inputFile = new File(inputFileName);

//in will take input from user defined source
Scanner inFile = new Scanner(inputFile);

int i = 0; //counter

while(inFile.hasNext()) {
lastName = inFile.next();
firstName = inFile.next();
hits = inFile.nextInt();
walks = inFile.nextInt();
totalBases = inFile.nextInt();
stolenBases = inFile.nextInt();
caughtStealing = inFile.nextInt();
atBats = inFile.nextInt();

runsCreated = ((hits + walks - caughtStealing) * (totalBases + (.55 * stolenBases)) / (atBats + walks));
runsCreatedList[i] = runsCreated;
sortedList[i] = runsCreatedList[i];

System.out.print(nameList[i] + ":" + " " + /*will be sortedList[i]*/runsCreatedList[i]);

i++;
}

sortedList[i] = Arrays.sort(sortedList);
inFile.close();
}
}

我在 sortedList[i] = Arrays.sort(sortedList); 上收到错误,无法从 void 转换为 double。我知道它应该是一个 double 我只是不知道如何修复它。任何帮助,将不胜感激。

最佳答案

Arrays.sort(array); 只是对数组进行排序。它不返回任何内容。 Documentation 。如果你期待双倍,你想做什么?能举个例子吗?

关于java - 如何对从 JAVA 中的 .txt 文件读取的数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22914628/

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