2013-10-10 49 views
-2
環用戶輸入

到目前爲止,我的代碼看起來是這樣的:如何在Java中

package kittensworld; 

    import java.util.Scanner; 

    public class KittensWorld { 
     public static void main(String[] args){ 

      System.out.println("What is the first kitten's name?"); 
      Scanner scan = new Scanner(System.in); 
      String kitten1 = scan.nextLine(); 
      System.out.println("What is the second kitten's name?"); 
      String kitten2 = scan.nextLine(); 
      System.out.println("And the third kiten's name?"); 
      String kitten3 = scan.nextLine(); 
      System.out.println("The first kitten's name is " + kitten1); 
      System.out.println("The second kitten is " + kitten2); 
      System.out.println("And last but not least, " + kitten3); 
     } 
    } 

我怎麼會去環路,這樣我可以索要20只小貓的名字,而不進行復制和粘貼20次?

+4

只需使用一個循環。你還在找什麼? – Lion

+1

@Lion Arrays,也許? – Ingo

+0

創建一個'String'的數組:String [] kittens = new String [20]' –

回答

2

使用for環和array那將包含的數據:

String[] inputs = new String[NUMBER_OF_INPUTS]; 
Scanner scanner = new Scanner(); 
for(i=0; i<NUMBER_OF_INPUTS; i++) { 
    inputs[i] = scanner.nextLine(); 
} 

如果你不知道輸入的數量(但你知道什麼符號將代表輸入的結束) ,您可以使用while循環和ArrayList

1
package kittensworld; 

import java.util.Scanner; 

public class KittensWorld { 
    public static void main(String[] args){ 
     Scanner scan = new Scanner(System.in); 
     for (int i = 0; i < 20; i++){ 
      System.out.println("What is the kitten's name?"); 
      String kitten = scan.nextLine(); 
      System.out.println("That kitten's name was " + kitten); 
     } 
    } 
} 
2

我想你需要查找本教程的這家名爲for-loop正確的循環:

String[] kitten = new String [size]; 
for(int i = 0 ; i < kitten.length; i++){ 
System.out.println("kitten number "+i+" : "); 
kitten [i] = scan.nextLine(); 
} 
+0

謝謝,我忘了'[]'。 –

+1

現在很好:)最好循環,直到'尺寸'而不是'kitten.length' ..你已經有了尺寸。 – Maroun

+1

如果你不想混淆別人,用'String [] kitten'替換'String [] kitten'因爲'[]'是類型的一部分,而不是名稱。 – zapl

0
package kittensworld; 

import java.util.Scanner; 

public class KittensWorld { 

    private static final int NUMBER_OF_KITTENS = 20; 
    public static void main(String[] args){ 
     String[] kittenNames = new int[NUMBER_OF_KITTENS]; 
     Scanner scan = new Scanner(System.in); 

     for(int i = 0; i < NUMBER_OF_KITTENS; ++i) 
     { 
      System.out.println("What is the name of kitten number " + (i+1) + "?"); 
      kittenNames[i] = scan.nextLine(); 
     } 
    } 
} 
0
public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    ArrayList<String> kittenNames = new ArrayList<>(); 
    int number = 3 ; // number of kitten names (+1) 

    for (int i = 1; i < number; i++) { 
     System.out.printf("What is the %s kitten's name?\n", ordinal(i)); 
     kittenNames.add(scan.nextLine()); 
    } 
    System.out.println("==============================================="); 
    for (String name : kittenNames) { 
     System.out.println(name); 
    } 
} 

public static String ordinal(int i) { 
    String[] sufixes = new String[]{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}; 
    switch (i % 100) { 
     case 11: 
     case 12: 
     case 13: 
      return i + "th"; 
     default: 
      return i + sufixes[i % 10]; 

    } 
}