抱歉,我是编程新手,我在编写程序时遇到困难。原始程序如下:

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


我在新程序中缺少什么?

最佳答案

你在哪里

nameTheBoat();


在for()循环中,您需要

boat[i] = new Boat();
boat[i].nameTheBoat();


原因如下:
1)nameTheBoat()是仅对Boat类型的对象进行操作的方法。您没有给它任何要处理的对象。
2)boat[] = new Boat[5];初始化一个Array对象,但没有在Boat对象中创建5个新的Array。因此,您需要先创建这5个Boat中的每一个,然后才能对它们运行Boat方法。否则,您将得到一个空指针错误。

编辑:当然,正如其他人提到的,boat.length是数组boat的长度,boat.Length是错误的。

请享用!

10-07 19:22