我很抱歉,但我是编程新手,我在编写程序时遇到了困难。原程序如下:
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
class buildABoat{
String boatName; // Boat name
void buildABoat(){
String BoatName;
}
void nameTheBoat() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\nWhat should we name this vessel? \n\n");
this.boatName = br.readLine();
System.out.println("\n\nThe " + boatName + " is ready to sail!\n");
}
catch (IOException e) {
}
}
}
class proj1{
public static void main(String[] arg){
buildABoat boat1;
buildABoat boat2;
buildABoat boat3;
buildABoat boat4;
buildABoat boat5;
boat1 = new buildABoat();
boat2 = new buildABoat();
boat3 = new buildABoat();
boat4 = new buildABoat();
boat5 = new buildABoat();
boat1.nameTheBoat();
boat2.nameTheBoat();
boat3.nameTheBoat();
boat4.nameTheBoat();
boat5.nameTheBoat();
System.out.println("(Press ENTER to exit)");
try {
System.in.read();
}
catch (IOException e) {
return;
}
}
}
这会产生以下结果:
What should we name this vessel?
Enterprise
The Enterprise is ready to sail!
What should we name this vessel?
Columbia
The Columbia is ready to sail!
What should we name this vessel?
Challenger
The Challenger is ready to sail!
What should we name this vessel?
Atlantis
The Atlantis is ready to sail!
What should we name this vessel?
Endeavor
The Endeavor is ready to sail!
(Press ENTER to exit)
我试图将其更改为以下内容:
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
class buildABoat{
String boatName; // Boat name
void buildABoat(){
String BoatName;
}
void nameTheBoat() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\nWhat should we name this vessel? \n\n");
this.boatName = br.readLine();
System.out.println("\n\nThe " + boatName + " is ready to sail!\n");
}
catch (IOException e) {
}
}
}
class proj1{
public static void main(String[] arg){
Boat[] boat;
boat = new Boat[5];
for(int i = 0; i <= Boat.Length; i++){
nameTheBoat();
}
System.out.println("(Press ENTER to exit)");
try {
System.in.read();
}
catch (IOException e) {
return;
}
}
}
这当然会产生以下错误:
proj1.java:71: error: cannot find symbol
for(int i = 0; i <= Boat.Length; i++){
^
symbol: variable Length
location: class Boat
proj1.java:73: error: cannot find symbol
nameTheBoat();
^
symbol: method nameTheBoat()
location: class proj1
2 errors
我在新程序中缺少什么?