1.实验目的
1). 熟悉java开发工具及配置
2). 熟悉java程序类型,掌握java应用程序结构及书写形式
3). 掌握java程序编写、编译、执行过程
4). 掌握java程序基本语法
5). 熟悉用System进行简单的输入输出
6). 熟悉命令行参数,熟悉基于包装类的数据转换
2.实验内容
1.下面的程序有错误请修改并编译运行。
1)public class KY2_2 {
static int i=10;
public static void main(String args[]) {
{
int k=10;
System.out.println("i="+i);
System.out.println("k="+k);
}
System.out.println("i="+i);
System.out.println("k="+k);
}
}
2)class Test {
public static void main(String args[]) {
int 2a = 257;
byte b2 = 2a;
System.out.println("a=" + a);
System.out.println("b=" + b);
}
}
3)求一组数字的平均值 */
import javax.swing.JOptionPane;
class Example2_18 {
public static void main(string args[]) {
double nums[5] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
for(int i=0; i<5; i++)
{result = result + nums[i]; }
JOptionPane.showMessageDialog(null,"平均值为: " + result / 5); //弹出对话框输出
System.exit(0); //退出程序
}
}
4)import java.io.*;
class KY2_9 {
public static void main(String args[]) throws IOException {
char ch;
System.out.println("按 1/2/3 数字键可得大奖!");
System.out.println("按空格键后回车可退出循环操作.");
while ((ch=System.in.read())!=' ')
{
System.in.skip(2); // 跳过回车键,否则下次读入回车
switch (ch) {
case 1:
System.out.println("恭喜你得大奖,一辆汽车!");
break;
case 2:
System.out.println("不错呀,你得到一台笔记本电脑!");
break;
case 3:
System.out.println("没有白来,你得到一台冰箱!");
break;
default:
System.out.println("真不幸,你没有奖品!下次再来吧。");
}
}
}
2. 补充语句;
1)编写声明不同数据类型变量的程序文件 KY2_1.java,源代码如下,请补充完整:
public class KY2_1 {
public static void main(String args[]) {
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)在下面源代码中补充语句,使得输出如下的结果,编译并运行:
8 16 24 32 <>
7 14 21 28 <>
class Test {
public static void main(String args[]) {
int j, k;
for (j = 8; j >1; j--) {
for (k=1; k<=9; k++) {
if (k ==5)
if (j==6)
System.out.print(j*k+“ ”);
}
System.out.println(“<>”);
}
}
}
三.编程题
2. 通过命令行参数输入整数n和m,求两数的最小公倍数。
3. 编写程序,把字符串“1345”,“23.6”,“45.888”,“222”,“true"分别转换为整型、实型、双精度、逻辑型的基本类型(提示:用各基本类型对应包装类的parseXXX()方法)
3.实验分析、代码及运行结果
1.
1)public class KY2_2 {
static int i=10;
static int k=10;
public static void main(String args[]) {
{
System.out.println("i="+i);
System.out.println("k="+k);
}
System.out.println("i="+i);
System.out.println("k="+k);
}
}
运行结果:
分析: k的作用域在main函数中需要把其放到外面,然后静态成员函数里面只能直接使用静态成员,加static关键字
2)class Test {
public static void main(String args[]) {
int a2 = 257;
byte b2 = (byte)a2;
System.out.println("a=" + a2);
System.out.println("b=" + b2);
}
}
运行结果:
分析:标识符命名规则,不能以数字开头
3)求一组数字的平均值
import javax.swing.JOptionPane;
class Example2_18 {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
for(int i=0; i<5; i++)
{result = result + nums[i]; }
JOptionPane.showMessageDialog(null,"平均值为: " + result / 5); //弹出对话框输出
System.exit(0); //退出程序
}
}
运行结果:
分析: 声明数组不能写具体的大小,需要在new时指定,或初始化时(间接指定)
4)
import java.io.*;
class KY2_9 {
public static void main(String args[]) throws IOException {
char ch;
System.out.println("按 1/2/3 数字键可得大奖!");
System.out.println("按空格键后回车可退出循环操作.");
while ((ch=(char)System.in.read())!=' ')
{
System.in.skip(2); // 跳过回车键,否则下次读入回车
switch (ch-'0') {
case 1:
System.out.println("恭喜你得大奖,一辆汽车!");
break;
case 2:
System.out.println("不错呀,你得到一台笔记本电脑!");
break;
case 3:
System.out.println("没有白来,你得到一台冰箱!");
break;
default:
System.out.println("真不幸,你没有奖品!下次再来吧。");
}
}
}
}
运行结果:
分析: read()方法读入的是字符对应的ascii码值,需要转换成char类型,char类型之间进行算数运算会自动转型为int类型的ascii码值, ‘1’ - ‘0’ <==> 49 - 48 = 1
2.
1)
public class KY2_1 {
public static void main(String args[]) {
byte b = 123;
short s = 123;
int i = 123;
long l = 123;
char c = 'x';
float f = 123.0F;
double d = 123.000;
boolean B = true;
String S = "123";
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);
}
运行结果:
分析: 注意(float f = 123.0F;)这个,默认为double类型,后面加F转float类型
2) }
class Test {
public static void main(String args[]) {
int j, k;
BT:
for (j = 8; j >1; j--) {
for (k=1; k<=9; k++) {
if (k ==5)
break;
if (j==6) {
break BT;
}
System.out.print(j*k+ " ");
}
System.out.println("<>");
}
}
}
运行结果:
分析: java中弃用goto,可以使用(break 标记名;) (continue 标记名;)实现跳转
2.
public class Work {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
int m = Integer.parseInt(args[1]);
int maxF = maxFactor(m, n);
System.out.println(m*n/maxF);
}
public static int maxFactor(int m, int n) { // 求最大公约数
return n > 0 ? maxFactor(n, m%n) : m;
}
}
运行结果:
分析: 先递归求出最大公约数,最小公倍数等于 (a * b) / 最大公约数
3. public static void main(String args[]) {
String a = "1345";
String b = "23.6";
String c = "45.888";
String d = "222";
String e = "true";
int a1 = Integer.parseInt(a);
float b1 = Float.parseFloat(b);
double c1 = Double.parseDouble(c);
int d1 = Integer.parseInt(d);
boolean e1 = Boolean.parseBoolean(e);
System.out.printf("a1:%s b1:%s c1:%s d1:%s e1:%s",a1,b1,c1,d1,e1);
}
运行结果:
分析: 基本类型都有其对应的包装类和parseXXX方法,注意int 对应 Integer
总结:
Java有八大基本类型及包装类,分别是:
byte 包装类是 Byte
short 包装类是 Short
int 包装类是 Integer
long 包装类是 Long
float 包装类是 Float
double 包装类是 Double
char 包装类是 Character
boolean 包装类是 Boolean
然而这几种包装类都是在JAVA.lang中的。
其中Integer ,Byte,Float,Double,Short,Long都属于Number类的子类,Character和Boolean是属于Object子类。
显示一个弹窗:(实用小工具)
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null,"平均值为: " + result / 5); //弹出对话框输出
跳过回车键,否则下次读入回车
System.in.skip(2);
读取一个字符:
system.in.read(); // 读取的为字符对应的ascii码,类型:int
数组注意:
声明数组不能写具体的大小,需要在new时指定,或初始化时(间接指定)
如 int a[5] = {1,2,3,4,5}; // 错误