Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class Main {
  5. public static void main(String[] args) throws Exception {
  6. //『スラスラわかるJava』 第6章-2
  7. //配列
  8. String[] colors = new String[4];
  9.  
  10. Scanner sc = new Scanner(System.in);
  11.  
  12. int i = 0;
  13.  
  14. while(i < 4){
  15. colors[i] = sc.next(); //入力 赤 青 緑 黄
  16.  
  17. System.out.println("colors[" + i + "]:" + colors[i]);
  18.  
  19. i++;
  20. }
  21.  
  22.  
  23. /*
  24. 実行結果
  25.  
  26. colors[0]:赤
  27. colors[1]:青
  28. colors[2]:緑
  29. colors[3]:黄
  30.  
  31. */
  32.  
  33.  
  34. //『スラスラわかるJava』 第7章
  35. //拡張for
  36. for (String color : colors){
  37. System.out.println(color + "色");
  38. }
  39.  
  40. /*
  41. 実行結果
  42.  
  43. 赤色
  44. 青色
  45. 緑色
  46. 黄色
  47. */
  48.  
  49.  
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement