java語言程序設(shè)計 第二版 (叢書主編:譚浩強 編著:李尊朝 蘇軍 著) 中國鐵道出版社 課后答案
作業(yè)參考答案習(xí)題一4、如何建立和運行Java程序?首先啟動文本編輯器,如記事本、UltraEdit等,編輯程序代碼,并以.Java作為文件擴展名保存程序源代碼;然后進入dos環(huán)境利用javac編譯源程序,生成擴展名為.class的字節(jié)碼文件;再利用命令java運行字節(jié)碼文件,得到程序的運行結(jié)果。在集成開發(fā)環(huán)境Jbuilder、Eclipse下,可以完成程序的編輯、編譯、調(diào)試及運行等所有任務(wù)。5、public class LikeJava public static void main(String args) System.out.println(“I Like Java Very much!”); 習(xí)題二5、(1) 45 (2) false (3) 14 (4) 14 (5)6 (6) true (7) 12 9、public class Volume public static void main(String args) double r=0,v=0; r=double.parseDouble(args0); v=4*3.14159/3*r*r*r; System.out.println(“球體積為:”+v); 習(xí)題三8、public class Factorials public static void main(String args) int i, j;long s=0, k;i=1;do /外循環(huán)開始k = 1;j=1;do/內(nèi)循環(huán)開始k = k * j; /內(nèi)循環(huán)體j+;while(j=i);/內(nèi)循環(huán)結(jié)束System.out.println(i + != + k);s = s + k;i+;while(i=20); /外循環(huán)結(jié)束System.out.println(Total sum= + s);10、public class Num public static void main(Stringargs) int i,j,k,n; for (n=100;n1000;n+) i=n/100; j=(n-i*100)/10; k=n%10; if (i*i*i+j*j*j+k*k*k=n) System.out.print(n+ ); 習(xí)題四5、import java.util.Scanner;class Factorlong fac(int m)if(m=0|m=1)return 1; else return m*fac(m-1); public static void main(String args) int i,n; long sum=0; String s=; Scanner input=new Scanner(System.in); System.out.print(Please input n: ); n=input.nextInt(); Factor f=new Factor(); for(i=1;i=n;i+) System.out.println(f.fac(i); sum=sum+f.fac(i); s=s+i+!+; System.out.println(s.substring(0,s.length()-1)+=+sum); 習(xí)題五2、import java.io.*;public class YangHuiOk public static void main (String args) throws IOException int max,a,i,j; char x; System.out.print(請輸入楊輝三角要顯示的行數(shù): ); x=(char)System.in.read(); max = Integer.parseInt(String.valueOf(x); a=new intmax; for (i=0;imax;i+) ai=new inti+1; a00=1; for (i=1;imax;i+) ai0=1; aiai.length-1=1; for (j=1;jai.length-1;j+) aij=ai-1j-1+ai-1j; for(i=0;imax;i+) /for(j=0;j=max-i;j+) System.out.print( ); for(j=0;j=ai.length-1;j+) System.out.print(aij+ ); System.out.println(); 5、import java.util.Scanner;public class MatrixTurn public static void main (String args) int m,n;Scanner input=new Scanner(System.in); System.out.print(請輸入矩陣的行數(shù): ); m=input.nextInt(); System.out.print(請輸入矩陣的列數(shù): ); n=input.nextInt(); Matrix t=new Matrix(m,n); for(int i=1;i=m;i+)/為矩陣各元素賦值 for (int j=1;j=n;j+) t.setElement(Math.random(),i,j); System.out.println(轉(zhuǎn)置前的矩陣如下: ); for(int i=1;i=m;i+) for (int j=1;j=n;j+) /System.out.print(t.matrixij+ ); System.out.print(t.getElement(i,j)+ );/訪問矩陣元素方法1 System.out.println(); Matrix z;/聲明轉(zhuǎn)置矩陣 z=t.turn(t); System.out.println(轉(zhuǎn)置后的矩陣如下: ); for(int i=0;in;i+) for (int j=0;jmax)max=grade; if(grademin)min=grade; System.out.println(學(xué)生成績中最高為:+max+,最低為:+min+,平均分為:+total*1.0/num); bf.close(); catch(FileNotFoundException e) System.out.println(e.toString(); catch(IOException e1) System.out.println(e1.toString(); 習(xí)題十一6、import java.awt.*;import java.awt.event.*;public class ChangeColor extends Frameprivate Button red=new Button(紅);private Button green=new Button(綠);private Button blue=new Button(藍);private TextField text=new TextField();public ChangeColor()super(改變顏色);this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)text.setBackground(Color.RED););green.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)text.setBackground(Color.GREEN););blue.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)text.setBackground(Color.BLUE);); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); setSize(300,200); setVisible(true); public static void main (String args) ChangeColor color=new ChangeColor(); 習(xí)題十二5、import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Goods extends JFrameprivate JComboBox list;private JTextArea info;private String names=請選擇你要查詢的商品,A商品,B商品,C商品,D商品,E商品,F商品;private String goods= , A商品,北京,¥300, B商品,上海,¥400, C商品,廣州,¥500, D商品,長沙,¥600, E商品,武漢,¥700, F商品,天津,¥800;public Goods()super(商品信息);Container pane=this.getContentPane();pane.setLayout(new BorderLayout();list=new JComboBox(names);info=new JTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent e)int index=list.getSelectedIndex();info.setText(商品名:+goodsindex0+n);info.append(產(chǎn) 地:+goodsindex1+n);info.append(價 格:+goodsindex2+n););this.setSize(250,300);this.setVisible(true); public static void main (String args) Goods ccc=new Goods(); ccc.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); );
收藏