저는 Java에서 초보자입니다. 아래 프로그램을 해결하려고 시도했지만 오류가 발생했습니다. 누군가 내가 실수를하고 있다고 말할 수 있습니까? 여기 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");
}
}
당신은 당신의
ounce
매개 변수를 받아
Glass
에 생성자를 추가 할 필요가
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
'Glass' 클래스에 생성자가 없습니다. –
* 생성자 *가 무엇인지 아십니까? 그렇지 않다면 https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – Pshemo
을 읽어야합니다. 향후 작업에 유용한 힌트가 있습니다. http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –