Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 5, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/c4aebc0c55022f7f34697e46b2f21ef9 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/c4aebc0c55022f7f34697e46b2f21ef9 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-5
import java.util.Scanner;
public class En06_05 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("要素数:");
int youso = stdIn.nextInt();
//int型の配列を作成
int[] hairetuYouso = new int[youso];
//for文を使用して、配列に代入させる。
for (int i = 0; i < youso; i++) {
System.out.printf("a[%d]:",i);
hairetuYouso[i]=stdIn.nextInt();
}
//for文で結果を{}の中に数字で表示
System.out.print("a = {");
for(int i=0;i<hairetuYouso.length;i++){
//配列の値を表示
System.out.print(hairetuYouso[i]);
//i+1が配列の長さと同値の場合、何も出力しない。
//それ以外では、文字の区切りを表示する。
System.out.print((i+1==hairetuYouso.length)?"":", ");
}
System.out.println("}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment