简体   繁体   English

Java程序给出错误

[英]Java program gives error

I am a beginner in java, I have tried to solve the below program, but getting error, can anyone tell me where I am doing mistake? 我是Java的初学者,我曾尝试解决以下程序,但遇到错误,有人可以告诉我我在哪里做错吗?

public class TestGlass 
    { 
       public static void main(String [] args) 
       { 
          Glass milk = new Glass(15); // 15 ounces of milk 
          Glass juice = new Glass(3); // 3 ources of juice



      milk.drink(2); 
      milk.drink(1); 

      milk.report(); 

      juice.fill(6);  // went from 3 to 9 ounces 
      juice.drink(1); // now down to 8 ounces 

      juice.report();  

      juice.spill(); 

      juice.report(); 
   } 
} 

class Glass 
{ 

     int ounce;

     public void spill()
     {
       ounce = 0;
     }


     public void drink(int x){
        ounce = ounce-x;
     }

     public void fill(int x){
        ounce = ounce+x;
     }

     public int getOunce()
     {
       return ounce;  
     }


     public void report()
     {
       int x = getOunce();
       System.out.println("Glass has " + x + " ounces");
     }

} 

Here is the error, 这是错误,

TestGlass.java:5: error: constructor Glass in class Glass cannot be applied to given types;
      Glass milk = new Glass(15); // 15 ounces of milk 
                   ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
TestGlass.java:6: error: constructor Glass in class Glass cannot be applied to given types;
      Glass juice = new Glass(3); // 3 ources of juice 
                    ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
2 errors

You need to add a constructor to Glass that accepts your ounce parameter. 您需要向Glass添加一个接受ounce参数的构造函数。

class Glass {
....
    public Glass (int ounce) {
        this.ounce = ounce;
    }
....
}

The constructor is the method that is called when you use the new operator. 构造函数是使用new运算符时调用的方法。 Its job is to initialize - to create - the object instance of the class. 它的工作是初始化-创建-类的对象实例。 A constructor with one or more arguments, like this one, is set up to receive values to initialize the instance variables of the class. 具有一个或多个参数的构造函数(如该参数)被设置为接收值以初始化类的实例变量。

Notice how the error message already mentions a constructor. 注意错误消息中已经提到了构造函数。 That is because if you do not specify your own constructor, Java adds a default constructor that receives no arguments. 这是因为,如果您不指定自己的构造函数,则Java将添加一个默认构造函数,该构造函数不接收任何参数。 That default no-arg constructor was what was being called when you called new . 默认的无参数构造函数是您调用new时所调用的构造函数。 Since you were passing arguments to a no-arg constructor, you got the error. 由于您将参数传递给无参数构造函数,因此出现了错误。 Once you add your own constructor, the default no-arg constructor goes away. 一旦添加了自己的构造函数,默认的no-arg构造函数就会消失。 If you want to have a no-arg version as well (eg setting ounce to 0 or to a default value), you can bring it back by specifying it along with the one I gave you - that is you can overload the constructor (see links below). 如果您也想使用无参数版本(例如,将ounce设置为0或默认值),则可以通过将其与我给您的版本一起指定来将其带回来-也就是说,您可以重载构造函数(请参见下面的链接)。

 class Glass {
    ....
        public Glass () {
            this.ounce = 1;  
            /* In this setup, a glass always has at least 1 ounce */
            /* If you want it be 0, you could say this.ounce = 0, or */
            /* just leave everything inside {} blank, since ounce will */
            /* default to 0 anyway */
        }

        public Glass (int ounce) {
            this.ounce = ounce;
        }
    ....
    }

Calling new Glass() would call that first no-arg constructor. 调用new Glass()将调用第一个无参数构造函数。 Calling new Glass(15) would call the second constructor, the one that takes an argument. 调用new Glass(15)将调用第二个构造函数,该构造函数接受一个参数。

Here's a nice tutorial on constructors. 这是一个关于构造函数的不错的教程

Here's a nice tutorial on overloading constructors. 这是有关重载构造函数的不错的教程

You need to write a constructor for Glass that takes an argument. 您需要为Glass编写一个带有参数的构造函数。

class Glass { 
     int ounce;

     public Glass(int ounce) {
         this.ounce = ounce;
     }

     public void spill()
     {
       ounce = 0;
     }


     public void drink(int x){
        ounce = ounce-x;
     }

     public void fill(int x){
        ounce = ounce+x;
     }

     public int getOunce()
     {
       return ounce;  
     }


     public void report()
     {
       int x = getOunce();
       System.out.println("Glass has " + x + " ounces");
     }

} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个程序中运行Java程序会出现错误 - Running Java Program in another Program gives error WordNet导出CSV Java程序出现错误 - WordNet Export CSV Java program gives error 简单的scala程序给出错误:java.lang.IncompatibleClassChangeError - simple scala program gives Error: java.lang.IncompatibleClassChangeError 从命令行在Java程序中传递运行参数会产生错误 - Passing run arguments in a java program from command line gives an error 我在 Java 中的程序在计算机上编译,但在网站 URI 上出现运行时错误 - My program in Java compiles on the computer, but gives runtime error on the website URI Java程序运行但没有输出 - Java Program runs but gives no output 我的 java 编译器在命令提示符下处理我的程序,但是当我尝试运行该程序时,它给了我一个错误 - My java compiler works on my program in the command prompt but when i try to run the program it gives me an error 程序进行编译,但随后在线程“ main”中给出错误Exception java.util.IllegalFormatConversionException:d!= java.lang.Double? - Program compiles but then gives error Exception in thread “main” java.util.IllegalFormatConversionException: d != java.lang.Double? IntelliJ 2021.2.2 在编译程序时给出错误“java:错误:无效的源版本:18” - IntelliJ 2021.2.2 gives error 'java: error: invalid source release: 18' when compiling program 使用ProcessBuilder从Java程序运行yarn工作会给出文件不存在错误 - Running yarn job from java program using ProcessBuilder gives file does not exist error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM