Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数学随机性在代码中工作不正常 封装阵列; 导入java.util.Scanner; 公开课抽奖{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); System.out.println(“输入参与方的数量”); int no=scan.nextInt(); 字符串参与者[]=新字符串[否]; System.out.println(“请输入他们的姓名并检查随机获胜者”); 对于(int i=0;i_Java_Eclipse - Fatal编程技术网

数学随机性在代码中工作不正常 封装阵列; 导入java.util.Scanner; 公开课抽奖{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); System.out.println(“输入参与方的数量”); int no=scan.nextInt(); 字符串参与者[]=新字符串[否]; System.out.println(“请输入他们的姓名并检查随机获胜者”); 对于(int i=0;i

数学随机性在代码中工作不正常 封装阵列; 导入java.util.Scanner; 公开课抽奖{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); System.out.println(“输入参与方的数量”); int no=scan.nextInt(); 字符串参与者[]=新字符串[否]; System.out.println(“请输入他们的姓名并检查随机获胜者”); 对于(int i=0;i,java,eclipse,Java,Eclipse,您正在将Math.random()的结果转换为int,然后将其乘以no,这将导致0,因为Math.random()

您正在将
Math.random()
的结果转换为
int
,然后将其乘以
no
,这将导致
0
,因为
Math.random()<1.0

改变

package arrays;
import java.util.Scanner;

public class Raffle {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the number of partcipants ");
        int no=scan.nextInt();
        String participants[]=new String[no];
        System.out.println("Please enter their names and check out the random winner");
        for(int i=0;i<no;i++) {
            participants[i]=scan.next();
        }
        int rand=(int)Math.random()*no;
        System.out.println(rand);
        System.out.println("The winner of the raffle is "+participants[rand]);
    }
}


您正在将
Math.random()
的结果强制转换为
int
,然后再乘以
no
,这将导致
0
,因为
Math.random()<1.0

改变

package arrays;
import java.util.Scanner;

public class Raffle {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the number of partcipants ");
        int no=scan.nextInt();
        String participants[]=new String[no];
        System.out.println("Please enter their names and check out the random winner");
        for(int i=0;i<no;i++) {
            participants[i]=scan.next();
        }
        int rand=(int)Math.random()*no;
        System.out.println(rand);
        System.out.println("The winner of the raffle is "+participants[rand]);
    }
}


在0.0(包含)和1.0(独占)之间强制转换一个double将始终向下舍入为零。Math.random()值应首先与某个因子相乘,然后再强制转换为int,以获得介于零(包含)和乘法因子(独占)之间的值。另一种可能的解决方案是使用nextInt()java.util.Random的方法。

在0.0(包含)和1.0(独占)之间强制转换双精度浮点值将始终向下舍入为零。Math.Random()值应首先与某个因子相乘,然后再强制转换为int,以获得介于零(包含)和乘法因子(独占)之间的值。另一种可能的解决方案是使用java.util.Random的nextInt()方法。

您假设ide的版本在这里甚至是远程相关的事实说明了很多。您说的“结果,而不是随机值”是什么意思是的,我知道这没有多大关系,只是添加了额外的信息。事实上,你认为你的ide版本在这里甚至是远程相关的,这说明了很多。你说的“结果,而不是随机值”是什么意思?是的,我知道这没有多大关系,只是添加了额外的信息
int rand=(int)(Math.random()*no);