【问题标题】:I am receiving the error error: '.class' expected我收到错误错误:预期为“.class”
【发布时间】:2013-04-05 16:29:30
【问题描述】:

我对 Java 比较陌生,我决定制作这个程序,因为开始学习如何使用方法似乎是一项足够简单的任务。 无论如何,这是我现在的程序。

    import javax.swing.JOptionPane;
    public class smallAverage{
    public smallAverage getsmall() {

            String sd1 = JOptionPane.showInputDialog("What is the first number?");
    double sdx = Double.parseDouble (sd1);
            String sd2 = JOptionPane.showInputDialog("What is the second number?");
    double sdy = Double.parseDouble (sd2);
            String sd3 = JOptionPane.showInputDialog("What is the third number?");
    double sdz = Double.parseDouble (sd3);

            return double sdx;
    return double sdy;
    return double sdz;
    }

    public smallAverage getavg() {

            String ad1 = JOptionPane.showInputDialog("What is the first number");
    double adx = Double.parseDouble (ad1);
            String ad2 = JOptionPane.showInputDialog("What is the second number");
    double ady = Double.parseDouble (ad2);
            String ad3 = JOptionPane.showInputDialog("What is the third number");
    double adz = Double.parseDouble (ad3);


            return double adx;
    return double ady;
    return double adz;
    }

     public smallAverage work() {
    double small = Math.min(sdx, sdy, sdz);
    double avg = (adx + ady + adz) / 3;

            System.out.println("The smallest of "+ sdx+ sdy+ sdz+ " is " + small + ", and the average of"+ adx+ ady+ adz+ " is "+ avg+ ".");
    }
    }

这里发生的实际数学对我来说真的毫无意义,我想学习的是代码的工作方式。我尝试过用许多不同的方式重写这段代码,但这似乎是最接近正确的。 我只是不知道为什么它不起作用。 当我尝试通过终端编译时,我得到了这个:

    smallAverage.java:12: error: '.class' expected
                    return double sdx;
                                  ^
    smallAverage.java:13: error: '.class' expected
            return double sdy;
                          ^
    smallAverage.java:14: error: '.class' expected
            return double sdz;
                          ^
    smallAverage.java:27: error: '.class' expected
                    return double adx;
                                  ^
    smallAverage.java:28: error: '.class' expected
            return double ady;
                          ^
    smallAverage.java:29: error: '.class' expected
            return double adz;
                          ^
    6 errors

任何帮助将不胜感激

【问题讨论】:

  • 您的代码在很多方面都是错误的,包括您的方法被定义为返回 smallAverage 对象。重新阅读有关如何创建方法的章节。

标签: java class methods compiler-errors arguments


【解决方案1】:

当您返回时,您不再需要输入说明符:

 return double adx;
 return double ady;
 return double adz;

应该是

 return adx;
 return ady;
 return adz;

其他三个同样的错误。

同时。

public smallAverage getsmall()

是错误的,因为您没有从该函数返回类对象。如果没有任何不同的数据路径,您不能将 3 个 return 语句放在一个彼此靠近的函数中。例如:

 return adx;
 return ady;  //two return statements below will never be reached.
 return adz;

这些是基本错误,您可能需要查看一些基本的 Java 教程:Java Tutorial

【讨论】:

  • 感谢您的帮助。我点击了链接,它让我走上了正确的道路。如果有人从搜索引擎中找到此内容,请点击链接。 [如果您需要提示,这里有 3 个; 1:)我再次重写了整个代码以使其工作。 2:) 您必须提交一些参考方法指南。 3:) 注意你的返回类型。]
【解决方案2】:

你只需要这样做,即

return sdx;

因为 type 不是必需的,因为您在声明 sdx 时已经定义了。

建议使用 Eclipse 之类的 IDE,因为它甚至在不运行程序的情况下就指出编译错误。对开发很有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多