1、JAVA简明教程习题答案JAVA简明教程习题答案 第二章课堂习题:public class Main public static void main(String args) /initialization phase初始段 float f1 = 2.5f; float f2; int a = 7; int b = 2; int c; /processing phase 处理段 f2 = f1 + (a/b); c = (a/b); /termination phase终止段 System.out.println(f2的值为: + f2); System.out.println(c的值为: +
2、 c); 课后习题:2-4(小程序部分)import java.applet.*;import java.awt.*;public class Gess extends Applet public void paint(Graphics g) /310000中的任意数 int m = 20; int n = 9993; g.drawString(本程序仅对310000中任意取的整数: ,80,50); g.drawString(20 ,80,65); g.drawString(9993 ,80,80); g.drawString(进行测试! ,80,95); while (m !=1) /假设
3、最后得到的数一定是1,则如果不为1就换算 if (m %2 = 0) /如果被2整除则为偶数,除以2 m = m /2; else /如果不被2整除则为奇数,乘以3再加1 m = m *3 + 1; while (n !=1) /假设最后得到的数一定是1,则如果不为1就换算 if (n %2 = 0) /如果被2整除则为偶数,除以2 n = n /2; else /如果不被2整除则为奇数,乘以3再加1 n = n *3 + 1; g.drawString(20换算后的值为: + m,80,110); g.drawString(9993换算后的值为: + m,80,125); (程序部分)imp
4、ort java.io.*; /引入java.io中的所有类(含输入输出类),因为程序中要用到输入类的read()方法public class Main public static void main(String args) byte buf =new byte50; /50个字节的数组用于存字符串 String str; long m = 0; System.out.println(请输入310000中的任意整数(回车确认):); try /try 语气 System.in.read(buf); /从键盘读入一数字串保存于buf中 str = new String(buf); /buf转换
5、成字符串对象str m = Long.parseLong(str.trim(); /数字转换成整数 catch (Exception e) /catch语气,Exception为异常类 while (m !=1) /假设最后得到的数一定是1,则如果不为1就换算 if (m %2 = 0) /如果被2整除则为偶数,除以2 m = m /2; else /如果不被2整除则为奇数,乘以3再加1 m = m *3 + 1; System.out.println(换算后的值为:t + m); 2-6public class Main public static void main(String args
6、) / initialization phase /初始段 int j = 1; int a; /用于存放第一个骰子的点数的整形数组 int b; /用于存放第二个骰子的点数的整形数组 int two = 0; /相加和为2的计数器 int sev = 0; /相加和为7的计数器 int twe = 0; /相加和为12的计数器 int oth = 0; /相加和为其他数的计数器 a = new int 6; /分配数组空间 b = a; /骰子的点数相同,这里将数组a赋给数组b(这时b和a同用一块内存空间) /processing phase 处理段 for(int i = 0; i 6;
7、i+) /通过循环赋值方式初始化数组 ai = j+; for(int i = 0; i 3600; i+) /测试3600次 /生成05随机数 int m = (int)(Math.random()*6 ); int n = (int)(Math.random()*6 ); if (am + bn) = 2) /当相加和为2时对应计数器加1 two+; else if (am + bn) = 7) /当相加和为7时对应计数器加1 sev+; else if(am + bn) = 12) /当相加和为12时对应计数器加1 twe+; else /当相加和为其他数时对应计数器加1 oth+; /
8、termination phase 终止段 /输出结果 /(float)two/3600)为将two/3600的结果强制转换成浮点数输出,否则输出为0 System.out.println(相加的和为 2 的次数是:t + two + ;t可能性为:t + (float)two/3600); System.out.println(相加的和为 12 的次数是:t + twe + ;t可能性为:t + (float)twe/3600); System.out.println(相加的和为 7 的次数是:t+ sev + ;t可能性为:t + (float)sev/3600); System.out.
9、println(相加的和为其他数的次数是:t + oth + ;t可能性为:t + (float)oth/3600) ); 第三章课堂习题随机产生26个英文字母a-z,直到大于u为止,用while或for语气实现public class Main public static void main(String args) int letter = 0; System.out.println(用 while 语句实现: ); while(letter u) letter = (int)(Math.random()*26) + a; System.out.println(char)letter);
10、System.out.println(用 for 语句实现: ); for(letter = 97; letter 1); return true; 3-2public class Main public static void main(String args) System.out.println(整数t + 平方t + 立方); /范围010 for(int i = 0; i = 10; i+) /依次打印输出整数,平方,立方 System.out.println(i + t + i*i + t + i*i*i); 3-3public class Main public static v
11、oid main(String args) / 把图分成三部分左中右三部分打印 for(int i = 0; i 5;i+ ) /循环打印出左边部分 for(int j = 0; j = i;j+) System.out.print(*); /循环打印出中间部分 for(int k = 2*i; k 18; k+) /空格要考虑前后各少一个所以从2*i开始 System.out.print( ); /循环打印出右边部分 for(int m = 0; m = i;m+) System.out.print(*); System.out.println(); System.out.println()
12、; 3-4import java.applet.Applet;import java.awt.*;public class Rectangle extends Applet Label lab; TextField input; int width; /矩形的长 public void init() lab = new Label(请输入矩形的长(整数):); /请示标签 input = new TextField(20); /输入文本框 add(lab); /在网页中显示标签lab add(input); /在网页中显示文本框 public boolean action(Event e, O
13、bject o) /当用户按下Enter时把输入的字符串转换成整形 if (e.target = input) width = Integer.parseInt( input.getText() ); System.out.println(下面是得到的矩形:); /矩形的上底 for(int i = 0; i width; i+) System.out.print(*); /矩形的边 for (int k = 0; k width-2; k+) System.out.println(); System.out.print(*); /矩形的中间为空 for (int m = 0; m width
14、-2; m+) System.out.print( ); System.out.print(*); System.out.println(); /矩形的下底 for(int j = 0; j width; j+) System.out.print(*); return true; 第四章4-1import java.io.*; public class ReverOrder static int x; int num; int m; int n; /从键盘读入字符串 public static String readString() BufferedReader br = new Buffer
15、edReader( new InputStreamReader(System.in),1 ); String string = ; try string = br.readLine(); catch( IOException ex ) System.out.println(ex); return string; /将读入的字符串转换为整形 public static int input() System.out.print(请输入一串数字:t); return Integer.parseInt(readString(); /倒序输入的字符串 int Rorder(int b) num = b;
16、 do m = num%10; /求余得最后一个数字 num = num/10; /取整去掉最后一个数字 n = n*10 + m; /把倒置后的数存到n中 while( num != 0); return n; /输出结果 void show(int a) System.out.println(倒序输出为:t + a); /主方法 public static void main(String args) int value; ReverOrder order = new ReverOrder(); x = ReverOrder.input(); /将输入的数传递给x value = orde
17、r.Rorder(x); /将x倒置 order.show(value); /输出 4-3package homework4_3;import java.io.*;public class Circle static double radius; double length; double area; static boolean checkr; /构造函数初始化半径 public Circle(double r) if(r 0) checkr = false; else radius = r; checkr = true; /从键盘读入字符串 public static String rea
18、dString() BufferedReader br = new BufferedReader( new InputStreamReader(System.in),1 ); String string = ; try string = br.readLine(); catch( IOException ex ) System.out.println(ex); return string; /将读入的字符串转换为整形 public static double input() System.out.print(请输入圆的半径:t); return Double.parseDouble(readS
19、tring(); /计算圆的周长的方法 void Length(double r) System.out.println(圆的周长为:t + Math.PI*r*2); /计算圆的面积 void Area(double r) System.out.println(圆的面积为:t + Math.PI*r*r); /主方法 public static void main(String args) double valueinput; double valuer; Circle r = new Circle(input(); /如果半径为正则求周长和面积 if(checkr) r.Area(radi
20、us); r.Length(radius); else System.out.println(输入圆的半径的值不能为负数!); 4-4public class Date static int year; static int month; static int day; public Date() /无参数的构造函数 /有三个参数的构造函数 public Date(int x, int y, int z) day = x; month = y; year = z; /主方法 public static void main(String args) year = 2008; month = 06
21、; day = 21; /()三种格式输出今天的日期 System.out.print(今天是: ); System.out.println(t + year + + month + + day ); System.out.println(t + month + + day + + year ); System.out.println(t + day + + month + + year ); /(2)调用重载构造函数创建对象date Date date = new Date(22,06,2008); System.out.print(n明天是: ); System.out.println(t
22、 + year + + month + + day ); System.out.println(t + month + + day + + year ); System.out.println(t + day + + month + + year ); 4-6/基类class base /方法一 void First() System.out.println(这是基类base的第一个方法!); Second(); /方法二 void Second() System.out.println(这是基类base的第二个方法!); /派生类class derived extends base void
23、 Second() System.out.println(这是派生类derived的方法!); public class UpCasting /主方法 public static void main(String args) /派生类derived对象test,test对象里有覆盖了基类的Second()方法的Second()方法 derived test = new derived(); base up = test; /向上类型转换, 基类对象up里有有First()方法和被覆盖的Second() /base up = new base(); /基类对象up里仅有First()方法和未被覆
24、盖的Second() up.First(); /调用基类的第一个方法 实验题目实验一(略)实验二(略)实验三import javax.swing.JOptionPane;public class GuessNumber public static void main(String args) System.out.println(给你一个1到100的整数,请猜测这个数); int realNumber =(int)(Math.random()*100)+1; /随机生成1100的整数 int yourGuess =0; /初始化输入的值 String str =JOptionPane.showInputDialog(输入你的猜测: ); yourGuess = Integer.parseInt(str); /将输入的字符串轮换成整形