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

问题描述:

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次。

+5

因为您不执行任何打印任何代码。你需要在某处调用'forloop()'。 –

+0

1)如上所述,在某处调用'forloop()'。 2)如果没有按预期工作,请务必检查for循环以查找是否存在潜在的错误。 3)总是给班级一个大写的骆驼案例名称(IE:'MyArrays' vs.'arrays')。 4)“信息”的意义何在? – Ironcache

+0

你需要在'animals'数组中添加'i'来遍历它的所有元素,如'System.out.println(animals [i] +“:野生中剩下多少个”);' – mmushtaq

你永远不会打电话给你的方法“forloop()”,这就是没有什么打印的原因。

NewbieJavaDeveloper的答案是一个很好的答案。但是,如果你想pactice“方法和回报”,这里是另一个答案:

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?"); 
     } 
    } 

} 

我做minimun改变了代码,希望你能找到它很容易理解。

我想你想是这样的:

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?"); 
     } 
    }  
} 

输出:

Komodo Dragon: How many are left in the wild? 
Manatee: How many are left in the wild? 
Kakapo: How many are left in the wild? 
Florida Panther: How many are left in the wild? 
White Rhino: How many are left in the wild? 

希望它能帮助,

感谢和问候。

+0

他们赢得' t学习互联网是否为他们做功课。 – Ironcache