java报告代码 java代码大全手册( 三 )


operator = "=";
}
/**
* 处理运算符键被按下的事件
* @param key
*/
private void handleOperator(String key) {
if (operator.equals("/")) {
//除法运算
//如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
//倒数运算
if (resultNum == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("零没有倒数");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")){
//加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
//减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
//乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
//平方根运算
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")){
//百分号运算,除以100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")){
//正数负数运算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
//赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
//双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
//运算符等于用户按的按钮
operator = key;
firstDigit = true;
operateValidFlag = true;
}
/**
* 从结果文本框中获取数字
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e){
}
return result;
}
public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.setVisible(true);
calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JAVA课程设计报告:将当前窗体压缩成ZIP文件(附上源代码) 。楼主 , 看java.io包里面的SerializableAPI 文档和ObjectInputStream
ObjectOutputStream
java实验报告总结源代码:
public class Exe1_1 {
public static void main (String args[]){
byte a1=127;
int a2=12345;
long a3=123456789L;
float a4=1.23e-5f;
double a5=1.4567e3;
char a6=65;
boolean a7=true;
System.out.println("a1="+a1+"\na2="+a2+"\na3="+a3+"\na4="+a4+"\na5="+a5+"\na6="+a6+"\na7="+a7);
}
}运行结果:
2.编写Java小应用程序 , 输出两行字符串:“Java很有趣 。”和“努力学习Java编程 。”,输出的起点坐标是(20,20) , 行距是50像素 。源程序代码:
import java.awt.Graphics;
import java.applet.Applet;
public class Exe1_2 extends Applet{
public void paint(Graphics g){
g.drawString("Java很有趣 。",20,20);
g.drawString("努力学习Java编程 。",20,70);
}
}运行情况:
3.使用算术运算符得到一个4位十进制数的各位数字并输出,然后输出该数的逆序数和各位数字平方后相加的和 。
源程序代码:
public class Exe1_3 {
public static void main (String args[]){
int n=3756,a,b,c,d,n1;
a=n/1000;
b=(n-1000*a)/100;
d=n%10;
c=(n%100-d)/10;
System.out.println("3756的逆序数为:"+d+" "+c+" "+b+" "+a);
System.out.print("各位数字平方后相加的和为:");

推荐阅读