JA V A语言程序设计实验报告
实验一:JA V A基本语法
班级:计算机083 姓名:陈晓旭学号:200810214303 日期:2011.05.06
一、实验目的
1.掌握各种数据类型及其使用方法
2.掌握定义变量的方法
3.掌握各种运算符的使用及其优先级控制
4.掌握字符串操作的常用方法
5.掌握基本语句的使用方法
6.掌握方法的定义与调用
7.掌握数组的定义与使用方法
二、实验内容(实验步骤)
题目1.分别运行以下四个独立程序,并解释标红色的语句执行结果。
程序1:
//package org.cxx.homework01;
public class SimpleTypes {
public static void main(String args[]) {
byte b = 055;
short s = 0x55ff;
int i = 1000000;
long l = 3615L;
char c = 'c';
float f = 0.23F;
double d = 0.7E-3;
boolean bool = true;
byte g = (byte) 255;
System.out.println("b=" + b);// b=055,0作为前缀,所以为8进制,055转换为10进制等于45,故标准输出为45
System.out.println("s=" + s);// 0X作为前缀,所以为16进制,0x55ff转换为10进制等于,故标准输出为22015
System.out.println("i=" + i);
System.out.println("l=" + l);
System.out.println("c=" + c + 0);
System.out.println("f=" + f);
System.out.println("d=" + d);// 0.7E-3 不是标准的科学计数法,标准输出则自动转换为7.0E-4
System.out.println("bool=" + bool);
System.out.println("g=" + g);// g 被强制转换为字节型,255为1111 1111 字节型最高位
}
}
程序2:
//package org.cxx.homework01;
public class ArithmaticOp {
public static void main(String args[]) {
int a = 5 + 4;
int b = a * 2;
int c = b / 4;
int d = b - c;
int e = -d;
int f = e % 4;
double g = 20.0;
double h = g % 4;
int i = 3;
int j = i++;
int k = ++i;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("e=" + e);
System.out.println("f=" + f);
System.out.println("g=" + g);
System.out.println("h=" + h);
System.out.println("i=" + i);// i 初始值为3,执行了j=i++,之后 i=4,j=3, k=++i 是
// 先加后赋值,故,i=5 k=5;
System.out.println("j=" + j);
System.out.println("k=" + k);
}
}
程序3:
//package org.cxx.homework01;
public class RelationAndConditionOp {
public static void main(String args[]) {
int a = 25, b = 3;
boolean d = a < b;
System.out.println(d);
int e = 3;
d = (
e != 0 && a / e > 5);
System.out.println(d); // && 优先度低于运算符,高于!=,因为e等于3,故e不等于 0 ,又因为 a/3>5
// ,故两个都为真,因为d为布尔型所以输出 true
int f = 2;
d = (d | a / f > 5);
System.out.println(d); // 为或运算,因为d之前为真,且a/f>5也为真,而或运算
// 要求至少一个为真,则输出为真,因此输出真}
}
程序4:
//package org.cxx.homework01;
public class MethodsOfString {
public static void main(String args[]) {
String str = "I like java programming";
int i1 = str.indexOf('j');
String s1 = str.substring(i1);
String s2 = str.substring(i1, i1 + 4);
int i2 = https://www.wendangku.net/doc/6d3503317.html,stIndexOf('j');
String s3 = str.substring(i2 + 5);
System.out.println("s1=" + s1);
System.out.println("s2=" + s2);
System.out.println("s3=" + s3);/*str.substring()函数为获得一个字符串的字串,s3 = str.substring(i2+5) 表示在i2后面的第五个字母开始,而i2= https://www.wendangku.net/doc/6d3503317.html,stIndexOf('j'); 意思为字符串的后面数,第一个j开始。*/
}
}
/*题目2:编写程序,实现输入学生的成绩等级,给出相应的成绩范围。设A级为80分以上(包括80分);
* B级为70分以上(包括70分);C级为60分以上(包括60分);D级为60分以下。*/
package org.cxx.homework01;
import java.io.*;
public class ChoseLeve {
public static void main(String[] args) {
BufferedReader buf = null;
buf = new BufferedReader(new InputStreamReader(System.in));
String str = null;
do {
System.out.print("Input the score level:");
try {
str = buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.equals("A")) {
System.out.print(str + ": the score is 80 up\n");
} else if (str.equals("B")) {
System.out.print(str + ": the score is 70 up\n");
} else if (str.equals("C")) {
System.out.print(str + ": the score is 60 up\n");
} else if (str.equals("D")) {
System.out.print(str + ": the score is 60 down\n");
} else {
System.out.print("Error.");
}
} while (!str.equals("E"));
}
}
/*题目3:编写程序,计算1!+2!+3!+……+10!,要求具有独立函数计算n!。*/ package org.cxx.homework01;
public class MultiplyDemo {
public static void main(String[] args) {
int temp0;
int temp1 = 0;
for(int i = 1;i <= 10;i++){
temp0 = 1;
for(int j = 1;j <= i;j++){
temp0 = temp0 * j;
}
System.out.println(i + "!=" + temp0);
temp1 = temp1 + temp0;
}
System.out.println("1!+2!+3!+4!+5!+6!+7!+8!+9!+10!="+temp1);
}
}
/*目4:编写程序,对集合{2,7,18,15,3,25,16,9,11,35}中的元素采用冒泡排序法由小到大排序。*/
package org.cxx.homework01;
public class SortDemo {
public static void main(String[] args) {
int[] a = { 2, 7, 18, 15, 3, 25, 16, 9, 11, 35 };
System.out.println("======== Sort before ========");
print(a);
int temp;
boolean flag = true;
for (int i = a.length - 1; i > 0 && flag; i--) {
for (int j = 0; j < i; j++) {
if (a[j] > a[j + 1]) {
flag = true;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println("\n======== Sort after =========");
print(a);
}
private static void print(int[] a) {
for (int k = 0; k < a.length; k++) {
System.out.print(a[k] + ",");
}
}
}
三、实验总结
通过这次试验我巩固了各种数据类型格式、转型等的操作及其使用,定义中间变量、局部全局变量,各种运算符的使用及其优先级控制,字符串操作,方法的定义与调用,数组的定义与使用方法,for循环语句嵌套,异常抛出。