面向对象编程基础习题及答案

发布时间 : 星期四 文章面向对象编程基础习题及答案更新完毕开始阅读

System.out.println(\ System.out.println(\ System.out.println(\ System.out.println(\ } }

Results: x is 0 y is 6 i is 5 j is 12 f is 0.0

2.

public class Test {

public static void main(String[] args) { int i = 1;

while (i <= 5) {

int num = 1;

for (int j = 1; j <= i; j++) { System.out.print(num + \ num *= 3; }

System.out.println(); i++; } } }

Results: 1bb 1bb3bb

1bb3bb9bb

1bb3bb9bb27bb 1bb3bb9bb27bb81bb

3.

public class Test {

public static void main(String[] args) {

int i = 1;

while (i <= 5) {

第 25 页 共 34 页

xMethod(i); i++; } }

public static void xMethod(int i) { int num = 1;

for (int j = 1; j <= i; j++) { System.out.print(num + \ num *= 3; }

System.out.println(); } }

Results: 1 1 3 1 3 9 1 3 9 27 1 3 9 27 81

4.

public class Test {

public static void main(String[] args) {

int i = 1;

while (i <= 5) { xMethod(i); i++; } }

public static void xMethod(int i) { do {

if (i % 2 != 0)

System.out.print(i + \ i--;

}while (i >= 1);

System.out.println(); } }

第 26 页 共 34 页

Results: 1 1 3 1 3 1 5 3 1

5.

public class Test {

public static void main(String[] args) { xMethod(4); }

public static void xMethod(int i) { if (i > 0) {

System.out.println(i); xMethod(--i); } } }

Results: 4 3 2 1

6.

public class Test {

public static void main(String[] args) { int[] a = {1, 2};

System.out.println(\

System.out.println(\ swap(a[0], a[1]);

System.out.println(\

System.out.println(\ }

public static void swap(int n1, int n2) { int temp = n1; n1 = n2; n2 = temp;

第 27 页 共 34 页

}

}

Results:

Before swapping: a[0] = 1 a[1] = 2 After swapping: a[0] = 1 a[1] = 2

7.

public class Test {

public static void main(String[] args){ int[] a = {1, 2};

System.out.println(\

System.out.println(\ swap(a);

System.out.println(\

System.out.println(\ }

public static void swap(int[] a){ int temp = a[0]; a[0] = a[1]; a[1] = temp; } }

Results:

Before swapping: a[0] = 1 a[1] = 2 After swapping: a[0] = 2 a[1] = 1

8.

public class Test {

public static void main(String[] args) {

int[] x = {1, 2, 3, 4, 5}; increase(x);

int[] y = {1, 2, 3, 4, 5}; increase(y[0]);

System.out.println(x[0] + \

第 28 页 共 34 页

联系合同范文客服:xxxxx#qq.com(#替换为@)