zoukankan      html  css  js  c++  java
  • 第16周作业


    题目1:编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。

    题目2:编写一个应用程序,利用Java多线程机制,实现猜数字游戏(随机数范围0~100之间的整数)。

    1、

    package ccut.edu.cn;
    /**
     * 实现时间的同步输出显示
     */
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class Time implements Runnable{
        Date date;
        public void run() {
            while(true){
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
                String time = sdf.format(date);
                System.out.println(time);
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    System.out.println(e);
                }
            }        
        }
    }
    import java.util.Scanner;
    public class CurrentTime {
        public static void main(String[] args) {
            Thread t=new Thread(new Time() );
            t.start();
        }
    }

    运行截图

    2、

    package ccut.edu.cn;
    /**
     * 实现猜数字游戏
     */
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    public class Thread implements Runnable{//实现线程接口
        Scanner s=new Scanner(System.in);
        int flag;//中间判断变量
        public void run() {
            while(true){
                System.out.println("输入数字");
                int c=s.nextInt();
                int a=(int)(Math.random()*100);//生成随机数
                if(c>=a){
                    System.out.println("猜大了");
                }else if(c<a)
                    System.out.println("猜小了");
                else System.out.println("猜对了");
             //   System.out.println(a);
                try{
                    Thread.sleep(1000);    //暂停            
                }catch(InterruptedException e){
                    System.out.println(e);
                }      
            } 
        }
    }
    package ccut.edu.cn;
    import java.util.Scanner;
    public class CurrentTime0 {
        public static void main(String[] args) {
            Thread t=new Thread(new thread() );
            t.start();
        }
    }

    运行截图

  • 相关阅读:
    JNDI使用小记
    Python基础之字符串
    openstack装B之路http协议
    openstack装B之路keystone
    Python基础之列表
    openstack装B之路Keystone部署以及操作
    openstack装B之路网关协议
    纵表与横表互转的SQL
    Asp.Net请求管道中的19个事件
    jquery ajax post get
  • 原文地址:https://www.cnblogs.com/TI-NA/p/12081425.html
Copyright © 2011-2022 走看看