2016-12-02 102 views
-5

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

回答

1

你永遠不會打電話給你的方法「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改變了代碼,希望你能找到它很容易理解。

1

我想你想是這樣的:

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

相關問題