




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 压力管道安全培训感受课件
- 2025年机器人行业智能机器人技术应用前景与产业发展研究报告
- 2025年生物医药行业生物医药技术高新发展与健康产业前景研究报告
- 2025年文化传媒行业文化传媒产业发展前景研究报告
- 2025年人工智能在医疗保健行业应用案例与市场前景报告
- 2025年智能医疗行业智能医疗设备市场前景展望研究报告
- 2025年汽车行业共享汽车市场前景研究报告
- 2025年文化行业文创产品市场前景分析研究报告
- 2025年无人机行业无人机应用案例与发展前景研究报告
- 宿迁市2025江苏宿迁市商务局局属事业单位招聘工作人员5人笔试历年参考题库附带答案详解
- 《分子生物学基础知识》课件
- GB/T 45147-2024道路车辆总质量大于3.5 t的车辆气制动系统试验使用滚筒制动试验台获取和使用参考值
- 食管纵隔瘘护理
- 建筑项目水泥采购合同
- 华为ICT大赛网络赛道考试题库(786题)
- 水果采购协议样本
- 中职英语(高教版2021基础模块1)Part01-Unit2-Transportation
- 哲学与人生 第二课 树立科学的世界观2.1
- 2024-2030年中国止痛药品市场供需形势及未来前景动态研究研究报告
- 风电110KV升压站土建工程施工方案
- 2018低压电力线高速载波通信互联互通技术规范第3部分:检验方法
评论
0/150
提交评论