java练习1(含答案)

loading 分享 2026-8-12 下载文档

public class AnonymousConstructor {

public static Base getBase(int i) { return new Base(i) { {

System.out.println(\ }

public void f() {

System.out.println(\ } }; }

public static void main(String[] args) { Base base = getBase(47); base.f(); } }

结果:

Base constructor, i =47 Inside instance initializer In anonymous f() 三、改错题(10分),找出错误并写出正确答案 1 public class Contents{ 2 int i;

3 void Contents(int j){i=j;}//应该把void去掉 4 }

5 class Game { 6 Game(int i) {

7 System.out.println(\8 } 9 }

10 class BoardGame extends Game { 11 BoardGame(int i) {

12 Game(i); //super(i);

13 System.out.println(\14 }

15 public static main(String[] args) { //漏写void 16 BoardGame x = new BoardGame(3); 17 } 18 }

19 public class Demotion {

20 void f(float x) { System.out.println(\21 void testDouble() {

22 double x = 0; //应定义为float格式 23 System.out.println(\

24 f(x); 25 }

26 public static void main(String[] args) { 27 Demotion p = new Demotion(); 28 p.testDouble(); 29 } 30 }

四、编程题(每题15分,共30分)

4.1编写Java应用程序实现下面数据的排序

int a[]={52, 38, 16,89, 24, 18, 0, 190, 87, 21} import java.util.*; public class Ssort {

public static void main(String[] args) {

int a[]={52, 38, 16,89, 24, 18, 0, 190, 87, 21}; Arrays.sort(a);

System.out.println(\排序之后的结果: \for(int i = 0; i < a.length; i++){ System.out.print(a[i] + \} } }

4.2编写一个算法求n!。 import java.util.*; import java.io.*; class aa {

public static void main(String[] args) throws IOException { int sum=1,num; String str;

System.out.println(\请输入n:\BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in)); str=buf.readLine();

num=Integer.parseInt(str); if(num!=0) {

for(int i=1;i<=num;i++) {

sum=sum*i;

} }

else{ System.out.println(\的阶乘是0\}

System.out.println(\的阶乘是:\


java练习1(含答案).doc 将本文的Word文档下载到电脑
搜索更多关于: java练习1(含答案) 的文档
相关推荐
相关阅读