2010年秋季Java考试编程题总结
更新时间:2024-04-11 03:42:01 阅读量: 综合文库 文档下载
- 2010世界杯推荐度:
- 相关推荐
1.编写一个程序,程序能在命令行中输出“早上好,good Morning”。
public class Hello{
public static void main (String args[]){ System.out.println(\早上好,goodMoeing\); } }
2.编写一个Java Applet程序,程序能在浏览器中显示\你好,hello\。
import java.applet.*; import java.awt.*;
public class Boy extends Applet {
public void paint(Graphics g){ //g.setColor(Color.blue);
g.drawString(\你好,hello\,12,30);} }
3.编写一个程序,输出ASICII字母表。
public class java1 {
public static void main(String[] args) { char c='!';
System.out.println(\字母\+c+\在ASICII表中的顺序位置:\+(int)c);
System.out.println(\输出ASICII字母表:\); for(int i=(int)c;i 4. 用户从键盘只能输入整数,程序输出这些整数的乘积。 import java.util.*; public class ZuoYe2_1 { public static void main(String args[]){ Scanner reader=new Scanner(System.in); long s=1; int m=0; while(reader.hasNextInt()){ int x=reader.nextInt(); m=m+1; s=s*x;} System.out.println(m+\个数的乘积为\+s);} } 5.有一函数:从键盘输入一个X值,程序输出Y值。 import java.util.*; public class ZuoYe2_1{ public static void main(String args[]){ double y=0,x=0; x=reader.nextDouble(); if(x<0) y=-1+2*x; y=-1; y=-1+3*x; else if(x==0) else if(x>0) Scanner reader=new Scanner(System.in); } System.out.println((int)y);} 6. 使用while循环计算1~1000之间能被3和7同时整除的整数之和。 public class ZuoYe2_1{ public static void main(String args[]){ } int sum=0,m=3,n=7,a=1; while(a<=1000){ if(a%m==0&&a%n==0) sum=sum+a; a++;} System.out.println(\+sum);} 7. 编写一个Java应用程序,使用for循环计算8+88+888+8888+88888+…的前10项之和。 public class ZuoYe2_1{ public static void main(String args[]){ long sum=0,a=8,item=a,i=1; for(i=1;i<=10;i++) { sum=sum+item; item=item*10+a; } System.out.println(sum);} } 8. 编写一个Java应用程序,计算1-1/3+1/5-1/7+1/9-1/11+…的前10000项之和。 public class ZuoYe2_1{ public static void main(String args[]){ double sum=0,a=1,b=1,f=1,item=a/b; int i=1; while(i<=1000) { sum=sum+f*item; i++; f=f*(-1); b=b+2; item=a/b;} System.out.println(\+sum);} } 9. 编写一个Java应用程序,计算1+2!+3!+4!+…从100项到200项之和。 public class ZuoYe2_1{ } public static void main(String args[]){ double sum=0,a=1; int i=1; while(i<=20) {if(i>=10) sum=sum+a; i++; a=a*i;} System.out.println(\+sum);} 10. 编写一个类,该类创建的对象可以计算等差数列的和。 class DengCha{ int start,d; DengCha(){} DengCha(int start,int d){ this.start=start; this.d=d;} void setStart(int s){ start=s;} void setD(int d){ this.d=d;} int getSum(int n){ int sum=0,i=1; while(i<=n){ sum=sum+start; start=start+d; i++;} return sum;} } public class ZuoYe2_1{ public static void main (String args[ ]){ } DengCha shulie=new DengCha(2,3); shulie.setStart(10); shulie.setD(5); System.out.println(shulie.getSum(9));} System.out.println(shulie.getSum(100)); 11. 编一个类,该类创建的对象可以输出英文字母表。 class Letter{ public void printLetter(){ for(char c='a';c<='z';c++) System.out.print(\+c);} } public class ZuoYe2_1{ public static void main (String args[ ]){ } Letter p=new Letter(); p.printLetter(); } 12. 编一个类,该类封装了一元二次方程共有的属性和功能,即该类有刻画方程系数的三个成员变量以及计算实根的方法。要求:该类的所有对象共享常数项。 class SquareEquation{ double a,b; static double c; double root1,root2; boolean boo; public SquareEquation(double a,double b,double c){ this.a=a; this.b=b; SquareEquation.c=c; if(a!=0){boo=true;} else{ boo=false;} } public void getRoots(){ if(boo){ System.out.println(\是一元2次方程\); double disk=b*b-4*a*c; if(disk>=0){ root1=(-b+Math.sqrt(disk))/(2*a); root2=(-b-Math.sqrt(disk))/(2*a); System.out.printf(\方程的根:%f,%f\\n\,root1,root2); } else{ System.out.printf(\方程没有实根\\n\);} } else{ System.out.println(\不是一元2次方程\);} } public void setCoefficient(double a,double b,double c){ this.a=a; this.b=b; SquareEquation.c=c; if(a!=0){boo=true;} else{boo=false;} } } public class ZuoYe2_1{ public static void main(String args[ ]){ SquareEquation equation1=new SquareEquation(4,5,1); SquareEquation equation2=new SquareEquation(3,5,-7); equation1.getRoots(); equation2.getRoots();} } 13. 编写两个类,A和B,类A创建的对象可以计算两个正整数的最大公约数,类B创建的对象可以计算两个书的最小公倍数。要求:类B中有一个成员变量是用类A的声明对象。 import java.util.Scanner; class A{ int f(int m,int n){ if(m*n<0) { System.out.println(\有负数,程序退出\); System.exit(0); } if(m { int temp=m; m=n; n=temp; } int a=m,b=n; int r=m%n; while(r!=0) { m=n; n=r; r=m%n;} return n;} } class B{ A a; B(){ a=new A();} int g(int m,int n){ int temp=a.f(m,n); return m*n/temp;} } public class ZuoYe2_1{ public static void main (String args[ ]){ Scanner reader=new Scanner(System.in); System.out.println(\输入2个正整数,程序计算出它们的最大公约数和最小公倍数\); System.out.print(\输入第一个整数:\); int m=reader.nextInt(); System.out.print(\输入第二个整数:\); int n=reader.nextInt(); A a=new A(); B b=new B(); System.out.println(m+\和\+n+\的最大公约数是\+a.f(m,n)); System.out.println(m+\和\+n+\的最小公倍数是\+b.g(m,n)); }} 14. 编写使用了包语句的类,然后在应用程序中用import语句引入该类,并用该类创建对象。 import java.applet.Applet; import java.awt.*; public class ZuoYe2_1 extends Applet{ Button redbutton; public void init(){ redbutton=new Button(\我是一个红色的按钮\); redbutton.setBackground(Color.red); redbutton.setForeground(Color.white); add(redbutton);} } 15. 编写一个类,该类有一个方法 Public int f(int a,int b) { //要求该方法返回a和b的最大公约数 } 然后编写一个该类的子类,要求子类重写方法f(),而且重写的方法将返回两个整数的最小公倍数。要求:在成功写的方法的方法体中首先调用被隐藏的方法返回a和b的最大公约数m,然后将(a*b)/m返回;在应用的程序的主类中分别使用父类和子类创建对象,并分别调用方法f()计算两个正整数的最大公约数和最小公倍数。 import java.util.Scanner; class A{ public int f(int m,int n) { if(m while(r!=0){ m=n; n=r; r=m%n;} return n;} } class B extends A{ public int f(int m,int n) { int division=super.f(m,n); return (m*n)/division;} } public class ZuoYe2_1 { public static void main (String args[ ]){ A a=new A(); B b=new B(); Scanner reader=new Scanner(System.in); System.out.println(\输入2个整数,程序计算出它们的最大公约数和最小公倍数\); System.out.print(\输入第一个整数:\); int m=reader.nextInt(); System.out.print(\输入第二个整数:\); int n=reader.nextInt(); if(m*n<0) { System.out.println(\有负数,程序退出\); System.exit(0); } System.out.printf(\和%d的最大公约数是%d\\n\,m,n,a.f(m,n)); System.out.printf(\和%d的最小公倍数是%d\\n\,m,n,b.f(m,n)); } } 16. 首先编写一个抽象类,要求该类有如下3个抽象方法: Public abstract void f(int x); Public abstract void g(int x.int y); Public abstract double h(double x); 然后分别给出该类的3个子类。要求:在应用程序的主类中使用这些子类创建对象,然后让它们的上转型对象调用方法:f(),g()和h()。 abstract class A{ public abstract void f(int x); public abstract void g(int x,int y); public abstract double h(double x);} class A1 extends A{ public void f(int x){ System.out.println(x); } public void g(int x,int y){ int z=x+y; System.out.println(z); } public double h(double x){ return x*x;} } class A2 extends A{ public void f(int x){ System.out.println(\+x); } public void g(int x,int y){ int z=x-y; System.out.println(z); } public double h(double x){ return Math.sqrt(x);} } class A3 extends A{ public void f(int x){ System.out.println(\你好:\+x); } public void g(int x,int y){ double z=(double)x/y; System.out.println(z); } public double h(double x){ return 1/x;} } public class ZuoYe2_1 { public static void main(String args[ ]){ A a=new A1(); a.f(10); a.g(12,20); System.out.println(a.h(100)); a=new A2(); a.f(10); a.g(12,20); System.out.println(a.h(100)); a=new A3(); a.f(10); a.g(12,20); System.out.println(a.h(100));} } 17. 编写一个类,要求该类实现一个接口,该接口有如下3个抽象方法: Public abstract void f(int x); Public abstract void g(int x.int y); Public abstract double h(double x); 要求:在应用程序的主类中使用该类创建对象,并使用接口回调来调用方法:f(),g()和h()。 interface A{ public abstract void f(int x); public abstract void g(int x,int y); public abstract double h(double x);} class A1 implements A{ public void f(int x){ System.out.println(x); } public void g(int x,int y){ int z=x+y; System.out.println(z); } public double h(double x){ return x*x;} } class A2 implements A{ public void f(int x){ System.out.println(\+x); } public void g(int x,int y){ int z=x-y; System.out.println(z); } public double h(double x){ return Math.sqrt(x);} } class A3 implements A{ public void f(int x){ System.out.println(\你好:\+x); } public void g(int x,int y){ double z=(double)x/y; System.out.println(z); } public double h(double x){ return 1/x;} } public class ZuoYe2_1 { public static void main(String args[ ]){ A a=new A1(); a.f(10); a.g(12,20); System.out.println(a.h(100)); a=new A2(); a.f(10); a.g(12,20); System.out.println(a.h(100)); a=new A3(); a.f(10); a.g(12,20); System.out.println(a.h(100)); } } 18. 举例说明匿名类的用法。 class Cubic { double getCubic(int n) { return 0;} } abstract class Sqrt{ public abstract double getSqrt(int x);} class A{ void f(Cubic cubic){ double result=cubic.getCubic(3); System.out.println(result);} } public class ZuoYe2_1 { public static void main(String args[]){ A a=new A(); a.f(new Cubic() { return n*n*n;} }); Sqrt ss=new Sqrt() { public double getSqrt(int x) { return Math.sqrt(x); } }; double m=ss.getSqrt(5); System.out.println(m); } } { double getCubic(int n) 19. 编写一个异常类,并具体给出一个产生该异常的方法。在一个应用程序中测试该异常类。 class IntegerException extends Exception { String message; IntegerException(int m) { message=\年龄\+m+\不合理\;} public String toString() { return message;} } class People { private int age=1; public void setAge(int age) throws IntegerException{ if(age>=160||age<=0) throw new IntegerException(age); else this.age=age;} public int getAge() { System.out.println(\年龄\+age+\合理\); return age;} } public class ZuoYe2_1 { public static void main(String args[]) { People wang=new People(), zhang=new People(); try { wang.setAge(189); System.out.println(wang.getAge());} catch(IntegerException e) try { zhang.setAge(28); System.out.println(zhang.getAge());} catch(IntegerException e) { System.out.println(e.toString());} } } { System.out.println(e.toString());} 20. 编写一个应用程序,用户从键盘输入一行字符串,程序输出该字符串中与模式”[24680]A[13579]{2}”匹配的子字符串。 import java.util.regex.*; import java.util.*; public class ZuoYe2_1 { public static void main(String args[ ]){ Scanner reader=new Scanner(System.in); String s1=reader.nextLine(); Pattern p; m=p.matcher(s1); while(m.find()){ String str=m.group(); System.out.print(\从\到\匹配模式子序列:\ System.out.println(str);} } } 21. 编写一个应用程序,用户从键盘输入一行含有数字字符的字符串,程序仅仅输出字符串中的全部数字字符。 import java.util.regex.*; import java.util.*; public class ZuoYe2_1 { public static void main(String args[ ]){ Scanner reader=new Scanner(System.in); String s1=reader.nextLine(); Pattern p; Matcher m; Matcher m; p=Pattern.compile(\ p=Pattern.compile(\); m=p.matcher(s1); while(m.find()){ String str=m.group(); System.out.print(str);} } } 22. 设计5个人排队买票,并规定卖票规则和排队顺序。//设计如下: 一张电影票5元。卖票员有一张10元。买 票人:A-5元,B-5元,C-10元,D-10元,E-20元。 public class ZuoYe2_1 { public static void main(String args[]){ String s1=\,s2=\,s3=\, s4=\,s5=\; Cinema canema=new Cinema(s1,s2,s3,s4,s5); Thread a,b,c,d,e; a=new Thread(canema); b=new Thread(canema); c=new Thread(canema); d=new Thread(canema); e=new Thread(canema); a.setName(s1); b.setName(s2); c.setName(s3); d.setName(s4); e.setName(s5); a.start(); b.start(); c.start(); d.start(); e.start();} } TicketSeller seller; String name1,name2,name3,name4,name5; seller=new TicketSeller(); name1=s1; name2=s2; name3=s3; name4=s4; name5=s5;} public void run(){ class Cinema implements Runnable{ Cinema(String s1,String s2,String s3,String s4,String s5){ if(Thread.currentThread().getName().equals(name1)){seller.sellTicket(5);} else if (Thread.currentThread().getName().equals(name2)){seller.sellTicket(5);} else if (Thread.currentThread().getName().equals(name3)){seller.sellTicket(10);} else if (Thread.currentThread().getName().equals(name4)){seller.sellTicket(10);} else if (Thread.currentThread().getName().equals(name5)){seller.sellTicket(20);} } class TicketSeller{ int fiveNumber=0,tenNumber=1, twentyNumber=0; public synchronized void sellTicket(int receiveMoney){ String s=Thread.currentThread().getName(); if(receiveMoney==5){ fiveNumber=fiveNumber+1; System.out.println(s+\给售票员5元钱,售票员卖给\+s+\一张票,不必找赎\);} else if(receiveMoney==10){ while(fiveNumber<1){ try{ System.out.println(s+\给售票员10元 } 钱\); System.out.println(\售票员请\+s+\靠边等一 会\); wait(); System.out.println(s+\结束等待,继续买 票\);} catch(InterruptedException e){}} fiveNumber=fiveNumber-1; tenNumber=tenNumber+1; System.out.println(s+\给售票员10元钱,售票员卖给\+s+\一张票,找赎5元\);} else if(receiveMoney==20){ while(fiveNumber<1||tenNumber<1){ try{ System.out.println(s+\给售票员20元钱\); System.out.println(\售票员请\+s+\靠边等一 会\); wait(); System.out.println(s+\结束等待,继续买 票\);} catch(InterruptedException e){}} fiveNumber=fiveNumber-1; tenNumber=tenNumber-1; twentyNumber=twentyNumber+1; System.out.println(s+\给售票员20元钱,售票员卖给\+s+\一张票,找赎15元\);} notifyAll();} } 23. 编写一个应用程序,读取一个文本文件的内容。 import java.io.*; import java.awt.*; import java.awt.event.*; public class ZuoYe2_1 { public static void main(String args[]) { int b; byte tom[]=new byte[25]; try{ File f=new File(\); FileInputStream in= new FileInputStream(f); while((b=in.read(tom,0,25))!=-1) { String s=new String (tom,0,b); System.out.print(s);} in.close();} catch(IOException e) { System.out.println(\+e);}}} 24. 编写一个应用程序,将用户从键盘输入的10行文字存入文件。 import java.io.*; import java.util.*; public class ZuoYe2_1 { public static void main(String args[]){ Scanner reader=new Scanner(System.in); int b; try{ FileOutputStream writefile= new FileOutputStream(\); int line=1,n=10; System.out.println(\输入\+n+\行文本,并存入磁盘:\); while(line<=n){ String s=reader.nextLine(); byte buffer[]=s.getBytes(); writefile.write(buffer,0,buffer.length); line++;} writefile.close();} catch(IOException e){ System.out.println(\+e);} } } 25. 使用数组字符流将俄文字母写入内存,然后再从内存读出。 import java.io.*; public class ZuoYe2_1 { public static void main(String args[ ]){ int n=-1; new CharArrayWriter(); for(char c='а';c<='я';c++){ out.write(c); } CharArrayReader in= new CharArrayReader(out.toCharArray()); try{ while((n=in.read())!=-1){ if(n%2==0){ System.out.printf(\);} System.out.printf(\位置%d,字符\\'%c\\'\,n,(char)n);} } catch(IOException e){} } } CharArrayWriter out= 26. 编写一个应用程序,将用户从键盘输入的10个整数存入文件,然后按顺序读出。 import java.io.*; import java.util.*; public class ZuoYe2_1 { public static void main(String args[]){ try{ FileOutputStream fos=new FileOutputStream(\); DataOutputStream out_data= new DataOutputStream(fos); Scanner reader=new Scanner(System.in); for(int i=1;i<=10;i++){ int x=reader.nextInt(); out_data.writeInt(x);} out_data.close();} try{ new FileInputStream(\); DataInputStream in_data= new DataInputStream(fis); for(int i=1;i<=10;i++){ catch(IOException e){} FileInputStream fis= } int m=in_data.readInt(); in_data.close();} catch(IOException e){} } System.out.print(\+m);} 27. 编写一个应用程序,要求将LinkedList 遍历LinkedList import java.io.*; import java.util.*; class Student implements Serializable{ String name ; int number; Student(String name,int number){ this.name=name; this.number=number;} } public class ZuoYe2_1 { public static void main(String args[]){ List new LinkedList list.add(new Student(\+k,k)); try{ new FileOutputStream(\); ObjectOutputStream objectOut= new ObjectOutputStream(fileOut); objectOut.writeObject(list); new FileInputStream(\); ObjectInputStream objectIn= new ObjectInputStream(fileIn); cloneList=(List)objectIn.readObject();} catch(Exception event){ System.out.println(event);} Iterator iter=cloneList.iterator(); while(iter.hasNext()){ Student te=(Student)iter.next(); System.out.println(te.number+\+ te.name);} } } 28.用RandomAccessFile流将一个文本文件倒置读出。 import java.io.*; public class ZuoYe2_1 { public static void main(String args[]){ FileInputStream fileIn= FileOutputStream fileOut= File f=new File(\try{ new RandomAccessFile(f,\ RandomAccessFile random= 29. 编写一个应用程序,在应用程序中有一个按钮和一个文本框。当单击按钮时,文本框显示按钮的名字。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 { public static void main(String args[]){ MathWindow win=new MathWindow();} } class MathWindow extends JFrame{ JTextField inputText; JButton button; MathWindow(){ inputText=new JTextField(10); button=new JButton(\); button.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e) { inputText.setText(button.getText()); }}); setLayout(new FlowLayout()); add(inputText); add(button); setBounds(100,100,260,190); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);} } random.seek(0); long m=random.length(); while(m>=0) { m=m-1; random.seek(m); int c=random.readByte(); if(c<=255&&c>=0) { System.out.print((char)c);} else { m=m-1; random.seek(m); byte cc[]=new byte[2]; random.readFully(cc); System.out.print(new String(cc)); } } random.close();} } catch(IOException ee){}} 30. 编写一个有两个文本框和一个按钮的应用程序,在一个文本框输入一个字符串按回车键或单击按钮,另一 个文本框都显示字符串中每个字符在Unicode表中的顺序位置。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 { public static void main(String args[]){ MathWindow win=new MathWindow();} } JTextField inputText,showUnicode; JButton button; MathWindow(){ inputText=new JTextField(10); showUnicode=new JTextField(10); button=new JButton(\); button.addActionListener(this); inputText.addActionListener(this); setLayout(new FlowLayout()); add(inputText); add(button); add(showUnicode); setBounds(100,100,260,190); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);} String s=inputText.getText(); StringBuffer str=new StringBuffer(); for(int i=0;i class MathWindow extends JFrame implements ActionListener{ public void actionPerformed(ActionEvent e){ showUnicode.setText(new String(str));} 31. 编写一个应用程序,设计4个按钮,分别命为:“加”、“差”、“乘”、“除”;还有3个文本框,单击相应的 按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 { public static void main(String args[]) { ComputerFrame fr= new ComputerFrame(); fr.setTitle(\计算\); } } class ComputerFrame extends JFrame implements ActionListener{ JTextField text1,text2,text3; JButton button1,button2,button3,button4; JLabel label; public ComputerFrame(){ setLayout(new FlowLayout()); text1=new JTextField(10); text2=new JTextField(10); text3=new JTextField(10); label=new JLabel(\,JLabel.CENTER); label.setBackground(Color.green); add(text1); add(label); add(text2); add(text3); button1=new JButton(\加\); button2=new JButton(\减\); button3=new JButton(\乘\); button4=new JButton(\除\); add(button1); add(button2); add(button3); add(button4); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setSize(300,320); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} }); validate();} { double n; if(e.getSource()==button1) { double n1,n2; public void actionPerformed(ActionEvent e) try{ n1=Double.parseDouble(text1.getText()); n2=Double.parseDouble(text2.getText()); n=n1+n2; label.setText(\);} text3.setText(String.valueOf(n)); catch(NumberFormatException ee) { text3.setText(\请输入数字字符\);} } else if(e.getSource()==button2) { double n1,n2; try{ n1=Double.parseDouble(text1.getText()); n2=Double.parseDouble(text2.getText()); n=n1-n2; text3.setText(String.valueOf(n)); label.setText(\);} catch(NumberFormatException ee) { text3.setText(\请输入数字字符\);} } else if(e.getSource()==button3) {double n1,n2; try{ n1=Double.parseDouble(text1.getText()); n2=Double.parseDouble(text2.getText()); n=n1*n2; label.setText(\); } text3.setText(String.valueOf(n)); catch(NumberFormatException ee) { text3.setText(\请输入数字字符\);} } else if(e.getSource()==button4) {double n1,n2; try{ n1=Double.parseDouble(text1.getText()); n2=Double.parseDouble(text2.getText()); n=n1/n2; text3.setText(String.valueOf(n)); label.setText(\);} catch(NumberFormatException ee) { text3.setText(\请输入数字字符\);} } validate();} } 32. 编写一个应用程序,要求有一个含有菜单的窗口,窗口中有文本区组件。菜单有“打开文件”的菜单项, 当单击该菜单项时,使用输入流将一个名字为“hello.txt”文件的内容读入到文本中。 import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; public class ZuoYe2_1 { public static void main(String args[]){ new ReadFileWindow();} } JMenuBar menubar; JMenu menu; JMenuItem openFile; JTextArea showText; ReadFileWindow(){ menubar=new JMenuBar(); menu=new JMenu(\文件\); openFile=new JMenuItem(\打开文件\); menu.add(openFile); class ReadFileWindow extends JFrame implements ActionListener{ menubar.add(menu); setJMenuBar(menubar); showText=new JTextArea(12,12); add(new JScrollPane(showText)); validate(); setBounds(120,120,500,370); setVisible(true); openFile.addActionListener(this); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);} public void actionPerformed(ActionEvent e){ String fileName=\; File readFile=new File(fileName); showText.setText(null); new FileReader(readFile); BufferedReader inTwo= new BufferedReader(inOne); String s=null; int i=0; while((s=inTwo.readLine())!=null) inOne.close(); inTwo.close();} try{ FileReader inOne= showText.append(\+s); catch(IOException ex){ showText.setText(ex.toString()); } } } 33.编写有两个文本区的应用程序。当我们在一个文本区中输入若干个数时,另一个文本区同时对输入的数进 行求和运算并求出平均值,也就是说随着输入的变化,另一个文本区不断地更新求和及平均值。 34. 编写一个应用程序,有8个按钮,用户通过按动键盘上的方向键移动这些按钮。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 { public static void main(String args[]){ Win win=new Win();}} class Win extends JFrame implements KeyListener{ JButton b[]=new JButton[8]; int x,y; Win(){ setLayout(new FlowLayout()); for(int i=0;i<8;i++) { b[i]=new JButton(\+i); add(b[i]);} b[i].addKeyListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0);}}); setBounds(10,10,300,300); setVisible(true); validate();} public void keyPressed(KeyEvent e){ int moveDistance=1; Component com=(Component)e.getSource(); int x=(int)com.getBounds().x; int y=(int)com.getBounds().y; { y=y-moveDistance; com.setLocation(x,y); Rectangle comRect=com.getBounds(); for(int k=0;k { Rectangle orthRect=b[k].getBounds(); { y=y+moveDistance; com.setLocation(x,y); break; } } if(y<=0) y=10; } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { y=y+moveDistance; com.setLocation(x,y); Rectangle comRect=com.getBounds(); for(int k=0;k { Rectangle orthRect=b[k].getBounds(); if(comRect.intersects(orthRect)&&com!=b[k]) { y=y-moveDistance; com.setLocation(x,y); break; } } if(y>=300) y=300; } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { x=x-moveDistance; com.setLocation(x,y); Rectangle comRect=com.getBounds(); for(int k=0;k { Rectangle orthRect=b[k].getBounds(); if(comRect.intersects(orthRect)&&com!=b[k]) { x=x+moveDistance; com.setLocation(x,y); break; } } if(e.getKeyCode()==KeyEvent.VK_UP) if(comRect.intersects(orthRect)&&com!=b[k]) if(x<=0) x=0; } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { x=x+moveDistance; com.setLocation(x,y); for(int k=0;k { Rectangle orthRect=b[k].getBounds(); { x=x-moveDistance; com.setLocation(x,y); break;} } if(x>=300) x=300; } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} } Rectangle comRect=com.getBounds(); if(comRect.intersects(orthRect)&&com!=b[k]) 35. 编写一个应用程序,用户可以在一个文本框里输入数字字符,按回车后将数字存入一个文件。当输入的数 字大于1000时,弹出一个有模式的对话框,提示用户数字已经大于1000,是否继续将该数字存入文件(建议使用RandomAcessFile流实现信息的写入)。 import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.io.*; public class ZuoYe2_1 { public static void main(String args[]) { new Dwindow();}} class Dwindow extends JFrame implements ActionListener { JTextField inputNumber; JTextArea show; Dwindow() { inputNumber=new JTextField(22); inputNumber.addActionListener(this); show=new JTextArea(); add(inputNumber,BorderLayout.NORTH); add(show,BorderLayout.CENTER); setBounds(60,60,300,300); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);} public void actionPerformed(ActionEvent e) { boolean boo=false; if(e.getSource()==inputNumber) { String s=inputNumber.getText(); for(int i=0;i if(boo==true) { JOptionPane.showMessageDialog(this,\您输入了非法字符\,\警告对话框\, JOptionPane.WARNING_MESSAGE); inputNumber.setText(null); } else if(boo==false) { int number=Integer.parseInt(s); if(number>=1000){ int n= JOptionPane.showConfirmDialog(this,\确认保存该数字到文件?\,\确认对话框\, JOptionPane.YES_NO_OPTION ); if(n==JOptionPane.YES_OPTION) { saveNumber(number); } else if(n==JOptionPane.NO_OPTION) { inputNumber.setText(null);} } else saveNumber(number);} } } void saveNumber(int n){ RandomAccessFile out=null; try{ out=new RandomAccessFile(\,\); out.seek(out.length()); out.writeUTF(\:\); out.writeInt(n); out.close();} catch(Exception e){} } }
正在阅读:
2010年秋季Java考试编程题总结04-11
景观改造工程 - 图文06-21
数控回转工作台设计 毕业设计05-07
根据山区特色指导孩子写日记04-10
浅谈湖北省某酒店给排水设计07-23
履行“一岗双责”情况汇报(张安华)02-01
大学英语二随堂随练08-06
郑伯克段于(原文及翻译)02-20
循环水管道施工方案11-08
- 小学生造句大全
- 增压泵投资项目可行性研究报告(模板)
- 高中语文人教版粤教版必修1-5全部文言文知识点归纳
- 两学一做专题民主生活会组织生活会批评与自我批评环节个人发言提
- 管理处环境保洁工作操作标准作业指导书
- 2012六一儿童节活动议程 - 图文
- 移树申请报告
- 《贵州省市政工程计价定额》2016定额说明及计算规则
- 计算机长期没有向WSUS报告状态
- 汉语拼音教学策略研究
- 发展西部领先的航空货运枢纽
- 司法所上半年工作总结4篇
- 如何提高银行服务水平
- 发电厂各级人员岗位职责
- 丰田汽车的外部环境分析
- 2017—2018年最新冀教版四年级数学下册《混合运算》教案精品优质
- 中建八局样板策划 - 图文
- 戚安邦《项目管理学》电子书
- 2015年高级项目经理笔记
- 弯桥的设计要点
- 秋季
- 编程
- 总结
- 考试
- 2010
- Java