1
import java.io.File;
import java.util.Scanner;

public class scoresedit {

public static void main(String[] args) throws Exception{
    System.out.println("Reads in a file at the beginning of a loop, displays these top 3 scores by name and their respective score." +
        "\nEnter a new name and score following a prompt. " +
        "\nIf the new name and score exceeds one of the top three scores," +
        "\nplaces the new name in the top three, moving all lower scores down, dropping the lowest score." +
        "\nSaves the results in the file top3Scores.txt."  +
        "\nStops the loop when exit is entered for a name.");
    File r = new File ("top3Scores.txt");
    System.out.println(r);
    String str = readSource (r);
    System.out.println(str);
    String[] strArray = new String[] {str};
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a new name and score or 'exit' to quit:");
    String nameAndScore = in.nextLine();
    String[] nameAndScoreArray = new String[] {nameAndScore};
    String nameAndScoreArray1[] =  nameAndScore.split(" ");
    while (nameAndScore != "exit") {
        if (nameAndScoreArray[1] > strArray[1]) }
    }

private static String readSource(File r) throws Exception{
    Scanner in;
    in = new Scanner (r);
    String str = "";
    while (in.hasNext())
        str += in.nextLine() +"\n";
    in.close();
    return str;
}

}

当我尝试比较两个数组并且不知道如何修复它时,我不断收到错误“运算符 > 未定义参数类型 java.lang.String、java.lang.String”。代码还没有完成;显然我在 if 语句下添加了更多内容,但我想先解决这个问题。

4

2 回答 2

6

您正在尝试比较nameAndScoreArray[1]哪些strArray[1]是字符串,我假设它们包含两支球队的分数,得分最高的球队获胜,因此您想使用Integer.parseInt(String string)以查看哪支球队的得分最高:

if (Integer.parseInt(nameAndScoreArray[1]) > Integer.parseInt(strArray[1]))
于 2013-06-18T22:52:40.663 回答
4

您不能String与运算符<,进行比较>,而是需要使用compareTo

于 2013-06-18T22:52:19.823 回答