/* Airport class
Anderson, Franceschi
*/
public class Airport
{
public static void main(String[] args)
{
// 1. ***** Define the instance variables *****
// airportCode is a String
// gates is an integer
// Part 1 student code starts here:
private String airportCode;
private int gates;
// Part 1 student code ends here.
// 2. ***** Write this method *****
// Default constructor:
// method name: Airport
// return value: none
// parameters: none
// function: sets the airportCode to an empty String
// Part 2 student code starts here:
public Airport()
{
airportCode = "";
}
// Part 2 student code ends here.
// 3. ***** Write this method *****
// Overloaded constructor:
// method name: Airport
// return value: none
// parameters: a String startAirportCode and an int startGates
// function:
// calls the the setAirportCode method,
// passing startAirportCode parameter;
// calls the setGates method, passing the startGates parameter
// Part 3 student code starts here:
public Airport(String startAirportCode, int startGates)
{
setAirportCode(startAirportCode);
setGates(startGates);
}
// Part 3 student code ends here.
// 4. ***** Write this method *****
// Accessor method for the airportCode instance variable
// method name: getAirportCode
// return value: String
// parameters: none
// function: returns airportCode
// Part 4 student code starts here:
public String getAirportCode()
{
return airportCode;
}
// Part 4 student code ends here.
// 5. ***** Write this method *****
// Accessor method for the gates instance variable
// method name: getGates
// return value: int
// parameters: none
// function: returns gates
// Part 5 student code starts here:
public int getGates()
{
return gates;
}
// Part 5 student code ends here.
// 6. ***** Write this method *****
// Mutator method for the airportCode instance variable
// method name: setAirportCode
// return value: void
// parameters: String newAirportCode
// function: assigns airportCode the value of the
// newAirportCode parameter
// Part 6 student code starts here:
public void setAirportCode(String newAirportCode)
{
airportCode = newAirportCode;
}
// Part 6 student code ends here.
// 7. ***** Write this method *****
// Mutator method for the gates instance variable
// method name: setGates
// return value: void
// parameters: int newGates
// function: validates the newGates parameter.
// if newGates is greater than or equal to 0,
// sets gates to newGates;
// otherwise, prints an error message to System.err
// and does not change value of gates
// Part 7 student code starts here:
public void setGates(int newGates)
{
if (newGates >= 0)
gates = newGates;
else
{
System.err.println("Gates must be at least 0.");
System.err.println("Value of gates unchanged.");
return;
}
/*public void setGates(int newGates)
{
newGates = gates;
}
*/
// Part 7 student code ends here.
enter code here
} // end of Airport class definition
}
书中的代码不包含公共静态 void main(String[] args(。一旦我搜索了该网站,我还是添加了它,它说你不能用最近的 java 更新做到这一点。在没有 main 语句的情况下,我让它成功编译,但有一个运行时错误,说"在机场类中找不到 main 方法"。我添加了它并遇到了一些编译器错误。我玩了一下花括号,最终打破了它......现在我不知道为什么我会得到 23 个合成错误。
我正在使用Java外部工具在文本板中编码。它适用于主类和 StackFlow 的方法,但并非没有主类 - 仍然说它找不到该类。我从实例变量中删除了"私有"。它在没有它的情况下成功编译,但仍然存在上述运行时错误。这个文件并没有真正运行,但它将与另一个为我编码的框架一起使用。这只是第 1 部分;第 2 部分的基础。
对于其他询问我的人,我相信它"仅适用于 java 1.6 或更低版本"Java 程序如何在不定义 main 方法的情况下运行?
再次感谢大家的帮助。这个网站真棒!
这应该可以解开(但不能修复(这一点:
public class Airport
{
public static void main(String[] args)
{
System.out.println("Running. But what do I have to do?");
} //CLOSE THAT MAIN <- need to close that method.
[... all you code ...]
} // end of Airport class definition
//Remove this. Class already ended. }
从那里开始,你应该做两件事:找出main((中应该发生什么,并安装一个带有语法高亮和eclipse之类的IDE。这将为您指出这些类型的错误。
你不能做
public static void main(String[] args)
{
private String airportCode;
private int gates;
}
你的变量在主函数之外或将它们声明为局部变量。在启动其他函数之前,您还需要关闭 main。
您更正的代码如下所示:
/* Airport class
Anderson, Franceschi
*/
public class Airport
{
public static void main(String[] args)
{
}
// 1. ***** Define the instance variables *****
// airportCode is a String
// gates is an integer
// Part 1 student code starts here:
private String airportCode;
private int gates;
// Part 1 student code ends here.
// 2. ***** Write this method *****
// Default constructor:
// method name: Airport
// return value: none
// parameters: none
// function: sets the airportCode to an empty String
// Part 2 student code starts here:
public Airport()
{
airportCode = "";
}
// Part 2 student code ends here.
// 3. ***** Write this method *****
// Overloaded constructor:
// method name: Airport
// return value: none
// parameters: a String startAirportCode and an int startGates
// function:
// calls the the setAirportCode method,
// passing startAirportCode parameter;
// calls the setGates method, passing the startGates parameter
// Part 3 student code starts here:
public Airport(String startAirportCode, int startGates)
{
setAirportCode(startAirportCode);
setGates(startGates);
}
// Part 3 student code ends here.
// 4. ***** Write this method *****
// Accessor method for the airportCode instance variable
// method name: getAirportCode
// return value: String
// parameters: none
// function: returns airportCode
// Part 4 student code starts here:
public String getAirportCode()
{
return airportCode;
}
// Part 4 student code ends here.
// 5. ***** Write this method *****
// Accessor method for the gates instance variable
// method name: getGates
// return value: int
// parameters: none
// function: returns gates
// Part 5 student code starts here:
public int getGates()
{
return gates;
}
// Part 5 student code ends here.
// 6. ***** Write this method *****
// Mutator method for the airportCode instance variable
// method name: setAirportCode
// return value: void
// parameters: String newAirportCode
// function: assigns airportCode the value of the
// newAirportCode parameter
// Part 6 student code starts here:
public void setAirportCode(String newAirportCode)
{
airportCode = newAirportCode;
}
// Part 6 student code ends here.
// 7. ***** Write this method *****
// Mutator method for the gates instance variable
// method name: setGates
// return value: void
// parameters: int newGates
// function: validates the newGates parameter.
// if newGates is greater than or equal to 0,
// sets gates to newGates;
// otherwise, prints an error message to System.err
// and does not change value of gates
// Part 7 student code starts here:
public void setGates(int newGates)
{
if (newGates >= 0)
gates = newGates;
else
{
System.err.println("Gates must be at least 0.");
System.err.println("Value of gates unchanged.");
return;
}
/*public void setGates(int newGates)
{
newGates = gates;
}
*/
// Part 7 student code ends here.
enter code here
}
机场类定义结束 }
有所帮助!!正如上面有人所说,
你的变量在主函数之外或将它们声明为局部变量。在启动其他函数之前,您还需要关闭 main。
public class Airport
{
String airportCode;
int gates;
public Airport()
{
airportCode = "";
}
public Airport(String startAirportCode, int startGates)
{
setAirportCode(startAirportCode);
setGates(startGates);
}
public String getAirportCode()
{
return airportCode;
}
public int getGates()
{
return gates;
}
public void setAirportCode(String newAirportCode)
{
airportCode = newAirportCode;
}
public void setGates(int newGates)
{
if (newGates >= 0)
gates = newGates;
else
{
System.err.println("Gates must be at least 0.");
System.err.println("Value of gates unchanged.");
return;
}
}
public static void main(String[] args) {
Airport r = new Airport("Hello", 1);
r.setAirportCode("Abcd");
System.out.println("" + r.getAirportCode());
r.setGates(3);
System.out.println("" + r.getGates());
}
}
你不需要 main 方法。 如果删除这些行:
public static void main(String[] args)
{
并在底部注释掉这一行:
enter code here
它应该编译:javac Airport.java
。 这个机场对象听起来像是你不运行的东西......只要建造它,它就会表现得像一个机场。 我猜在课程的后面,你会制作一些使用机场的东西......就像机场模拟器.java。 它可能有一个 main 方法,看起来像这样:
public class AirportSimulator
{
public static void main(String[] args)
{
AirportSimulator airsim = new AirportSimulator();
airsim.simulate();
}
public void simulate()
{
Airport air = new Airport();
System.out.println("Setting gates to 1");
air.setGates(1);
System.out.println(String.format("Gates set to %d", air.getGates());
System.out.println("Setting airport code to IAD");
air.setAirportCode("IAD");
System.out.println(String.format("Airport code is %s", air.getAirportCode());
}
}
我的观点是,模拟器就像一个动词,更有意义,你可以用一个主线运行它,而机场就像一个名词。