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();
        }
    }

    运行截图

  • 相关阅读:
    LeetCode 977 有序数组的平方
    LeetCode 24 两两交换链表中的节点
    LeetCode 416 分割等和子集
    LeetCode 142 环形链表II
    LeetCode 106 从中序与后序遍历序列构造二叉树
    LeetCode 637 二叉树的层平均值
    LeetCode 117 填充每个节点的下一个右侧节点
    LeetCode 75 颜色分类
    redhat 7.4 挂载ntfs格式的u盘并且使用
    redhat 查看CPU frequency scaling(CPU频率缩放)
  • 原文地址:https://www.cnblogs.com/TI-NA/p/12081425.html
Copyright © 2011-2022 走看看