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

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

最佳答案

添加了几行以利用MAX Temp和MIN Temp
现在,如果生成的温度超出给定限制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();
        }
    }


}

关于java - Java,将随机生成的数字保存几次,以备后用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46908895/

10-13 07:54
查看更多