A1
package com.lenovo.s0620review;
/**
* 求解 1+2!+3!+...+10!
* @author Shinelon
*
*/
class A1 {
public static void main(String[] args) {
float sum = 0.0f, count = 1.0f;
for (int i = 1; i <= 10; ++i) {
count = 1.0f;
for (int j = 1; j <= i; ++j) {
count *= j;
}
sum += count;
}
System.out.println("1+2!+3!+...+10!的值为:" + sum);
}
}
A2
package com.lenovo.s0620review;
/**
* 运行结果为在定义的窗口中显示【确定】、【取消】按钮
*/
import javax.swing.*;
public class A2 extends JFrame {
A2() {
super("程序调试");
JButton jbtn1 = new JButton("确定");
JButton jbtn2 = new JButton("取消");
this.add(jbtn1,java.awt.BorderLayout.NORTH);
this.add(jbtn2,java.awt.BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
public static void main(String args[]) {
new A2();
}
}
A3
package com.lenovo.s0620review;
/**
* 能正确捕获到异常,并处理
* @author Shinelon
*
*/
public class A3 {
public static void main(String[] args) {
try {
int num[] = new int[10];
System.out.println("num[10] is " + num[10]);
} catch (ArithmeticException ex) {
System.out.println("ArithmeticException");
} catch (RuntimeException ex) {
System.out.println("RuntimeException");
}catch (Exception ex) {
System.out.println("Exception");
}
}
}
A4
package com.lenovo.s0620review;
/**
* 求Fibonacci数列:1,1,2,3,5,8,…的前20个数
* 并且输出时按照每行4个数输出
* @author Shinelon
*
*/
public class A4 {
public static void main(String[] args) {
System.out.println("** 菲波拉挈数列的前20个数为:**");
long f1 = 1, f2 = 1;
for (int i = 1; i <=10; i++) {
System.out.print(f1 + " " + f2 + " ");
if (i % 2 == 0) {
System.out.println();
}
f1 = f1 + f2;
f2 = f1 + f2;
}
}
}
A5
package com.lenovo.s0620review;
/**
* 实现一个Person的类和它的子类Student。
* Person类只定义了一个属性name(姓名),以及有参构造方法,通过构造方法可以对name 进行初始化。
* 子类Student有新增加属性stuID(学号)。定义一个学生对象(张三, 20080601),输出他的姓名和学号。
* @author Shinelon
*
*/
class People {
String name;
People(String name){
this.name = name;
}
}
class Student extends People {
String stuID;
Student(String name,String stuID){
super(name);
this.stuID = stuID;
}
}
public class A5 {
public static void main(String arg[]) {
Student stu = new Student("张三", "0001");
System.out.println(stu.name +" "+stu.stuID); ;
}
}
A6
package com.lenovo.s0620review;
/**
* 用户对文本域中显示的内容进行判断,
* 进行相应的选择后,文本框中显示答案的正确与否
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class A6 extends JFrame implements ActionListener {
JRadioButton rad1 = new JRadioButton("说法正确", false);
JRadioButton rad2 = new JRadioButton("说法错误", false);
JTextArea ta = new JTextArea(2, 10);
JTextField tf = new JTextField(4);
JLabel lb = new JLabel("你的选择:");
JPanel jp = new JPanel();
String text = "BoxLayout是由Swing 提供的布局管理器,功能上同GridBagLayout一样强大,而且更加易用。";
public A6() {
this.setLayout(new FlowLayout());
ta.setText(text);
this.add(ta);
rad1.addActionListener(this);
rad2.addActionListener(this);
this.add(rad1);
this.add(rad2);
this.add(lb);
this.add(tf);
this.setSize(520, 150);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == rad1) {
rad2.setEnabled(false);
tf.setText("正确!");
} else {
rad2.setEnabled(false);
tf.setText("错误!");
}
}
public static void main(String args[]) {
new A6();
}
}
A7
package com.lenovo.s0620review;
/**
* 编写设计一个包含菜单的简易计算器界面
* 下拉菜单中的菜单项可自行设计。
*/
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class A7 extends JFrame {
public A7() {
super("计算器");
JPanel p11 = new JPanel();
JTextField viewfield = new JTextField("0");
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton bt0 = new JButton("0");
JButton btx2 = new JButton(".");
JButton btx3 = new JButton("/");
JButton btx4 = new JButton("*");
JButton btx5 = new JButton("-");
JButton btx6 = new JButton("+");
JButton btx10 = new JButton("=");
p11.setLayout(new GridLayout(4, 4, 4, 4));
p11.add(bt7);
p11.add(bt8);
p11.add(bt9);
p11.add(btx3);
p11.add(bt4);
p11.add(bt5);
p11.add(bt6);
p11.add(btx4);
p11.add(bt1);
p11.add(bt2);
p11.add(bt3);
p11.add(btx5);
p11.add(bt0);
p11.add(btx2);
p11.add(btx6);
p11.add(btx10);
this.setLayout(new BorderLayout());
this.getContentPane().add("North", viewfield);
this.getContentPane().add("South", p11);
this.pack();
this.setLocation(600, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new A7();
}
}