已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 惠州滴滴应聘试题与答案解析
- 2027届陕西省咸阳秦都区四校联考数学九年级第一学期期末检测试题含解析
- 往事依依模拟试题及答案
- 幼小衔接之入园分离焦虑:从容上小学
- 江西省崇仁县2027届九上数学期末考试试题含解析
- 2026年安全B证考试模拟题及答案详解
- 2026年全国普法印花税法题库(含答案)
- 2026年泡桐树小学都江堰校区招聘考试题库(含答案)
- 黑龙江省哈尔滨市南岗区第十七中学2027届数学七年级第一学期期末达标检测试题含解析
- 2026年广东春考考试模拟题及答案详解
- 2026年成都市金牛区人民检察院公开招聘编外人员笔试参考题库及答案详解
- 2026年上海市建筑三类人员项目负责人(安全员B证)考试题库
- 《整 理收纳》高职现代家政服务专业全套教学课件
- 湖南省湘潭市2027届高三上学期第一次模拟考试英语试卷(含答案)
- IPC-4101 标准中文版文档
- 第一单元 健康生活(单元自测)科学教科版六年级上册2026秋
- 《化工设备基础》课程标准
- 2026年贵州省现代种业集团有限公司第二批人才招聘考试备考试题及答案详解
- 2026统考专升本英语:英语550个高频核心词
- 26.3 二次函数与一元二次方程 教学课件
- 高标准农田建设项目监理服务方案投标文件(技术方案)
评论
0/150
提交评论