[数组的输出

问题描述 投票:-5回答:1

[首先,我试图获取我编写的用于计算和输出从数据文件中读取的数组的最小值,最大值和平均值的代码,但是它完全跳过了这些方法。

最后,程序应该跳过这三行345,1230 / 8,0025 / 35,5900,因为第二个数字是不正确的磅数百分比,产生的磅数其余的一分为二。我正在努力让程序捕获以余数结尾的任何结果,并跳过该行以读取下一行。您将看到我在尝试中注释掉了if语句。对于如何解决这个问题,有任何的建议吗?

------Data File-----
233, 7500
23, 5000
56, 3125
79, 0625
45, 8125
76, 3750
76, 6250
15, 4375
56, 3750
345, 1230
34, 4375
654, 6875
8, 0025
5, 3125
987, 2500
56, 8125
24, 2500
92, 0000
35, 3125
32, 0625
35, 5900

Conversion Chart

   package projectone;

import java.io.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

public class ProjectOne{
 public static class Weight { // Start Weight class

private int pound;
private int ounce;

Scanner down = new Scanner(System.in);


    private Weight(int pound, int ounce) { //Start Weight Constructor
    this.pound = pound;
    this.ounce = ounce;
    this.normalize();
} // End Weight constructor

public boolean lessThan(Weight other) { //Start lessThan method
    other.normalize();
    if (this.pound > other.pound) { // Start If
        return false;
    } // End if
    return !((this.pound == other.pound) && (this.ounce > other.ounce));
}  //End lessThan method

public void addTo(Weight other) { // Start addTo
    this.pound += other.pound;
    this.ounce += other.ounce;
    this.normalize();
} // End addTo

public void divide(int divisor) { // Start divide
    this.pound = this.pound / divisor;
    this.ounce = this.ounce / divisor;
    //if (ounce = float) {
       //   down.nextLine();
   // }
    this.normalize();
}// End divide

@Override
public String toString() { // Start toString
    return String.format("%d lbs, %d oz" , pound , ounce);
}

private double toOunces() { // Start toOunces
    return pound * 16 + ounce;
} // End toOunces

private void normalize() { // Start normalize
    while (ounce >= 16) { // Start while loop
        ounce = ounce - 16;
        } // End while loop
} // End nomalize

     private static Weight findMinimum(Weight[] weightArray, Integer weights) {  // Start findMin
    Weight minWeight = weightArray[0];
    for (int i = 0; i < weights; i++) { // Start for loop
        if (minWeight.lessThan(weightArray[i]) == false) { // Start If 
            minWeight = weightArray[i];
        } // End if
    } // End for
    return minWeight;

} // End findMin

private static Weight findMaximum(Weight[] weightArray, Integer weights) { // Start findMax
    Weight maxWeight = weightArray[0];
    for (int i = 0; i < weights; i++) { // Start for
        if (maxWeight.lessThan(weightArray[i])) { //Start if
            maxWeight = weightArray[i];
        } //end if 
    }// end for
    return maxWeight;

} //end findMax
private static Weight findAverage(Weight[] weightArray, Integer weights) { // Start findAverage
    Weight totalWeight = weightArray[0];
    for (int i = 0; i < weights; i++) { // Start for
        totalWeight.addTo(weightArray[i]);
    } // End for
    totalWeight.divide(weights);
    return totalWeight;
} // End findAverage


}
    public static void main(String[] args) { // Start Main 

   System.out.println(findAverage(weightArray, Integer weights));
    System.out.println(findMaximum());
    System.out.println(findMinimum());


    Weight[] weightArray;  
    JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
    int returnValue = jfc.showOpenDialog(null);
    File selectedFile;
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        selectedFile = jfc.getSelectedFile();
    }
    else {
        System.out.println("Process Canceled!");
        return;
    }
    int lines = 0;
    Scanner sc;
    try {
        sc = new Scanner(selectedFile);
    }
    catch (FileNotFoundException ex) {
        System.out.println("File Not Found!");
        return;
    }
    String ln;
    // Count number of data lines in data file.
    while (sc.hasNextLine()) {
        ln = sc.nextLine().trim();
        if (ln.equals("")) {
            continue;
        }
        lines++;
    }
    sc.close(); 
    // Initialize the weightArray to proper length
    weightArray = new Weight[lines];   
    lines = 0;  // Reset lines to 0 for the re-read.
    // Re-Read data file to acquire data. 


    try (Scanner scanner = new Scanner(selectedFile)) {
        String dataLine = "";
        while (scanner.hasNextLine()) {
            // Read file line and trim leading/trailing whitespaces.
            dataLine = scanner.nextLine().trim();
            // Skip Blank Lines
            if (dataLine.equals("")) {
                continue;
            }
            lines++;  // Actual Data Line Counter.

            if (lines >= 25) {
                System.out.println("The number of lines has exceeded 25 lines maximum!");
                System.exit(0);
            }//End if 
            else {

                String[] arrOfStr = dataLine.split("\\s{0,},\\s{0,}");
                weightArray[lines - 1] = new Weight(Integer.parseInt(arrOfStr[0]),
                Integer.parseInt(arrOfStr[1]));

            }
        }
    }
    catch (FileNotFoundException e) { // Start catch
    }
    // Display the weightArray
    for (int i = 0; i < weightArray.length; i++) {
         if (weightArray[i] != null) {
            System.out.println(weightArray[i].toString());
        }
    }

 }
}
java arrays max average min
1个回答
0
投票

根据您的最新帖子,我可以指出您犯的一些错误。

错误在您的主要方法的三行上:

System.out.println(findAverage(weightArray, Integer weights));
System.out.println(findMaximum());
System.out.println(findMinimum());
  1. 您在Weight类中定义了这三个方法,但是您正在ProjectOne主方法中调用它。编译器抱怨找不到这三种方法。

调用这三个函数的正确方法是

System.out.println(Weight.findMinimum()); 
  1. 此行是错误的!

要将数据传递给方法,我们不需要数据类型。因此,请删除Integer

   System.out.println(Weight.findAverage(weightArray, Integer weights));

最后,您要传递给这三个函数的两个参数是什么?尚不清楚您要实现什么。

© www.soinside.com 2019 - 2024. All rights reserved.