实验一:Java 基本语法.doc

实验一: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)程序源代码

文档评论(0)

1亿VIP精品文档

相关文档