0

我的任务是编写 的重载版本iquote(),该方法显示其参数的类型和用双引号括起来的参数。我被要求写三个版本:一个用于int论证,一个用于double论证,一个用于String论证。我不需要提供应用程序部分。

这是我到目前为止所拥有的,它会编译但是会收到一条错误消息:Could not find or load main class assign61

有人可以帮我这个代码...

public class assign61 {
     public void iquote(String s){
        return sQuote;
        System.out.println( "sQuote\" );" 
    }
    public void iquote(int n){
        return iQuote;
        System.out.println( "sQuote\" );" 
    }       
    public void iquote(double d){
        return iQuote;
        System.out.println( "sQuote\" );"          
    }
}
4

2 回答 2

2

您的代码无法正确编译,因为其中存在错误。因此,如果它不能编译,它就不能运行。

您需要进行一些更改...

public class assign61 {

    public assign61(){
        iquote("Test");
        iquote("123");
        iquote("5.678");
        }

    public void iquote(String s){
        System.out.println("sQuote:" + s);
    }
    public void iquote(int n){
        System.out.println("iQuote:" + n);
    }       
    public void iquote(double d){
        System.out.println("dQuote:" + d);       
    }

    public static void main(String[] args){
        new assign61();
    }

}

基本上这是需要改变的......

  1. 您要输出该值,因此将其System.out.println()通过+符号添加到行尾。
  2. 您的方法中有return语句,但它们不是您想要使用的。return用于将值返回给调用该方法的代码 - 它们不是用于将值输出到命令提示符。
  3. 由于您的return语句,它们也会阻止您的代码编译,因为您将它们写在语句之前System.out.println()而不是之后return在代码中后面不能写任何东西。此外,要使用return语句,您需要将方法更改为使用语句public int iQuote(int n)而不是使用void语句,因此它知道您将从方法返回的数据类型
  4. 我添加了一个构造方法assign61,其中包含一些测试代码,以表明您的其他方法可以正常工作。我还添加了该main方法,以便您可以运行您的代码。

I hope this helps you to understand a little more. Try making some of the changes suggested above, then compile your code. Once it compiles correctly, you will be able to run it. Then you can come back to us with any further problems.

于 2012-06-21T02:56:40.543 回答
1
Could not find or load main class assign61

我认为最可能的原因可能是您没有将文件命名为 assign61.java (顺便说一句,您应该按照惯例将类名大写。)

同样在这个时候,要运行你的类,至少你应该有一个 main 方法。java的一个方便的方法是在这个类中创建一个main方法。

于 2012-06-21T02:53:32.200 回答