2014-12-03 70 views
1

所以我一直對這個代碼一段時間了,我測試了它,並有0個錯誤之前添加的JOptionPane,然後我加入的JOptionPane和27-之間有38。所以,我清楚地做了一些錯誤的,我的JOptionPane,但我不知道,我只使用經驗大部分掃描器所以任何幫助,將不勝感激。謝謝!導入.txt文件到數組,輸出W /的對話框

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 
} 

^

+0

它總是有幫助的,包括錯誤/堆棧跟蹤。 – ChiefTwoPencils 2014-12-03 01:06:05

+0

添加錯誤,謝謝大家的意見 – 2014-12-03 01:08:38

回答

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

我經歷了整個代碼去,發現不少misstakes。

  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; 
    } 
} 
+0

非常感謝,我會嘗試用你告訴我 – 2014-12-03 01:22:44

+0

@CobyBushong玩弄,我做了一個編輯您的文章,使之更有點清楚,有你也可以看看通常大括號是如何放置的。 – Emz 2014-12-03 01:26:43

+0

我注意到其他人在做這種方式,但我始終認爲它看起來更整潔我所採取的方式,但由於生病嘗試習慣把他們的方式,而不是 – 2014-12-03 01:29:17