当我编译这个为什么它没有打印任何东西?

时间:2016-12-02 17:03:15

标签: java arrays for-loop printing initialization

1.为什么这不打印任何东西,这段代码也有意义吗?我是java的新手,所以我不确定。

import java.util.Scanner;//import scanner so user can input

class arrays
{
    public static void main(String[] param)
    {
        arrays();
        System.exit(0);
    }//end main method

    public static int arrays() //array method 
    {
        int information = 0; // keeping a variable count
        String[] animals = new String[5]; //array to store 5 animals

        animals[0] = "Komodo Dragon"; //animals stored
        animals[1] = "Manatee";
        animals[2] = "Kakapo";
        animals[3] = "Florida Panther";
        animals[4] = "White Rhino";

        return information;
    }

    public static void forloop()
    {
        String[] animals = new String[5];

        //for loop to print the below print 5 times using the different animal names.
        for(int i =0; i<4; i++) {
            System.out.println(
                    animals[0] + ": How many are left in the wild?");
        }
    }
}

2.我希望在问题出现前用动物名称打印5次。

2 个答案:

答案 0 :(得分:1)

我想你想要这样的东西:

With ActiveDocument.Content
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .text = ABC_123.XXXX
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = False
            .Execute Replace:=wdReplaceOne
        End With

        While .Find.Found = True
            count = count + 1
            lastReqNr = lastReqNr + 1
            .text = ABC_123. & Right(String(4, "0") & lastReqNr, 4)
            .Collapse wdCollapseEnd
            .Find.Execute Replace:=wdReplaceOne
        Wend
    End With

<强>输出:

public class Sample {

    public static void main(String[] args) 
    {
        array();
        //System.exit(0); //not needed
    }

    public static void array()
    {
        String[] animals = new String[5]; //array to store 5 animals

        animals[0] = "Komodo Dragon"; //animals stored
        animals[1] = "Manatee";
        animals[2] = "Kakapo";
        animals[3] = "Florida Panther";
        animals[4] = "White Rhino";

        for(int i = 0 ; i < 5 ; i++)
        {
            System.out.println(
                    animals[i] + ": How many are left in the wild?");
        }
    }    
}

希望它有所帮助,

谢谢和问候。

答案 1 :(得分:1)

你永远不会把你的方法称为“forloop()”,这就是为什么没有打印出来的原因。

NewbieJavaDeveloper的答案很好。但是如果你想要“方法并返回”,那么这是另一个答案:

import java.util.Scanner;//import scanner so user can input

class arrays
{

    public static void main(String[] param)
    {
        String[] animals = arrays();
        forloop(animals);
        System.exit(0);
    } //end main method

    public static String[] arrays() //array method 
    {
        String[] animals = new String[5]; //array to store 5 animals

        animals[0] = "Komodo Dragon"; //animals stored
        animals[1] = "Manatee";
        animals[2] = "Kakapo";
        animals[3] = "Florida Panther";
        animals[4] = "White Rhino";

        return animals;
    }

    public static void forloop(String[] animals)
    {

        for(int i =0; i<5; i++) //for loop to print the below 
        //print 5 times using the different animal names.
        {
            System.out.println(animals[i] + ": How many are left in the wild?");
        }
    }

}

我对您的代码进行了最小化更改,希望您能够轻松理解。