JAVA实验_题目2
- 格式:docx
- 大小:25.62 KB
- 文档页数:6
第二章 Java语法基础
【实验目的】
(1) 常量、变量与数据类型
a. 掌握Java的常量、变量声明及使用方法
b. 掌握Java各种数据类型的使用
c. 掌握基本的输入输出函数
(2) 运算符与表达式
a. 掌握算术运算、关系运算、逻辑运算,及优先关系
b. 掌握表达式的使用
(3) 常用系统类
a. 基本输入输出类的使用
b. 掌握Math类的使用
(4) 类及其方法的使用
a. 掌握类的定义和方法调用
【实验范例】
1、 数据类型范例
publicclass DataTypeDemo {
publicstaticvoid main(String args[]) {
byte b = 0x55;
short s = 0x55ff;
int i = 1000000;
long l = 0xffffL;
char c = 'a';
float f = 0.23F; //此行0.23后的F可否去掉,为什么?
double d = 0.7E-3; //可否将0.7E-3变换为另一种等值方式来表示
boolean B = true;
String S = "这是字符串类数据类型";
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("布尔型变量 B = " + B);
System.out.println("字符串类对象 S = " + S);
}
}
2、 运算符与表达式
(1) 数值运算范例
publicclass NumCalcDemo {
publicstaticvoid main(String args[]) {
int a = 8, b = 5;
//除法运算
System.out.println( a+"/"+ b +" = " + a/b);
System.out.println( a+"/"+(-b)+" = " + a/-b);
System.out.println(-a+"/"+ b +" = " + -a/b);
System.out.println(-a+"/"+(-b)+" = " + -a/-b);
//取模运算
System.out.println(a +"%"+ b +" = " + a%b);
System.out.println(a +"%"+(-b)+" = " + a%-b);
System.out.println(-a+"%"+ b +" = " + -a%b);
System.out.println(-a+"%"+(-b)+" = " + -a%-b);
//自加运算
System.out.println(++a + "+" + b + "=" ++ +a + b); //此行会出现什么错误提示,请修正
System.out.println(++a + "+" + b + "=" +(++a) + b);
System.out.println(a++ + "+(" + (-b) + ")=" + +(++a) + b); //为使得输出的表达式正确,此处应该怎么改
System.out.println(- ++a + "+" + b + "=" + - ++a + b);
System.out.println(- a++ + "+" + (-b) + "=" + -a++ + -b);
}
}
(2) 关系运算与逻辑运算范例
publicclass OperationDemo {
publicstaticvoid main(String args[]) {
int a = 25, b = 3;
boolean d = a
System.out.println(a+"<"+b+" = "+d);
int e = 3;
d = (e!=0 && a/e>5);
System.out.println(e+"!=0&&"+a+"/"+e+">5 = "+d);
int f = 0;
d = (f!=0 && a/f>5);
System.out.println("d=" + d);
d = (a/f>5 && f!=0); //有错,如何修正?
System.out.println("d=" + d);
}
}
【预备知识】
1、控制台输出
Java从控制台输出可使用以下方法:
System.out.print()
System.out.println()
2、控制台输入
Java从控制台输入的3种方式:
(1) 用System.in.read()获取"单个"字符
import java.io.*; //导入Input/Output数据包
publicclassCharInputDemo {
publicstaticvoid main(String [] args) throws IOException//抛出IO异常
{
System.out.print("Enter a Char:");
char i = (char) System.in.read();
System.out.println("your char is :"+i);
}
}
(2) 用BufferedReader类和InputStreamReader类获取"字符串"
import java.io.*;
publicclassStreamReaderDemo {
publicstaticvoid main(String [] args) throws IOException{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = null;
System.out.println("Enter your value:");
str = br.readLine();
System.out.println("your value is :"+str);
}
}
(3) 使用Scanner类读入"字符串"
import java.util.*; //导入java.util工具包
publicclassScannerDemo {
publicstaticvoid main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.nextLine();
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
System.out.println("请输入你的工资:");
float salary = sc.nextFloat(); System.out.println("你的信息如下:");
System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary);
sc.close(); //关闭输入流
}
}
注:(1) (2)需要抛出异常:throws IOException
(3) 比较灵活,建议使用
3、常用系统类:
ng包: String类、Math类(使用方法请参考教材Java常用系统类),String类使用实例在下一章介绍。
Math类常用方法:abs、exp、floor、log、pow、random、round、sin\cos\tan(asin\acos\atan)
例:
publicclass Prog1 {
publicstaticvoid main(String[] args) {
double a = 1.5, b = 4, c = -3;
System.out.println("floor=" + Math.floor(a)); // 求最大的整数,但不大于本身
System.out.println("ceil=" + Math.ceil(a)); // 求最小的整数,但不小于本身
System.out.println("round=" + Math.round(a)); // 四舍五入
System.out.println("sqrt=" + Math.sqrt(b)); // 平方根
System.out.println("abs=" + Math.abs(c)); // 绝对值
System.out.println("max=" + Math.max(a, b)); // 最大值
System.out.println("random=" + Math.random()); // 随机函数
System.out.println("exp=" + Math.exp(2)); // e为底的指数函数
System.out.println("atan=" + Math.atan(1)); // 反正切函数
}
}
【综合例子】
(类的定义和方法使用)
例:建立一个银行账户,包括账号、户名、密码、余额等个人信息,并对各项信息初始化和输出屏幕。(程序名称:CustDemo.java)
class Account {
intcustID;
String custName;
intcustPWD;
doublecustMoney;
void initAccount(intid, String name, intPWD, doublemoney) {