Java,保存随机生成的数字几次以后再使用它们

时间:2017-10-24 11:05:06

标签: java

我正在制作一个程序,它在一个循环中多次生成一个范围(0到100)内的数字。 我需要在每次生成时保存这些数字,以使程序输出一些内容并将它们放在类似于它的东西上。

public static void main(String args[]) {
    ArrayList<Integer> temps = new ArrayList(); //will contain all temperatures
    Scanner scan = new Scanner(System.in);
    System.out.println("Set the ammount of minutes that the program lasts");
    int minutes = scan.nextInt();
    System.out.println("Enter max temperature");
    int maxT = scan.nextInt();
    System.out.println("Enter min temperature");
    int minT = scan.nextInt();

    int count = 0;

    while(minutes > 0) {
        minutes--;
        pause();
        int newTemp = (ThreadLocalRandom.current().nextInt(0, 100)); //Generate random number
        temps.add(newTemp); // random number to arraylist
        if(newTemp > maxT) //if new temperature is above the limits
            count++;
        if(count>=3) { //if the temperature has gone above the limits more than 3 times
            System.out.println("ALARMA!!!");
            break; //break loop
        }
        else if(newTemp < maxT && newTemp > minT) //if new temperature is inside the limits
        {
            System.out.println("REVISAR SISTEMES");
            break; //break loop
        }
        else if (newTemp < minT)
        {
            System.out.println("EMERGENCIA ACABADA. Tot correcte");
            break; //break loop
        }
    }
    System.out.println(temps);
}

public static void pause() {
    try        
    {
        Thread.sleep(1000);
    } 
    catch(InterruptedException ex) 
    {
        Thread.currentThread().interrupt();
    }
}

2 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西

import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class myRan {
    public static void main(String args[]) {
        ArrayList<Integer> rands = new ArrayList();
        Scanner scan = new Scanner(System.in);
        System.out.println("Set the ammount of minutes that the program lasts");
        int minutes = scan.nextInt();


        while(minutes > 0) {
            pause();
            rands.add(ThreadLocalRandom.current().nextInt(0, 100));
            minutes--;
        }
        System.out.println(rands);

    }

    public static void pause() {
        try        
        {
            Thread.sleep(1000);
        } 
        catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }
    }


}

要使其每分钟生成一个随机数,请更改 Thread.sleep(1000); Thread.sleep(60000);

请记住,它会运行指定的分钟数。

答案 1 :(得分:0)

添加了几行来使用MAX Temp&amp;最低温度 现在,如果生成的温度超出给定限制超过3次,程序将退出 您可以在该代码块中执行任何操作,而不是退出(中断)

import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class myRan {
    public static void main(String args[]) {
        ArrayList<Integer> temps = new ArrayList(); //will contain all temperatures
        Scanner scan = new Scanner(System.in);
        System.out.println("Set the ammount of minutes that the program lasts");
        int minutes = scan.nextInt();
        System.out.println("Enter max temperature");
        int maxT = scan.nextInt();
        System.out.println("Enter min temperature");
        int minT = scan.nextInt();

        int count = 0;

        while(minutes > 0) {
            minutes--;
            pause();
            int newTemp = (ThreadLocalRandom.current().nextInt(0, 100)); //Generate random number
            temps.add(newTemp); // random number to arraylist
            if(newTemp > maxT || newTemp < minT) //if new temperature is out of limits
                count++;
            if(count>3) { //if the temperature has gone out of limits more than 3 times
                System.out.println("ALERT! Issue at time :" + minutes);
                break; //add your code for alert here
            }
        }
        System.out.println(temps);

    }

    public static void pause() {
        try        
        {
            Thread.sleep(1000);
        } 
        catch(InterruptedException ex) 
        {
            Thread.currentThread().interrupt();
        }
    }


}