dorishome吧 关注:10贴子:48
  • 5回复贴,共1
几个书本上的代码,


1楼2014-04-15 12:59回复
    import java.awt.*;
    import java.awt.event.*;
    public class MyLabel extends Label implements Runnable{
    int value;
    boolean stop = false;
    public MyLabel(){
    super("number");
    value=0;
    }
    public void run(){
    for(;;){
    value = (int)(Math.random()*10);
    setText(Integer.toHexString(value));
    try{
    Thread.sleep(500);
    }catch(InterruptedException e){
    }
    if(stop){
    break;
    }
    }
    }
    }
    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class ChooseNumber extends Frame{
    MyLabel x[]=new MyLabel[6];
    Button control;
    public ChooseNumber(String title){
    super(title);
    Panel disp = new Panel();
    disp.setLayout(new FlowLayout());
    for(int i=0;i<6;i++){
    x[i]= new MyLabel();
    disp.add(x[i]);
    new Thread(x[i]).start();
    }
    add("Center",disp);
    control = new Button("停止");
    add("South",control);
    pack();
    setVisible(true);
    control.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    for(int i=0;i<6;i++){
    x[i].stop =true;
    }
    }
    });
    }
    public static void main(String[] args){
    new ChooseNumber("选号程序");
    }
    }


    2楼2014-04-15 13:00
    回复
      import java.sql.*;
      public class DBConnection {
      public static void main(String args[]){
      String url = "jdbc:mysql://localhost:3306/mydb";
      Connection con = null;
      Statement stat = null;
      ResultSet rs = null;
      try{
      Class.forName("com.mysql.jdbc.Driver");
      System.out.println("驱动程序已经装载");
      System.out.println("即将连接数据库");
      }catch(Exception ex){
      ex.printStackTrace();
      return;
      }
      try{
      con = DriverManager.getConnection(url,"root","mysql");
      stat = con.createStatement();
      rs = stat.executeQuery("SELECT*FROM student");
      System.out.println("查询结果为:");
      while(rs.next()){
      System.out.println(rs.getString(1)+"\t"+rs.getString(2));
      }
      rs.close();
      stat.close();
      con.close();
      }catch(SQLException ex){
      while (ex !=null){
      ex.printStackTrace();
      ex = ex.getNextException();
      }
      }
      }
      }


      4楼2014-04-15 13:01
      回复


        5楼2014-04-15 13:01
        回复
          package MySecondPackage;
          import java.util.Scanner;
          public class AnotherBoringLesson {
          /**
          * 主函数,程序的入口
          */
          public static void main(String[] args) {
          // 实例化Scanner对象
          Scanner writeIn = new Scanner(System.in);
          // 创建一个值,控制菜单输入
          int flag = 0;
          // 创建菜单
          do {
          System.out.println("请输入你的选择:\n\t"
          + "1.求最大公约数\n\t2.求最小公倍数\n\t0.退出使用");
          flag = writeIn.nextInt();
          // 实例化两个数
          int i = 0, j = 0;
          if (flag == 1 || flag == 2) {
          System.out.println("请输入两个数:");
          i = writeIn.nextInt();
          j = writeIn.nextInt();
          }
          switch (flag) {
          case 1:
          System.out.println("最大公约数是 : " + maxCommonDivisor(i, j) + "\n");
          break;
          case 2:
          System.out
          .println("最小公倍数是 : " + minCommonMultiple(i, j) + "\n");
          break;
          case 0:
          System.out.println("谢谢使用!\n");
          break;
          default:
          System.out.println("输入有误,请重新输入!\n");
          break;
          }
          } while (flag != 0);
          }
          /**
          * 方法:得到两个数中较大的那个数
          */
          public static int getMax(int i, int j) {
          // 三元关系表达式
          return i > j ? i : j;
          }
          /**
          * 方法:得到两个数中较小的那个数
          */
          public static int getMin(int i, int j) {
          // 三元关系表达式
          return i < j ? i : j;
          }
          /**
          * 方法:得到最小公倍数
          */
          public static int minCommonMultiple(int i, int j) {
          // 创建一个新值,用于返回所得结果
          int k = getMax(i, j);
          // 控制最小公倍数的取得
          if (i == j) {
          return k;
          } else {
          while (k % i != 0 || k % j != 0) {
          k++;
          }
          }
          return k;
          }
          /**
          * 方法:得到最大公约数
          */
          public static int maxCommonDivisor(int i, int j) {
          // 创建一个新值,用于返回所得结果
          int k = getMin(i, j);
          // 控制最大公约数的取得
          if (i == j) {
          return k;
          } else {
          while (i % k != 0 || j % k != 0) {
          k--;
          }
          }
          return k;
          }
          }


          6楼2014-04-15 13:01
          回复
            赞一个


            IP属地:贵州来自Android客户端7楼2014-04-16 01:06
            回复