JAVA题库.doc

上传人:s****u 文档编号:12812015 上传时间:2020-05-26 格式:DOC 页数:23 大小:138.54KB
返回 下载 相关 举报
JAVA题库.doc_第1页
第1页 / 共23页
JAVA题库.doc_第2页
第2页 / 共23页
JAVA题库.doc_第3页
第3页 / 共23页
点击查看更多>>
资源描述
异常处理:import java.util.Scanner;public class Exception /* * param args */public static void main(String args) / TODO Auto-generated method stubint a=0;int b=0;char c; int result = 0; try a = Integer.parseInt(args0); b = Integer.parseInt(args2); c = args1.charAt(0); switch(c) case + :result=a+b;break; case - :result=a-b;break; case # :result=a*b;break; case / :result=a/b;break; default: throw new java.lang.Exception(); System.out.println(result=+result); catch(java.lang.ArrayIndexOutOfBoundsException e1) System.out.println(您的参数不够,请增加到3个); catch(java.lang.NumberFormatException e2) System.out.println(转换有问题,无法转换成数字); catch(java.lang.ArithmeticException e3) System.out.println(捕获到数字类异常,除数不能为0); System.out.println(请重新输入除数:); Scanner keyin = new Scanner(System.in); b = keyin.nextInt(); result = a/b; System.out.println(result=+result); catch(java.lang.Exception e4) System.out.println(出现异常,请自行查找何种异常); finally System.out.println(不管是否有异常,总是要执行); 从文件中提取一行,并加行号,输出到另外一个文件:import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;public class ReadFileTest /* * param args */public static void main(String args) / TODO Auto-generated method stubFile file = new File(a.txt);File file2 = new File(b.txt);tryFileWriter in1= new FileWriter(file);BufferedWriter in2= new BufferedWriter(in1);FileReader out1 = new FileReader(file2);BufferedReader out2 = new BufferedReader(out1);String str= null;int i =1;while(str=out2.readLine()!=null)in2.write(i+:+str);in2.newLine();i+;in2.flush();in1.close();in2.close();out1.close();out2.close(); catch(Exception e)System.out.println(IOError!); GUI界面,输入姓名,显示欢迎:import java.awt.Button;import java.awt.Frame;import java.awt.Label;import java.awt.Panel;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ButtonStringTest extends Frame private Label lb;private TextField tf;private Button btn1,btn2;public ButtonStringTest(String s) / TODO Auto-generated constructor stubsuper(s);lb = new Label(姓名:);tf = new TextField(55);btn1 = new Button(确定);btn2 = new Button(清除);btn1.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(tf.getText().equals() | tf.getText().equals(请你输入姓名)tf.setText(请你输入姓名);elsetf.setText(你好, + tf.getText() + ,欢迎你使用java编程!););btn2.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubtf.setText(););Panel p = new Panel();p.add(lb);p.add(tf);p.add(btn1);p.add(btn2);this.add(p);this.setVisible(true);this.setSize(500, 100);this.setLocation(500, 500);this.addWindowListener(new WindowAdapter()Overridepublic void windowClosing(WindowEvent e) / TODO Auto-generated method stubsuper.windowClosing(e);System.exit(0););/* * param args */public static void main(String args) / TODO Auto-generated method stubnew ButtonStringTest(文本框Action事件测试);简单的文本剪辑器:public class MyNotepad extends JFrame implements ActionListener private JTextArea editText;File f = null;JFileChooser fc = new JFileChooser();String menu_str = 文件, 编辑, 文本颜色, 帮助;String file_str = 打开, 保存, 另存为, 退出;String edit_str = 复制, 剪切, 粘贴;String color_str = 红色,绿色, 蓝色, 自定义;String help_str = 帮助 ;JMenu menu = new JMenumenu_str.length;JMenuItem file_item = new JMenuItemfile_str.length;JMenuItem edit_item = new JMenuItemedit_str.length;JMenuItem color_item = new JMenuItemcolor_str.length;JMenuItem help_item = new JMenuItemhelp_str.length;public MyNotepad(String string) / TODO Auto-generated constructor stubsuper(string);editText = new JTextArea();this.add(new JScrollPane(editText);JMenuBar mb = new JMenuBar();for(int i=0; imenu.length; i+) menui = new JMenu(menu_stri);mb.add(menui);for(int i=0; ifile_item.length; i+) file_itemi = new JMenuItem(file_stri);file_itemi.addActionListener(this);menu0.add(file_itemi);for(int i=0; iedit_item.length; i+) edit_itemi = new JMenuItem(edit_stri);edit_itemi.addActionListener(this);menu1.add(edit_itemi);for(int i=0; icolor_item.length; i+) color_itemi = new JMenuItem(color_stri);color_itemi.addActionListener(this);menu2.add(color_itemi);for(int i=0; ihelp_item.length; i+) help_itemi = new JMenuItem(help_stri);help_itemi.addActionListener(this);menu3.add(help_itemi);this.setJMenuBar(mb);this.setBounds(500,300, 600,400);this.setVisible(true);/* * param args */public static void main(String args) / TODO Auto-generated method stubnew MyNotepad(我的简单记事本);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = file_item0) int n = fc.showOpenDialog(this);if(n = JFileChooser.APPROVE_OPTION) f = fc.getSelectedFile();try FileReader fr = new FileReader(f);BufferedReader br = new BufferedReader(fr);String str = null;while( (str = br.readLine() != null)editText.append(str+n);fr.close();br.close(); catch (FileNotFoundException e1) / TODO Auto-generated catch blocke1.printStackTrace(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); else if(e.getSource() = file_item1 ) if(f = null) int n = fc.showSaveDialog(this);if(n = JFileChooser.APPROVE_OPTION) f = fc.getSelectedFile();try FileWriter fw = new FileWriter(f);BufferedWriter bw = new BufferedWriter(fw);bw.write(editText.getText();bw.flush();fw.close();bw.close(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); else if(e.getSource() = file_item2) int n = fc.showSaveDialog(this);if(n = JFileChooser.APPROVE_OPTION) f = fc.getSelectedFile();try FileWriter fw = new FileWriter(f);BufferedWriter bw = new BufferedWriter(fw);bw.write(editText.getText();bw.flush();bw.close();fw.close(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); else if(e.getSource() = file_item3) System.exit(0); else if(e.getSource() = edit_item0) editText.copy(); else if(e.getSource() = edit_item1) editText.cut(); else if(e.getSource() = edit_item2) editText.paste(); else if(e.getSource() = color_item0) editText.setForeground(Color.RED); else if(e.getSource() = color_item1) editText.setForeground(Color.GREEN); else if(e.getSource() = color_item2) editText.setForeground(Color.BLUE); else if(e.getSource() = color_item3) Color newColor = JColorChooser.showDialog(this, 调色板, editText.getForeground();editText.setForeground(newColor); else if(e.getSource() = help_item0) JOptionPane.showMessageDialog(this, 我的简单记事本 V1.0);简单图像查看器:public class PicLookTest extends JFrame implements ActionListener JButton btn1,btn2;int index = 0;JLabel lb;public PicLookTest(String string) / TODO Auto-generated constructor stubsuper(string);JPanel p1 = new JPanel();lb = new JLabel();lb.setIcon(new ImageIcon(pic+(index+1)+.jpg);p1.add(lb);JPanel p2 = new JPanel();btn1 = new JButton(上一张);btn2 = new JButton(下一张);btn1.addActionListener(this);btn2.addActionListener(this);p2.add(btn1); p2.add(btn2);this.add(p1);this.add(p2,BorderLayout.SOUTH);this.setVisible(true);this.setBounds(500, 500, 500, 300);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = btn1) if(index!=0)index = index - 1;elseindex = 6;System.out.println(index);lb.setIcon(new ImageIcon(pic+(index+1)+.jpg); else if(e.getSource() = btn2) if(index!=6)index = index + 1;elseindex = 0;System.out.println(index);lb.setIcon(new ImageIcon(pic+(index+1)+.jpg);/* * param args */public static void main(String args) / TODO Auto-generated method stubnew PicLookTest(picture look test);图片幻灯片播放:import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JOptionPane;public class AnimationTest extends JFrame implements Runnable public static final int PN = 4;Image im;Thread th;int index = 0;int x = 0, y = 0; public AnimationTest(String string) / TODO Auto-generated constructor stubsuper(string);Toolkit toolkit = Toolkit.getDefaultToolkit();im = new ImagePN;for(int i=0; iPN; i+) imi = toolkit.getImage(u + (i+1) + .jpg);th = new Thread(this);th.start();this.setVisible(true);this.setBounds(500, 500, 400, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Overridepublic void run() / TODO Auto-generated method stubwhile(x=400 & y=400) repaint();index+;if(index = PN) index = 0;x = x + 10;y = y + 10;try Thread.sleep(500); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();JOptionPane.showMessageDialog(this, Animation closed);Overridepublic void paint(Graphics g) / TODO Auto-generated method stubsuper.paint(g);g.drawImage(imindex, x, y, this);g.drawLine(0, y, x, y); / 横向g.drawLine(x, 0, x, y); / 竖线/* * param args */public static void main(String args) / TODO Auto-generated method stubnew AnimationTest(animation test);图片动画:import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JOptionPane;public class AnimationTestAnother extends JFrame implements Runnable public static final int PN = 5;Image im;Thread th;int index = 0;int x = 0, y = 0; public AnimationTestAnother(String string) / TODO Auto-generated constructor stubsuper(string);Toolkit toolkit = Toolkit.getDefaultToolkit();im = new ImagePN;for(int i=0; iPN; i+) imi = toolkit.getImage(bird + i + .gif);th = new Thread(this);th.start();this.setVisible(true);this.setBounds(500, 500, 400, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Overridepublic void run() / TODO Auto-generated method stubwhile(x=400 & y=400) repaint();index+;if(index = PN) index = 0;x = x + 10;y = y + 10;try Thread.sleep(100); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();JOptionPane.showMessageDialog(this, Animation closed);Overridepublic void paint(Graphics g) / TODO Auto-generated method stubsuper.paint(g);g.drawImage(imindex, x, y, this);g.drawLine(0, y, x, y); / 横向g.drawLine(x, 0, x, y); / 竖线/* * param args */public static void main(String args) / TODO Auto-generated method stubnew AnimationTestAnother(animation test);计算三角形面积:package ch01test1;public class TriangelTest /* * param args */public static void main(String args) / TODO Auto-generated method stubTriangle tri = new Triangle(3,4,5); / 创建对象double a = tri.geta(); / 获得3条边参数double b = tri.getb();double c = tri.getc();double area;double p = (a + b + c) / 2;area = Math.sqrt(p*(p-a)*(p-b)*(p-c); / 海伦公式System.out.println(三角形面积: + area);/ 输出结果/ 三角形面积:6.0小球反弹:package ch02test13;public class BallTest public static void main(String args) / TODO Auto-generated method stubint i = 1; / 第1次弹起是50m, 经过100mdouble s = 100;double h = 50;do i+; / 第2次弹起是25m,经过200ms += 2 * h; / 2*h为上次弹起走过的距离, s为这次落地已经经过的总距离h /= 2; / 这次弹起的高度计算 while(i10);System.out.println(i: + i); / 第10次落地System.out.println(s: + s); / 经过多少mSystem.out.println(h: + h); / 弹起多少m/ 输出结果/i: 10/s: 299.609375/h: 0.09765625最小公倍数丶最大公因数:package ch04test2;public class Father public int f(int a, int b) int num = Math.max(a, b);int den = Math.min(a, b);int rem;/ 最大公约数, 辗转相处法/ 例1:12 % 8 = 4/ 8 % 4 = 0, 4就是12,8的最大公约数/ 例2:30 % 16 = 14/ 16 % 14 = 2/ 14 % 2 = 0, 2就是30,16的最大公约数while(true) rem = num % den;if(rem = 0) break;num = den;den = rem;return den; / 返回最大公约数package ch04test2;public class Son extends Father / 继承Fatherpublic int f(int a, int b) int mul = a * b;/ 用super来调用父类方法 int gcm = super.f(a, b); / 最大公约数 greatest common measure GCM , 最小公倍数 lowest common multiple LCM int lcm = mul / gcm; / 最小公倍数 = 乘积 / 最大公约数 return lcm; / 返回最小公倍数
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!