如何在循环中打印方法

时间:2021-03-24 07:35:43

I apologize but I am new to programming and I'm having difficulty coding a program. The original program is as follows:

我很抱歉,但我是编程新手,编写程序时遇到了困难。原计划如下:

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;
        }
    }
}

This produces the following:

这产生以下结果:

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)

I tried to change this to the following:

我试图将其更改为以下内容:

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;
        }
    }
}

This of course produces the following error:

这当然会产生以下错误:

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

What am I missing in the new program?

我在新计划中缺少什么?

8 个解决方案

#1


1  

where you have

你在哪里

nameTheBoat();

in your for() loop you need to have

在你的for()循环中你需要有

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

The reasons are: 1) nameTheBoat() is a method that only operates on objects of type Boat. You are not giving it any object to work on. 2) boat[] = new Boat[5]; initializes an Array object, but doesn't create the 5 new Boats in the Array object. So you need to create each of those 5 Boats before you can run a Boat method on them. Otherwise you'll get a null pointer error.

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

EDIT: and of course as others mentioned, boat.length is the length of the array boat, boat.Length is wrong.

编辑:当然,正如其他人提到的,boat.length是阵列船的长度,船。长度错了。

Enjoy!

请享用!

#2


2  

use lowercased length

使用较小的长度

for(int i = 0; i <= Boat.length; i++)

#3


2  

As Nurlan mentions, the first error is because you're using a capital L for the length property of an array. The actual property begins with a lowercase l.

正如Nurlan所提到的,第一个错误是因为你使用大写L来表示数组的长度属性。实际属性以小写l开头。

The second error is because the method nameTheBoat is part of the class named buildABoat, but you're trying to invoke it as if it were part of your main class. You need an instance of a buildABoat object in order to invoke this method.

第二个错误是因为方法nameTheBoat是名为buildABoat的类的一部分,但是您尝试调用它,就像它是主类的一部分一样。您需要一个buildABoat对象的实例才能调用此方法。

One other suggestion: to comply with java naming conventions, you should NOT begin the name of a class with a lowercase letter. Classes should always start with a capital letter. Methods should always begin with a lowercase letter.

另一个建议是:要遵守java命名约定,不应该以小写字母开头的类的名称。类应始终以大写字母开头。方法应始终以小写字母开头。

#4


2  

You must try to calculate the length on the object rather than the class. So, it should be boat.length, where boat is your object here

您必须尝试计算对象而不是类的长度。所以,它应该是boat.length,船在这里是你的对象

You call a method using the class name, if the method is a static method.

如果方法是静态方法,则使用类名调用方法。

#5


2  

Try doing boat.nameTheBoat() instead of just nameTheBoat()

尝试使用boat.nameTheBoat()而不仅仅是nameTheBoat()

Also

for(int i = 0; i <= boat.length; i++){          
        boat[i].nameTheBoat();          
    }    

boat is the instance of the class Boat (probably should be buildABoat). These look like they're compiler errors and not runtime errors, so the compiler should give you some hint (as it does) as to the exact line numbers.

船是班级船的实例(可能应该是buildABoat)。这些看起来像是编译器错误而不是运行时错误,因此编译器应该给出一些提示(就像它一样)来确切的行号。

#6


0  

The object array you have created is boat = new Boat[5];. Ideally you whole code should be like this

你创建的对象数组是boat = new Boat [5];。理想情况下,您的整个代码应该是这样的

Boat[] boat = new Boat[5];    
for(int i = 0; i <= Boat.Length; i++){          
boat[i] = new Boat();
boat[i].nameTheBoat();          
}

#7


0  

You have a few problems.

你有一些问题。

One is that boat as an array

一个是船作为阵列

Boat[] boat;            
boat = new Boat[5];    
for(int i = 0; i <= Boat.Length; i++){          
    nameTheBoat();          
} 

To access the array length it would be boat.length instead of what you have.

要访问数组长度,它将是boat.length而不是你拥有的。

The next problem I see is your call to nameTheBoat(). That method is part of the buildABoat class.

我看到的下一个问题是你对nameTheBoat()的调用。该方法是buildABoat类的一部分。

#8


0  

This code looks like a constructor with accident.

此代码看起来像一个意外的构造函数。

class buildABoat{    
    String boatName;      // Boat name

    void buildABoat(){
        String BoatName;
    }

If void buildABoat () would be a method (void says so), why would you declare a local variable in it, and not use it?

如果void buildABoat()是一个方法(void表示如此),为什么要在其中声明一个局部变量,而不是使用它?

And don't repeat the name of a well named attribute as comment.

并且不要将具有良好命名属性的名称重复为注释。

class BuildABoat {    
    private String boatName;   

    public BuildABoat (String name) {
        boatName = name;
    }

This would make some sense. You now have a class, which can be instantiated by passing a boatname, which is preserved in the class.

这有点道理。你现在有了一个类,它可以通过传递一个船名来实例化,该船名保存在类中。

In your Proj1-Class, you split declaration and initialisation. This is a bad habit. Sometimes it's not avoidable, but if it is, avoid it.

在Proj1-Class中,您可以拆分声明和初始化。这是一个坏习惯。有时这是不可避免的,但如果是,请避免它。

class Proj1 {

    public static void main(String[] arg){

        BuildABoat [] boat = new BuildABoat[5];    
        for(int i = 0; i <= boat.length; i++) {
            // to make this work, we have to change the 
            // buildABoat
            boat[i] = new BuildABoat (BuildABoat.nameTheBoat ());
        }
        System.out.println ("(Press ENTER to exit)");
        try {           
            System.in.read ();
        }           
        catch (IOException ignored) {         
            return;
        }
    }
}

The method nameTheBoat has to be static, if we call it without actually having a (builda)boat already.

方法nameTheBoat必须是静态的,如果我们在没有实际拥有(builda)船的情况下调用它。

public static String nameTheBoat () {
    try {           
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println ("\n\nWhat should we name this vessel? \n\n");
        String s = br.readLine ();
        System.out.println ("\n\nThe " + s + " is ready to sail!\n");
        return s;
    }
      catch (IOException e) {
        return "";
    }   
}

I hope the code does a little what you intended.

我希望代码能做到你想要的一点点。

#1


1  

where you have

你在哪里

nameTheBoat();

in your for() loop you need to have

在你的for()循环中你需要有

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

The reasons are: 1) nameTheBoat() is a method that only operates on objects of type Boat. You are not giving it any object to work on. 2) boat[] = new Boat[5]; initializes an Array object, but doesn't create the 5 new Boats in the Array object. So you need to create each of those 5 Boats before you can run a Boat method on them. Otherwise you'll get a null pointer error.

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

EDIT: and of course as others mentioned, boat.length is the length of the array boat, boat.Length is wrong.

编辑:当然,正如其他人提到的,boat.length是阵列船的长度,船。长度错了。

Enjoy!

请享用!

#2


2  

use lowercased length

使用较小的长度

for(int i = 0; i <= Boat.length; i++)

#3


2  

As Nurlan mentions, the first error is because you're using a capital L for the length property of an array. The actual property begins with a lowercase l.

正如Nurlan所提到的,第一个错误是因为你使用大写L来表示数组的长度属性。实际属性以小写l开头。

The second error is because the method nameTheBoat is part of the class named buildABoat, but you're trying to invoke it as if it were part of your main class. You need an instance of a buildABoat object in order to invoke this method.

第二个错误是因为方法nameTheBoat是名为buildABoat的类的一部分,但是您尝试调用它,就像它是主类的一部分一样。您需要一个buildABoat对象的实例才能调用此方法。

One other suggestion: to comply with java naming conventions, you should NOT begin the name of a class with a lowercase letter. Classes should always start with a capital letter. Methods should always begin with a lowercase letter.

另一个建议是:要遵守java命名约定,不应该以小写字母开头的类的名称。类应始终以大写字母开头。方法应始终以小写字母开头。

#4


2  

You must try to calculate the length on the object rather than the class. So, it should be boat.length, where boat is your object here

您必须尝试计算对象而不是类的长度。所以,它应该是boat.length,船在这里是你的对象

You call a method using the class name, if the method is a static method.

如果方法是静态方法,则使用类名调用方法。

#5


2  

Try doing boat.nameTheBoat() instead of just nameTheBoat()

尝试使用boat.nameTheBoat()而不仅仅是nameTheBoat()

Also

for(int i = 0; i <= boat.length; i++){          
        boat[i].nameTheBoat();          
    }    

boat is the instance of the class Boat (probably should be buildABoat). These look like they're compiler errors and not runtime errors, so the compiler should give you some hint (as it does) as to the exact line numbers.

船是班级船的实例(可能应该是buildABoat)。这些看起来像是编译器错误而不是运行时错误,因此编译器应该给出一些提示(就像它一样)来确切的行号。

#6


0  

The object array you have created is boat = new Boat[5];. Ideally you whole code should be like this

你创建的对象数组是boat = new Boat [5];。理想情况下,您的整个代码应该是这样的

Boat[] boat = new Boat[5];    
for(int i = 0; i <= Boat.Length; i++){          
boat[i] = new Boat();
boat[i].nameTheBoat();          
}

#7


0  

You have a few problems.

你有一些问题。

One is that boat as an array

一个是船作为阵列

Boat[] boat;            
boat = new Boat[5];    
for(int i = 0; i <= Boat.Length; i++){          
    nameTheBoat();          
} 

To access the array length it would be boat.length instead of what you have.

要访问数组长度,它将是boat.length而不是你拥有的。

The next problem I see is your call to nameTheBoat(). That method is part of the buildABoat class.

我看到的下一个问题是你对nameTheBoat()的调用。该方法是buildABoat类的一部分。

#8


0  

This code looks like a constructor with accident.

此代码看起来像一个意外的构造函数。

class buildABoat{    
    String boatName;      // Boat name

    void buildABoat(){
        String BoatName;
    }

If void buildABoat () would be a method (void says so), why would you declare a local variable in it, and not use it?

如果void buildABoat()是一个方法(void表示如此),为什么要在其中声明一个局部变量,而不是使用它?

And don't repeat the name of a well named attribute as comment.

并且不要将具有良好命名属性的名称重复为注释。

class BuildABoat {    
    private String boatName;   

    public BuildABoat (String name) {
        boatName = name;
    }

This would make some sense. You now have a class, which can be instantiated by passing a boatname, which is preserved in the class.

这有点道理。你现在有了一个类,它可以通过传递一个船名来实例化,该船名保存在类中。

In your Proj1-Class, you split declaration and initialisation. This is a bad habit. Sometimes it's not avoidable, but if it is, avoid it.

在Proj1-Class中,您可以拆分声明和初始化。这是一个坏习惯。有时这是不可避免的,但如果是,请避免它。

class Proj1 {

    public static void main(String[] arg){

        BuildABoat [] boat = new BuildABoat[5];    
        for(int i = 0; i <= boat.length; i++) {
            // to make this work, we have to change the 
            // buildABoat
            boat[i] = new BuildABoat (BuildABoat.nameTheBoat ());
        }
        System.out.println ("(Press ENTER to exit)");
        try {           
            System.in.read ();
        }           
        catch (IOException ignored) {         
            return;
        }
    }
}

The method nameTheBoat has to be static, if we call it without actually having a (builda)boat already.

方法nameTheBoat必须是静态的,如果我们在没有实际拥有(builda)船的情况下调用它。

public static String nameTheBoat () {
    try {           
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println ("\n\nWhat should we name this vessel? \n\n");
        String s = br.readLine ();
        System.out.println ("\n\nThe " + s + " is ready to sail!\n");
        return s;
    }
      catch (IOException e) {
        return "";
    }   
}

I hope the code does a little what you intended.

我希望代码能做到你想要的一点点。