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次。
因爲您不執行任何打印任何代碼。你需要在某處調用'forloop()'。 –
1)如上所述,在某處調用'forloop()'。 2)如果沒有按預期工作,請務必檢查for循環以查找是否存在潛在的錯誤。 3)總是給班級一個大寫的駱駝案例名稱(IE:'MyArrays' vs.'arrays')。 4)「信息」的意義何在? – Ironcache
你需要在'animals'數組中添加'i'來遍歷它的所有元素,如'System.out.println(animals [i] +「:野生中剩下多少個」);' – mmushtaq