1、李尊朝java教材作业参考答案new作业参考答案习题一4、如何建立和运行Java程序?首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java作为文件扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。在集成开发环境Jbuilder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。5、public class LikeJava public static void main(String args) System.out.println(“I Li
2、ke Java Very much!”); 习题二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; /float pi=3.14159f;System.out.println(“球体积为:”+v); 习题三8、public class Factorials public static voi
3、d main(String args) int i, j; long s=0, k; i=1; do /外循环开始 k = 1; j=1; do/内循环开始 k = k * j; /内循环体 j+; while(j=i);/内循环结束 System.out.println(i + != + k); s = s + k; i+; while(i=20); /外循环结束 System.out.println(Total sum= + s); 10、public class Num public static void main(Stringargs) int i,j,k,n; for (n=100
4、;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+ ); 习题四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);
5、 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); 习题五2、import java.io.*;public class YangHuiOk public static void main (String args) throws IOE
6、xception int max,a,i,j; char x; System.out.print(请输入杨辉三角要显示的行数: ); 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
7、;i+) /for(j=0;j=max-i;j+) System.out.print( ); for(j=0;jmax)max=grade; if(grademin)min=grade; System.out.println(学生成绩中最高为:+max+,最低为:+min+,平均分为:+total*1.0/num); bf.close(); catch(FileNotFoundException e) System.out.println(e.toString(); catch(IOException e1) System.out.println(e1.toString(); 习题十一6、im
8、port java.awt.*;import java.awt.event.*;public class ChangeColor extends Frame private 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(C
9、olor.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.setBackgroun
10、d(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()
11、 public void windowClosing(WindowEvent e) System.exit(0); ); setSize(300,200); setVisible(true); public static void main (String args) ChangeColor color=new ChangeColor(); 习题十二5、import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Goods extends JFrame private JComboBox list; p
12、rivate 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
13、 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(产 地:+goodsindex1+n);
14、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); ); 习题十三4、import java.awt.*;import javax.swing.JApplet;publ
15、ic class SimpleApplet extends JApplet implements Runnable int j=0; Thread thread; public void init() thread=new Thread(this); thread.start(); public void stop() thread.destroy(); thread=null; public void run() while(true) repaint(); j+; j=j%3; try thread.sleep(4000); catch(InterruptedException e) pu
16、blic void paint(Graphics g) while(true) g.setFont(new Font(黑体,Font.BOLD,24); switch(j) case 0: g.setColor(Color.RED); break; case 1: g.setColor(Color.BLUE); break; case 2: g.setColor(Color.GREEN); g.drawString(各位同学大家好,你们辛苦啦! ,50,60); 习题十四4、public class UseThread extends Thread int st,en,count=0; UseThread(int m,int n) this.st=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1