第一范文网 - 专业文章范例文档资料分享平台

java练习1(含答案)

来源:用户分享 时间:2025/7/21 23:40:22 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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(含答案) 的文档
java练习1(含答案).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4av2181rw39gaia4884t_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top