实验一:Java 基本语法.doc
实验一:Java 基本语法
实验内容:
1、声明不同数据类型变量
编写声明不同数据类型变量的程序
public class SimpleTypes public static void main String args[] byte b 0x55; short s 0x55ff; 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、使用选择结构
使用 if...else 语句
1)编写源程序文件,代码如下。 public class TestIf public static void main String args[] boolean leap; int year 2005; if year%4 0 year%100! 0 || year%400 0 // 方法 1 System.out.println year+ 年是闰年 ; else System.out.println year+ 年不是闰年 ; year 2008; // 方法 2 if year%4! 0 leap false; else if year%100! 0 leap true; else if year%400! 0 leap false; else leap true; if leap true System.out.println year+ 年是闰年 ; else System.out.println year+ 年不是闰年 ; year 2050; // 方法 3 if year%4 0 if year%100 0 if year%400 0 leap true; else leap false; else leap false; else leap false; if leap true System.out.println year+ 年是闰年 ; else System.out.println year+ 年不是闰年 ; 2)编译运行程序
使用 switch 语句
1)编写程序用swith语句实现从键盘读如1,2,3时,屏幕提示不同信息
class SwitchTest public static void main String args[] throws java.io.IOException char 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 ; 2)编译运行程序
3、使用循环结构
使用for语句
1)程序源代码如下:
class TestFor public static void main String args[] throws java.io.IOException int 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 loop for int i 0;i 10;i++ for int j 0;j 10;j++ a char System.in.read ; if a b break outer; if a c continue outer; 2)编译运行程序
使用while语句
1)程序源代码
您可能关注的文档
最近下载
- 2025年山东药品食品职业学院高职单招综合素质考试参考题库及答案解析.docx VIP
- (高清版)DB4409∕T 24-2021 《奇楠沉香扦插技术规程》.docx VIP
- 2026年广东中考生物命题趋势预测试卷(附答案解析).docx VIP
- 北京市2025年高考:《英语》考试真题(含答案).pdf VIP
- 世界金融史:泡沫、战争与股票市场([日]板谷敏彦 著).pdf VIP
- 2025年北京央教湘岳假期寒假作业七年级英语人教版答案.pdf VIP
- 明月海藻实习素材.doc VIP
- 死因监测工作例会的制度.doc VIP
- 《墙面石材干挂(背栓式)施工工艺》.docx VIP
- 老年人营养不良的评估(老年人能力评估课件).ppt
原创力文档

文档评论(0)