2022年JAVA语言程序设计清华大学书上例题源代码第二章.docx

上传人:C****o 文档编号:57852963 上传时间:2022-11-06 格式:DOCX 页数:20 大小:35.45KB
返回 下载 相关 举报
2022年JAVA语言程序设计清华大学书上例题源代码第二章.docx_第1页
第1页 / 共20页
2022年JAVA语言程序设计清华大学书上例题源代码第二章.docx_第2页
第2页 / 共20页
点击查看更多>>
资源描述

《2022年JAVA语言程序设计清华大学书上例题源代码第二章.docx》由会员分享,可在线阅读,更多相关《2022年JAVA语言程序设计清华大学书上例题源代码第二章.docx(20页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -【2_1】/ 运算二个数的和 class Example2_1 public static void mainString args int x,y,s; x = 3; y = 5; s =x+y; /求和 System.out.println 二数之和为: + s; 【2_2】/ 运算圆的面积 class Example2_2 public static void mainString args double pi,r,s; r = 10.8; / 圆的半径 pi = 3.1416; s = pi * r

2、* r; / 运算面积 System.out.println 圆的面积为: + s; 【2_3】/* char 变量的用法 */ class Example2_3 public static void mainString args char ch1,ch2; ch1 = 88; / code for X ch2 = Y; System.out.printch1 and ch2 :; System.out.printlnch1 + + ch2; 【2_4】/* 布尔类型的用法 */ class Example2_4 public static void mainString args bool

3、ean b; b = false; System.out.printlnb is + b; b = true; System.out.printlnb is + b; / outcome of a relational operator is a boolean value 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 1 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -System.out.println10 9 is + 10 9; 【2_5】publi

4、c class Example2_5 public static void mainString agrs / 定义几个变量并赋值 int a=41; int b=21; double x=6.4; double y=3.22; System.out.println 变量数值: ; System.out.printlna=+a; System.out.printlnb=+b; System.out.printlnx=+x; System.out.printlny=+y; / 加法 System.out.println 加:; System.out.printlna+b=+a+b; System

5、.out.printlnx+y=+x+y; / 减法 System.out.println 减:; System.out.printlna-b=+a-b; System.out.printlnx-y=+x-y; / 乘法 System.out.println 乘:; System.out.printlna*b=+a*b; System.out.printlnx*y=+x*y; / 除法 System.out.println 除:; System.out.printlna/b=+a/b; System.out.printlnx/y=+x/y; / 从除法中求得余数 System.out.prin

6、tln 运算余数 :; System.out.printlna%b=+a%b; System.out.printlnx%y=+x%y; / 混合类型 System.out.println 混合类型 :; System.out.printlnb+y=+b+y; System.out.printlna*x=+a*x; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 2 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -【2_6】public class Exampl

7、e2_6 public static void mainString args / 定义如干整型数 int i=37; int j=42; int k=42; System.out.println 变量数值 ; System.out.printlni=+i; System.out.printlnj=+j; System.out.printlnk=+k; / 大于 System.out.println 大于 :; System.out.printlnij=+ij;/false System.out.printlnji=+ji;/true System.out.printlnkj=+kj;/fal

8、se / 大于等于 System.out.println 大于等于 :; System.out.printlni=j=+i=j;/false System.out.printlnj=i=+j=i;/true System.out.printlnk=j=+k=j;/true / 小于 System.out.println 小于 :; System.out.printlnij=+ij;/true System.out.printlnji=+ji;/false System.out.printlnkj=+kj;/false / 小于等于 System.out.println 小于等于 :; Syst

9、em.out.printlni=j=+i=j;/true System.out.printlnj=i=+j=i;/false System.out.printlnk=j=+kb t=a; a=b; b=t; ifac t=a; a=c; c=t; ifbc t=b; b=c; c=t; System.out.printlna=+a+,b=+b+,c=+c; 细心整理归纳 精选学习资料 /* if-else-if 结构 . */ 第 4 页,共 10 页 - - - - - - - - - - - - - - - - - - - - - - - - 名师归纳总结 精品学习资料 - - - - -

10、 - - - - - - - - - -【2_9】class Example2_9 public static void mainString args int month = 4; / 4 月份 String season; ifmonth = 12 | month = 1 | month = 2 season = 冬天 ; else ifmonth = 3 | month = 4 | month = 5 season = 春天 ; else ifmonth = 6 | month = 7 | month = 8 season = 夏天 ; else ifmonth = 9 | month

11、= 10 | month = 11 season = 秋天 ; else season = 不合法的月份 ; System.out.println4 月是 + season + .; 【2_10】/* switch 开关语句 */ import java.applet.*; import java.awt.*; public class Example2_10 extends Applet public void paintGraphics g int x=1,y=1; switchx+y case 1 : g.setColorColor.red; g.drawStringi am 1,5,1

12、0; break; case 2: g.setColorColor.blue; g.drawStringi am 2,5,10; / break; case 3: g.setColorColor.green; g.drawStringi am 3,5,10; break; default: g.drawString 没有般配的 ,5,10; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 5 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - - /* */ 【2_

13、11】/* for 循环 */ import javax.swing.JOptionPane; public class Example2_11 public static void mainString args int sum=0; forint i=1;i=100;i+ sum=sum+i; JOptionPane.showMessageDialognull,1+2+3+.+100= +sum; System.exit0; / 退出程序 【2_12】/* while 循环 */ import javax.swing.JOptionPane; public class Example2_1

14、2 public static void mainString args int s=1, i=1; whilei=10 s=s*i; i+; JOptionPane.showMessageDialognull,1*2*3*.*10= +s; System.exit0; / 退出程序 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 6 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -【2_13】/* do-while循环*/ import java.apple

15、t.*; import java.awt.*; public class Example2_13 extends Applet public void paintGraphics g int i=1; do g.drawOval110-i*10,110-i*10,i*20,i*20; i+; whilei=10; /* */ 【2_14】/* 使用 break 语句跳出循环 */ import javax.swing.JOptionPane; class Example2_14 public static void mainString args forint i=0; i100; i+ if

16、i = 10 break; / i=10 时跳出循环 JOptionPane.showMessageDialognull,i=+i; JOptionPane.showMessageDialognull, 循环已经终止!; System.exit0; / 退出程序 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 7 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -【2_15】/* 使用 break 语句跳出内循环 */ class Example2_15 pub

17、lic static void mainString args forint i=1; i6; i+ forint j=1;j3; j+ ifi = 3 break; / i=5 时跳出循环 int sum=i+j; System.out.printlni+j+=+sum; System.out.println 循环已经终止!; 【2_16】/* 使用 标签化中断 的 break 语句跳出循环 */ class Example2_16 public static void mainString args out: forint i=1; i6; i+ / 设置标号 forint j=1;j3;

18、 j+ ifi = 3 break out; / i=3 时跳出循环 int sum=i+j; System.out.printlni+j+=+sum; ; System.out.println 循环已经终止! 【2_17】/*continue语句打印三角形*/ import javax.swing.JOptionPane; class Example2_17 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 8 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -

19、public static void mainString args String output=; forint i=0; i5; i+ forint j=0; j i continue ; output= output + + ; output=output+n; JOptionPane.showMessageDialognull,output; System.exit0; 【2_18】/* 求一组数字的平均值 */ import javax.swing.JOptionPane; class Example2_18 public static void mainString args do

20、uble nums = 10.1, 11.2, 12.3, 13.4, 14.5; double result = 0; forint i=0; i5; i+ result = result + numsi; JOptionPane.showMessageDialognull, 平均值为: + result / 5; System.exit0; 【2_19】/ 二维数组赋值 class Example2_19 public static void mainString args int twoD= new int45; int i, j, k = 0; fori=0; i4; i+ forj=

21、0; j5; j+ twoDij = k; k+; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 9 页,共 10 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -fori=0; i4; i+ forj=0; j5; j+ System.out.printtwoDij + ; System.out.println; 【2_20】import java.util.*; class Example2_20 public static void mainString args

22、Vector v1 =new Vector; for int i=1;i10;i+ v1.addElement + i; System.out.printlnv1.toString; v1.insertElementAtabc,5; 【2_21】import java.util.*; public class Example2_21 public static void mainString args String s=boy,java,Applet girl,Hat; StringTokenizer st=new StringTokenizers, ,; / 空格和逗号做分 int number=st.countTokens; whilest.hasMoreTokens String str=st.nextToken; System.out.printlnstr; System.out.printlns 共有单词: +number+ 个; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 10 页,共 10 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com