java - 这个Java程序是否需要是两个独立的文件

标签 java

大家好,我正在上 Java 课,这是我的第一个涉及面向对象编程的作业。我遇到了 SimpleCalc 部分的问题,想知道我的工作是否应该是两个单独的文件,或者我是否缺少一个允许 StatCalc 部分和 SimpleCalc 部分相互对话的组件。请记住,我是 Java 的新手,所以我可能需要比过去有时在堆栈溢出流上看到的更多的拼写,但是,我将不胜感激任何帮助,所以提前谢谢你。这是我的代码:

package tutorial; 
/*   
* An object of class StatCalc can be used to compute several simple statistics  
* for a set of numbers.  Numbers are entered into the dataset using  
* the enter(double) method.  Methods are provided to return the following  
* statistics for the set of numbers that have been entered: The number  
* of items, the sum of the items, the average, the standard deviation,  
* the maximum, and the minimum.  
*/ public class StatCalc {

           private int count;   // Number of numbers that have been entered.
           private double sum;  // The sum of all the items that have been entered.
           private double squareSum;  // The sum of the squares of all the items.        
           private double max = Double.NEGATIVE_INFINITY;                               private double min = Double.POSITIVE_INFINITY;
           /**
            * Add a number to the dataset.  The statistics will be computed for all
            * the numbers that have been added to the dataset using this method.
            */
           public void enter(double num) {
                  count++;
                  sum += num;
                  squareSum += num*num;               
                  if (count == 1){
                      max = num;
                      min = num;
                  }
                  else {
                      if (num > max)
                          max = num;
                      if (num < min)
                          min = num;
                  }     
           }
           /**

            * Return the number of items that have been entered into the dataset.

            */

           public int getCount() {

                  return count;
           }
           /**

            * Return the sum of all the numbers that have been entered.

            */

           public double getSum() {

                  return sum;
           }         
           /**

            * Return the average of all the items that have been entered.

            * The return value is Double.NaN if no numbers have been entered.

           */

           public double getMean() {

                  return sum / count;
           }     

           /**

            * Return the standard deviation of all the items that have been entered.

            * The return value is Double.NaN if no numbers have been entered.

            */

           public double getStandardDeviation() {

                  double mean = getMean();

                  return Math.sqrt( squareSum/count - mean*mean );
           }

           public double getMin(){
               return min;
           }
           public double getMax(){
               return max;
           }        }// end class StatCalc

public class SimpleCalc {
    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        SimpleCalc calc;
        calc = new SimpleCalc();

        double item;
        System.out.println("Enter numbers here. Enter 0 to stop.");       
        System.out.println();

        do{ 
            System.out.print("? ");
            item = in.nextDouble();

            if (item != 0)
                calc.enter(item);
        }while (item != 0);

        System.out.println("\nStatistics about your calc:\n");   
        System.out.println(Count: "+calc.getCount"()); 
        System.out.println(Sum: "+calc.getSum"());   
        System.out.println("Minimum: "+calc.getMin());  
        System.out.println("Maximum: "+calc.getMax());  
        System.out.println("Average: "+calc.getMean());     
        System.out.println("Standard Deviation: "+calc.getStandardDeviation());     
    }// end main
}//end SimpleCalc

最佳答案

在 Java 中,公共(public)类必须位于与该类同名的文件中。因此,由于您有一个名为 StatCalc 的公共(public)类,因此文件名必须是 StatCalc.java。同样,第二类也是公开的,因此它必须在自己的文件中。

关于java - 这个Java程序是否需要是两个独立的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227239/

相关文章:

java - 保存/更新具有多对多关系的 Hibernate 实体

javascript - 如何在前端 react 中设置后端URL

java - ClassNotFoundException : com. 谷歌.gwt.junit.server.JUnitHostImpl

java - 该方法只能设置 public/protected/private 之一

java - 如何随机生成 75 - 100% 之间的数字?

java - java在屏幕上显示鼠标坐标的问题

java - Wildfly 8.2/Undertow - 属性占位符似乎不适用于主机配置中的别名属性

Java - 停止 ExecutorService 中的所有任务

java - 使用 Xuggler 从 mp4 创建 mp3

java - 尝试获取 EJB 时出现 JBoss 4.2.3 NullPointerException