将.text导入Array,输出WITH / Dialog框

时间:2014-12-03 01:04:30

标签: java arrays methods import joptionpane

所以我现在一直在研究这段代码,在添加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
}

^

1 个答案:

答案 0 :(得分:0)

首先是两个问题。你班上的最后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()
  4. 编辑#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;
        }
    }