简体   繁体   English

根据用户输入年份,根据用户输入为2d数组生成随机数

[英]Based on user input years, generate random numbers for a 2d array based on the user input

Before I state my question I have searched the questions that have already been posted and they were of some help to me, but not really what I am looking for. 在陈述问题之前,我已经搜索了已经发布的问题,它们对我有一定帮助,但并不是我真正想要的。

For now, don't worry about the 2d array portion. 现在,不必担心2d数组部分。

I am supposed to create a program that generates random float values based on the user input for a number of years. 我应该创建一个程序,根据多年的用户输入生成随机浮点值。 Let me explain. 让我解释。

First it asks for the user input for years; 首先,它要求用户输入多年。 the user enters a value between 1-80 and the program checks if the entered value is between those two. 用户输入一个介于1-80之间的值,程序检查输入的值是否介于两个之间。 (DONE) (完成)

Then, based on the user input, it will print out each year with a random value between [0.00 and 100.00] like so. 然后,根据用户输入,它将每年打印出[0.00到100.00]之间的随机值,就像这样。 Example; 例; if user enters 3, then the output will display; 如果用户输入3,则将显示输出;

year 1: random values 第一年:随机值

year 2: random values 第二年:随机值

year 3: random values 第三年:随机值

Let's just start with that for now. 让我们从现在开始。 I already have it to where it it asks for user input and I did have it to where it generated random values, but they were not between what I wanted. 我已经将它放置在它要求用户输入的位置,并且确实将它放置在它生成随机值的位置,但是它们不在我想要的范围之内。

Here is my code so far. 到目前为止,这是我的代码。

package name;
import java.util.*;

public class name {

public static void main(String[] args) {

        Random generator = new Random();

        inputCheck();

    }
    public static void inputCheck(){

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the desired number of years: ");
        int years = keyboard.nextInt();
        System.out.println();

        while (years < 1 || years >= 80){
            System.out.print("Please enter a value for years that is greater than 1 and less than 80: ");
            years = keyboard.nextInt();
            System.out.println();
        }
    }
}

Here you are 这个给你

public class Test {

    public static void main(String[] args) {

        Random generator = new Random();

        int years = inputCheck();
        for (int i = 1; i <= years; i++) {
            System.out.println("Year " + i + ": " + generator.nextFloat() * 100);
        }
    }

    public static int inputCheck() {

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the desired number of years: ");
        int years = keyboard.nextInt();
        System.out.println();

        while (years < 1 || years >= 80) {
            System.out.print("Please enter a value for years that is greater than 1 and less than 80: ");
            years = keyboard.nextInt();
            System.out.println();
        }
        return years;
    }
}

I don't see that generator being used, but aside from that, when you do you will only get an integer between zero and the given value(without manipulation of course) wha you want is a float between 0 and 100 with two decimal places, so generate the random number: 我看不到正在使用该生成器,但是除此之外,当您执行此操作时,您只会得到一个介于0和给定值之间的整数(当然,无需进行任何操作),而您想要的是0到100之间的一个浮点数,两个小数位,因此生成随机数:

Randint=(Generator.nextFloat(100.00));

At least that is what I remember 😁. 至少那是我记得的what。 I only gave this since you say you have everything else down pat. 我只是因为你说你还有其他一切而放弃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM