如何让我的代码选择从X到Y的数字? [英] How do I make my code choose a number from X to Y?

查看:81
本文介绍了如何让我的代码选择从X到Y的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Random;

import java.lang.Math;



public class Exercise_7_8e {



public static void main(String [] args){

// TODO自动生成方法存根



float [] Array1 = new float [99];



float max = Array1 [0];

float min = Array1 [0];



for(int i = 0; i< Array1.length; i ++){

Array1 [i] =(int)(Math.random()* 200 + 6);

if(Array1 [i]> max){

max = Array1 [i];

}否则if(Array1 [i]< min)

min = Array1 [i];



}



System.out.println(最大值是+最大值);

System.out.println(最小值是+ min;



}



}



输出:



最大值是205.0

最小值是0.0



我尝试过:



所以Math.random()的格式化应该是Math.random()* max + min正确吗?但是,在我的代码中,输出将始终返回0作为min。我如何解决这个问题?

import java.util.Random;
import java.lang.Math;

public class Exercise_7_8e {

public static void main(String[] args) {
// TODO Auto-generated method stub

float[] Array1 = new float[99];

float max = Array1[0];
float min = Array1[0];

for (int i = 0; i < Array1.length; i++) {
Array1[i] = (int) (Math.random() * 200 + 6);
if (Array1[i] > max) {
max = Array1[i];
} else if (Array1[i] < min)
min = Array1[i];

}

System.out.println("The largest value is " + max);
System.out.println("the smallest value is " + min);

}

}

Output:

The largest valus is 205.0
the smallest value is 0.0

What I have tried:

So the formatting of Math.random() is supposedly Math.random()*max + min correct? However, in my code the output will always return 0 as the min. How do i fix this?

推荐答案

float[] Array1 = new float[99];<br />
float min = Array1[0];



从未初始化的数组中分配 min 。到目前为止,我知道Java它默认初始化为'0'。我的个人观点:总是明确地初始化变量而不依赖于编译器/链接器行为。



所以 Math.random()* 200 + 6 永远不会< min意味着< 0(来自默认初始化)。



尝试 - 并考虑它的含义:

float min = 9999999.9 或者更好的max(浮动)如果它确实存在于Java中(我认为它是Float.MAX_VALUE)



我希望它帮助。


You assign min from a not initialiezed Array. So far I know Java it does initialize by Default to '0'. My personal opinion: Always initialize variables explicitely and don't rely on Compiler/Linker behaviours.

So Math.random() * 200 + 6 will never be < min which means < 0 (from the Default init).

Try - and think about what it means:
float min = 9999999.9 or much better to max(float) if it does exists in Java (I think it is Float.MAX_VALUE)

I hope it helps.


引用:

因此Math.random()的格式化应该是Math.random( )* max + min是否正确?

So the formatting of Math.random() is supposedly Math.random()*max + min correct?



简单阅读文档分析会告诉你答案。




A simple reading of documentation an analyze would tell you the answer.

0 <= Math.random() < 1
0 <= Math.random() * 200 < 200
6 <= Math.random() * 200 + 6 < 206
6 <= (int) (Math.random() * 200 + 6) < 206
6 <= (int) (Math.random() * 200 + 6) <= 205




Quote:

但是,在我的代码中,输出将始终返回0作为min。我如何解决这个问题?

However, in my code the output will always return 0 as the min. How do i fix this?



问题是你初始化 min 0,这已经低于你的任何随机值得到。



请注意,您不需要将每个随机值存储在数组中,因为您不会重复使用它们。


The problem is that you initialize min with 0 which is already lower than any random value that you get.

Note that you don't need to store each random values in an array because you don't reuse them.


这篇关于如何让我的代码选择从X到Y的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆