zoukankan      html  css  js  c++  java
  • java打印随机函数

    一 ,打印1-10的随机函数

    public static void randomprint(){
          for (int i=0;i<100;i++){          //打印一百次
                System.out.println((int)(1+Math.random()*10));
          }
    }

    (int)(1+Math.random()*10)      int:返回类型     1:最小值       10最大值

    二,在给的的范围内随机输出一个数字

    import java.util.Random;
     
    public class RandomNumber {
     
          public static void main(String[] args) {
     
               int []numbers = {1,2,5,4,3,42,71,25,6};
               Random random = new Random();
               int index = random.nextInt(numbers.length);
               System.out.println(numbers[index]);        
        }
    }

    三,让程序运行一段时间后停止运行(运行30秒)

    public static void main(String[] args) {
        long begain = System.currentTimeMillis();//开始系统时间
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long CheckTime = System.currentTimeMillis(); //判断时间
        while(true){
            System.out.println(CheckTime-begain);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            CheckTime = System.currentTimeMillis(); 
            if((CheckTime-begain)>=(30*1000)){//判断时候到30秒
                System.out.println("30秒后结束");
                break;
            }
        }
        }

    简化之后的代码:

    public static void main(String[] args) {
        long begain = System.currentTimeMillis();//开始系统时间
        long CheckTime = System.currentTimeMillis(); //判断时间
    
        while(true){
            //此处写需要运行的程序!!!!!
    CheckTime = System.currentTimeMillis(); if((CheckTime-begain)>=(30*1000)){//判断时候到30秒 System.out.println("30秒后结束"); break; } } }
  • 相关阅读:
    软件概要设计
    项目文件-搭建工程
    select标签中设置只读几种解决方案
    PHP ob缓冲区函数的使用
    laravel笔记
    ubuntu系统更新命令
    RBAC权限控制系统
    laravel 数据库获取值的常用方法
    php中获取数据 php://input、$_POST与$GLOBALS['HTTP_RAW_POST_DATA']三者的区别
    php 文件上传 $_FILES 错误码
  • 原文地址:https://www.cnblogs.com/111testing/p/6681787.html
Copyright © 2011-2022 走看看