这个Java程序需要是两个独立的文件吗

这个Java程序需要是两个独立的文件吗,java,Java,大家好,我正在学习Java课程,这是我的第一个涉及面向对象编程的作业。我在SimpleCalc部分遇到了一些问题,我想知道我的工作应该是两个独立的文件,还是缺少一个允许StatCalc部分和SimpleCalc部分相互对话的组件。请记住,我是Java新手,所以我可能需要比我过去在stack over flow上看到的更多的详细说明,但是,我将感谢您的帮助,因此提前感谢您。这是我的密码: package tutorial; /* * An object of class StatCalc

大家好,我正在学习Java课程,这是我的第一个涉及面向对象编程的作业。我在SimpleCalc部分遇到了一些问题,我想知道我的工作应该是两个独立的文件,还是缺少一个允许StatCalc部分和SimpleCalc部分相互对话的组件。请记住,我是Java新手,所以我可能需要比我过去在stack over flow上看到的更多的详细说明,但是,我将感谢您的帮助,因此提前感谢您。这是我的密码:

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
软件包教程;
/*   
*StatCalc类的对象可用于计算几个简单的统计数据
*对于一组数字。使用将数字输入到数据集中
*enter(double)方法。方法返回以下内容
*已输入数字集的统计信息:数字
*项目总数、项目总数、平均值、标准差、,
*最大值和最小值。
*/公共级StatCalc{
private int count;//已输入的数字数。
private double sum;//输入的所有项目的总和。
private double squareSum;//所有项的平方和。
private double max=double.NEGATIVE无限;private double min=double.NEGATIVE无限;
/**
*向数据集中添加一个数字。将计算所有数据的统计信息
*使用此方法添加到数据集的数字。
*/
公共无效输入(双数字){
计数++;
sum+=num;
平方和+=num*num;
如果(计数=1){
max=num;
min=num;
}
否则{
如果(数值>最大值)
max=num;
if(num
是的,它需要两个文件


Java的约定是每个“顶级”公共类都需要自己的文件。

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

很抱歉,我以后会记住这一点。啊,这完全有道理。因此,我尝试使用两个单独的文件,但在网上看到我将使用scanner,以便让这两个文件相互识别,当我按照我看到的步骤操作时,我仍然无法解决问题。让两个文件相互查看的正确方式是什么?我不确定我的老师是否正确地解释了它,也不确定这两个文件是否可以在同一个包中,相同的src,等等@MadProgrammer让我们不要太挑剔,因为OP似乎在挣扎一些基本语法。您引用的示例涉及嵌套的静态类。虽然绝对正确,但我不认为这有助于回答这个问题。@Jerlip不知道你在问什么。你们互相认识是什么意思?如果这两个文件在同一个目录中,并且都在同一个包中,那么如果您试图从另一个文件中引用其中一个文件,它们应该只是“看到”彼此。@VincentRamdhanie可能不是,但是“在Java中,公共类必须”语句是错误的…所以如果两个文件都在同一个包中,那么它们应该可以看到对方,而无需在java文件中包含任何内容?我想我只是不确定,因为在过去,我的老师会使用文本IO文件和文本文件,所以我不确定是否适用相同的步骤