Created
December 13, 2016 13:09
-
-
Save mokomokohitsuzi/73ff0df1385e570d7377e2c69eb0caa1 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class En06_13 { | |
public static void main(String[] args) { | |
Scanner stdIn = new Scanner(System.in); | |
System.out.print("要素数:"); | |
int n = stdIn.nextInt(); | |
// 配列aと配列bを作成 | |
int[] a = new int[n]; | |
int[] b = new int[n]; | |
// 配列aに数値を入力させる。 | |
for (int i = 0; i < n; i++) { | |
System.out.printf("a[%d] =", i); | |
a[i] = stdIn.nextInt(); | |
} | |
// 配列bに逆順に値をコピー | |
for (int i = a.length, j = 0; i > 0; i--, j++) { | |
// 配列は0から始まるため、i-1を使用 | |
b[j] = a[i - 1]; | |
} | |
System.out.println("配列aの全要素を配列bに逆順にコピーしました。"); | |
// 配列bの表示 | |
for (int i = 0; i < b.length; i++) { | |
System.out.printf("b[%d] =%d\n", i, b[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment