【问题标题】:Import .txt into Array, output W/ Dialog boxes将.text导入数组,输出WITH/对话框
【发布时间】:2015-01-31 10:30:46
【问题描述】:

所以我已经在这段代码上工作了一段时间,在添加 JOptionPane 之前我测试了它并且有 0 个错误,然后我添加了 JOptionPane 并得到了 27-38 之间的错误。所以我显然在我的 JOptionPane 中做错了,但我不确定是什么,我大部分情况下只有使用 Scanner 的经验,所以任何帮助都将不胜感激。谢谢!

import javax.swing.JOptionPane;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class TestScores
{
    public static void main(String args[]) throws IOException {
        String filename = ("scores.txt");
        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        while (inputFile.hasNextInt()) {
            String line = inputFile.nextLine();
            ArrayList<Integer> scores = new ArrayList<Integer>();
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");

            while(scanner.hasNextInt()) {
                scores.add(scanner.nextInt());
            }

            scanner.close();
        }
    }

    public static int averageScore(int[] numbers) {
        int total = 0;
        for (int i : numbers) {
            total += i;
        }
        return total/(numbers.length);
    }

    public static int modeOfScores(int[] numbers) {
        int modeCount = 0;
        int mode = 0;     
        int currCount = 0;    
        int currElement;

        for (int candidateMode : numbers) {
            currCount = 0;

            for (int element : numbers) {
                if (candidateMode == element) {
                    currCount++;
                }
            }

            if (currCount > modeCount) {
                modeCount = currCount;
                mode = candidateMode;
            }
        }

        return mode;
    }

    public static int lowestScore(int[] numbers) {
        int lowest = numbers[0];

        for(int i = 1; i > numbers.length; i++) {
            if(numbers[i] < lowest) {
                lowest = numbers[i];
            }
        }
        return lowest;
    }

    public static int largestScore(int[] numbers) {  
        int largest = numbers[0];  
        for(int i = 1; i < numbers.length; i++) {  
            if(numbers[i] > largest) {  
                largest = numbers[i];  
            }
        }  
        return largest;
    }
    JOptionPane.showMessageDialog (null, "The Average number is: " + total);
    JOptionPane.showMessageDialog (null, "The Mode number is: " + mode);
    JOptionPane.showMessageDialog (null, "The Lowest number is: " + lowest);
    JOptionPane.showMessageDialog (null, "The Largest number is: " + largest);
}

编辑:这是我得到的所有错误,都来自 JOptionPane 部分

TestScores.java:86: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                ^
TestScores.java:86: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                 ^
TestScores.java:86: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                       ^
TestScores.java:86: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                ^
TestScores.java:86: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                  ^
TestScores.java:86: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                               ^
TestScores.java:86: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                                ^
TestScores.java:86: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                                 ^
TestScores.java:86: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                                  ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
              ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                 ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                       ^
TestScores.java:87: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                             ^
TestScores.java:87: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                               ^
TestScores.java:87: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                            ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                             ^
TestScores.java:87: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                              ^
TestScores.java:87: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                               ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
              ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                 ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                       ^
TestScores.java:88: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                               ^
TestScores.java:88: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                 ^
TestScores.java:88: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                             ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                              ^
TestScores.java:88: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                               ^
TestScores.java:88: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                                ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
              ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                 ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                       ^
TestScores.java:89: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                ^
TestScores.java:89: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                  ^
TestScores.java:89: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                               ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                                ^
TestScores.java:89: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                                 ^
TestScores.java:89: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                                  ^
TestScores.java:90: error: reached end of file while parsing
}

^

【问题讨论】:

  • 包含错误/堆栈跟踪总是有帮助的。
  • 添加错误,谢谢指教

标签: java arrays methods import joptionpane


【解决方案1】:

首先有两个问题。您的最后 4 个方法调用在类中是松散的。

JOptionPane.showMessageDialog(null, "The Average number is: " +(total));
JOptionPane.showMessageDialog(null, "The Mode number is: " +(mode));
JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowest));
JOptionPane.showMessageDialog(null, "The Largest number is: " +(largest));

在这种情况下,它们需要在方法中。

第二个总计、众数、最低和最大仅在其各自的方法范围内本地定义。

您需要将它们存储为 TestScores 的成员。

编辑#1

小记,Java是用lowerCamelCase写的http://en.wikipedia.org/wiki/CamelCase

编辑#2

我浏览了整个代码,发现了很多错误。

  1. Java 中的arrayArrayList 不同。所以你必须 选择一个,在这种情况下,ArrayList 将为您解决问题,因为 你可以更优雅地处理它。
  2. ArrayList 没有像 array 那样的索引。相反,您必须使用它的方法get(index). 运算符访问。
  3. 由于方法返回totalmodelowestlargest 的值,为什么不调用它们 直接在你的showMessageDialog()?

编辑#3

我现在没有用于测试的 scores.txt。这不会产生编译错误。

import javax.swing.JOptionPane;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestScores
{
    public static void main(String args[]) throws IOException { 
        String filename = ("scores.txt");
        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        List<Integer> scores = new ArrayList<Integer>();

        while (inputFile.hasNextInt()) {
            String line = inputFile.nextLine();

            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");

            while(scanner.hasNextInt()) {
                scores.add(scanner.nextInt());
            }

            scanner.close();
        }

        JOptionPane.showMessageDialog (null, "The Average number is: " + averageScore(scores));
        JOptionPane.showMessageDialog (null, "The Mode number is: " + modeOfScores(scores));
        JOptionPane.showMessageDialog (null, "The Lowest number is: " + lowestScore(scores));
        JOptionPane.showMessageDialog (null, "The Largest number is: " + largestScore(scores));
    }

    public static int averageScore(List<Integer> numbers) {
        int total = 0;
        for (int i : numbers) {
            total += i;
        }
        return total/(numbers.size());
    }

    public static int modeOfScores(List<Integer> numbers) {
        int modeCount = 0;
        int mode = 0;     
        int currCount = 0;    
        int currElement;

        for (int candidateMode : numbers) {
            currCount = 0;

            for (int element : numbers) {
                if (candidateMode == element) {
                    currCount++;
                }
            }

            if (currCount > modeCount) {
                modeCount = currCount;
                mode = candidateMode;
            }
        }

        return mode;
    }

    public static int lowestScore(List<Integer> numbers) {
        int lowest = numbers.get(0);

        for(int i = 1; i > numbers.size(); i++) {
            if(numbers.get(i) < lowest) {
                lowest = numbers.get(i);
            }
        }
        return lowest;
    }

    public static int largestScore(List<Integer> numbers) {  
        int largest = numbers.get(0);  
        for(int i = 1; i < numbers.size(); i++) {  
            if(numbers.get(i) > largest) {  
                largest = numbers.get(i);  
            }
        }  
        return largest;
    }
}

【讨论】:

  • 非常感谢,我会试试你告诉我的内容
  • @CobyBushong,我对您的帖子进行了编辑以使其更加清晰,您还可以看到大括号的放置方式。
  • 我注意到其他人这样做,但我一直认为我这样做看起来更整洁,但谢谢我试着习惯这样放置它们
  • 好吧,我想我明白你在#3 JOptionPane.showMessageDialog(null, "The Average number is: " + (modeOfScores));正确的??但是,我不太确定我是否理解您在 #1 和 #2 中的意思
  • @CobyBushong 差不多! modeOfScores() 是一个方法,记住括号。它也需要一个论据。在您的原始代码中,它采用了int[],但您的分数被声明为List&lt;Integer&gt;,因此请更改您的方法并将scores 作为您的输入参数。
猜你喜欢
  • 1970-01-01
  • 2021-03-17
  • 2011-12-09
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多