我無法返回數組的值,我知道我返回它錯了,但我不知道如何。請幫助。返回數組主體()
這些是操作的指令:
說明:代碼,對於3種 不同狗類型提示)稱爲dogTypes(的方法,存儲類型稱爲狗, 一個本地陣列並返回該數組的在main()中調用語句。一旦 main()獲取數組,它將使用for循環在如下所示的單獨行上打印數組 內容,其中9表示 數值,以便標籤讀取「Dog Type 1:」,然後「Dog類型2: 「等,從陣列中檢索每種狗類型。您可以使用for循環中的 循環控制變量生成9值。在名爲YourLastNameFirstInitialArraysV1.java的程序中保存 的代碼。 狗類型9:XXXXXXXXXXXXXXX狗類型9:XXXXXXXXXXXXXXX
import java.util.Scanner;
public class BarrazaMArrayV2
{//BEGIN BarrazaMLE52
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
String[] dog = dogType();
System.out.printf("Dog Type 1: %S", dog[0]);
System.out.printf("Dog Type 2: %S", dog[1]);
System.out.printf("Dog Type 3: %S", dog[2]);
}
public static String[] dogType()
{
String[] dogType = new String[3];
System.out.printf("Enter your dog type: ");
dogType[0] = input.nextLine();
System.out.printf("Enter your dog type: ");
dogType[1] = input.nextLine();
System.out.printf("Enter your dog type: ");
dogType[2] = input.nextLine();
return dogType[3];
}
}
只要使用'返回dogtype;'。當你說'return dogType [3];'你試圖返回一個字符串,在這種情況下,因爲數組中有3件事情而不存在,所以'4'將是第4個。 – csmckelvey 2014-11-24 03:25:00