已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验一:Java基本语法【 实验目的】了解Java的数据类型,掌握各种变量的声明方式,理解运算符的优先级,掌握Java基本数据类型、运算符与表达式,掌握顺序结构、选择结构和循环结构语法的程序设计方法。【实验要求】1、编写一个声明Java不同数据类型变量的程序。2、编写使用不同选择结构的程序。3、编写使用不同循环结构结构的程序。【实验内容】 1、声明不同数据类型变量public class SimpleTypepublic static void main(String args)byte b=0x55;short s=0x55f;int i=1000000;long l=0xfffl;char c=c;float f=0.23F;double d=0.7E-3;boolean bool=true;System.out.println(b=+b);System.out.println(s=+s);System.out.println(i=+i);System.out.println(l=+l);System.out.println(c=+c);System.out.println(f=+f);System.out.println(d=+d);System.out.println(bool=+bool);2、(1)使用选择结构public class TestIfpublic static void main(String args)boolean leap;int year=2005;if(year%4=0&year%100!=0)|(year%400=0)/方法一System.out.println(year+年是闰年);elseSystem.out.println(year+年不是闰年);year=2008;/方法二if(year%4!=0)leap=false;else if(year%100!=0)leap=true;else if(year%400!=0)leap=false;elseleap=true;if(leap=true)System.out.println(year+年是闰年);elseSystem.out.println(year+年不是闰年);year=2050;/方法三if(year%4=0)if(year%100=0)if(year%400=0)leap=true;elseleap=false;elseleap=false;elseleap=false;if(leap=true)System.out.println(year+年是闰年);elseSystem.out.println(year+年不是闰年); (2) 使用switch语句public class SwitchTestpublic static void main(String args)throws java.io.IOExceptionchar a;System.out.println(Enter a number from 1-3:);a=(char)System.in.read();switch(a)case1:System.out.println(win a Car!);break;case2:System.out.println(picked the goat);break;case3:System.out.println(get to keep your 100);break;default:System.out.println(entry);3. 使用循环结构(1)使用for语句public class TestForpublic static void main(String args)throws java.io.IOExceptionint fahr,cels;System.out.println(Celsius Fahrenheit);for(cels=0;cels=100;cels+=5)fahr=cels*9/5+32;System.out.println(cels+ +fahr);char a;outer:/this is the label for the outer loopfor(int i=0;i10;i+)for(int j=0;j10;j+)a=(char)System.in.read();if(a=b)break outer;if(a=c)continue outer;2) 使用while语句A、import java.io.*;public class TestWhilepublic static void main(String args)throws IOExceptionchar ch;System.out.println(按1/2/3数字键可得大奖!);System.out.println(按空格键后回车可推出循环操作。);while(ch=(char)System.in.read()!= )System.in.skip(2);/跳过回车键switch(ch)case1:System.out.println(恭喜你得大奖,一辆汽车!);break;case2:System.out.println(不错呀,你得到一台笔记本脑!);break;case3:System.out.println(没有白来,你得到了一台冰箱!);break;default:System.out.println(真不幸,你没有奖品,下次再来。);B、求1+2+-+100之和的程序如下:public class TestDoWhilepublic static void main(String args)int n=1,sum=0;dosum+=n+;while(n=100);System.out.println(1+2+.+100=+sum);C、多重循环,输出九九乘法表的程序。public class TestMulpublic static void main(String args)int i,j,n=9;System.out.println( * |);for(i=1;i=n;i+)System.out.print( +i);System.out.print(n-|);for(i=1;i=n;i+)S
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年区块链Web3开发技术白皮书及源码
- 深度复盘智能平衡板年度发展:融资轮次与渗透率
- 脑机接口初探:智慧储能云平台在特殊场景下的交互可能性
- 2026-2027年深圳市固废资源化利用可行性研究报告
- 如何撰写高质量学术论文开题报告
- 蓝图绘就 文体中心项目 2026年大湾区文体中心建设可行性研究报告
- 企业级SaaS产品从0到1的产品设计与增长路径
- 智能拾便袋盒赋能智慧养老:解决失能老人照护核心痛点
- 2024年重庆城市管理职业学院高职单招职业适应性测试考试模拟试卷及参考答案详解【典型题】
- 2024年江西省上饶市高职单招职业技能考试模拟试卷(夺冠系列)附答案详解
- 2026年国企中层干部竞聘笔考试题与答案
- 2026江苏苏州市相城区人民检察院招聘编外人员3人笔试题库及答案详解(新)
- 2026年生产文员测试题及答案
- 2026中国氢能储运装备安全标准与国际对标报告
- 乡镇(街道功能区)党政领导干部离任经济事项交接表(开发区和园区适用本表-修订)
- 2025贵州六盘水市盘州市教育局机关所属事业单位考调19人备考题库必考题
- TSG D0001-2009:压力管道安全技术监察规程-工业管道
- 钠离子电池热失控机理及其防火阻燃策略研究
- 2025年希望杯夏令营竞赛四年级下学期数学S卷
- 2025年部编版新教材语文小学二年级上册全册单元检测题带答案(共8单元)
- 测绘单位安全生产培训
评论
0/150
提交评论