【问题标题】:Finding the multiple in while loop在while循环中查找倍数
【发布时间】:2020-09-26 16:23:31
【问题描述】:

返回类型为int,入参也是int

我在while 循环中生成随机整数,我正在检查int 是否是输入参数int 的倍数,并返回获得匹配的尝试次数。

我想在while 循环中而不是在for 循环中进行。

public int findMul(int v){
    double random = Math.random();
    int numoftries = 0;

    while(v % random == 0){
        numoftries++;
    }
    return numoftries;
}

由于某种原因,当我在 main 方法中调用它然后执行它时,无论是什么数字,它总是显示数字 0,我不知道如何解决。

【问题讨论】:

  • This: double random = Math.random(); 返回一个双精度数,因此您的 int 永远不会等于它。进行一些调试,并打印出数字以供您自己查看。您需要转换为 int。
  • 您可能还想在循环中生成一个新的随机值。如果曾经进入过循环,则您当前会永远循环!
  • 以上两个cmets都是正确的。我要补充一点,循环的条件需要为!=0,因为当随机生成的数字不是v的倍数时,您想增加尝试次数。

标签: java random while-loop


【解决方案1】:

我将建议另一种方法。

生成下一个数字时,应该只使用rand.nextInt(v),因为它保证生成的数字小于v。生成大于v 的数字永远不会成为一个因素。

public int findMul(int v) {
    Random rand = new Random();
    int numoftries = 0;
    int random = rand.nextInt(v);
    while (true) {
        if(random==0) {
            numoftries++;
            random=rand.nextInt(v);
            continue;
        }
        if(v%random==0) {
            numoftries++;
            break;
        }else {
            numoftries++;
            random=rand.nextInt(v);
        }

    }
    return numoftries;
}

你的循环最初返回0,因为条件是v%random==0,所以如果第一个生成的数字不是一个因素,它会立即返回numoftries

您还需要每次在循环中生成一个新数字,因为如果生成的数字不是一个因素,那将是一个无限循环。

你可以像这样测试代码:

import java.util.Random;

public class RandomFactor {
    public int findMul(int v) {
        Random rand = new Random();
        int numoftries = 0;
        int random = rand.nextInt(v);
        while (true) {
        if(random==0) {
            numoftries++;
            random=rand.nextInt(v);
            continue;
        }
            if(v%random==0) {
                numoftries++;
                break;
            }else {
                numoftries++;
                random=rand.nextInt(v);
            }

        }
        return numoftries;
    }
    public static void main(String[] args) {
        RandomFactor x = new RandomFactor();
        System.out.println(x.findMul(1000));//1000 is the test case
    }
}

有时,如果数字太小,它产生0的可能性更高,这会抛出一个ArithmeticException。所以我们添加了一个 if 语句来防止这种情况发生。

如果你想用nextInt()达到同样的效果:

import java.util.Random;
import java.util.Scanner;

public class RandomFactor {
    public int findMul(int v) {
        Random rand = new Random();
        int numoftries = 0;
        int random = rand.nextInt(v);
        while (true) {
            if(random==0) {
                numoftries++;
                continue;
            }
            if(v%random==0) {
                numoftries++;
                break;
            }else {
                numoftries++;
                random=rand.nextInt(v);
            }

        }
        return numoftries;
    }
    public static void main(String[] args) {
        RandomFactor x = new RandomFactor();
        Scanner input = new Scanner(System.in);
        while(true) {
            try {
                System.out.print("\nEnter your guess: ");
                int guess = input.nextInt();
                System.out.println(x.findMul(guess));
            }catch(NumberFormatException e) {
                break;
            }
            
        }
        
    }
}

如果必须使用 Math.random

import java.util.Scanner;

public class RandomFactor {
    public static int digits(int number) {
        String current = String.valueOf(number);
        return current.length();
    }
    public static int newrandom(int v) {
        return (int)(Math.random()*(digits(v)*10));
    }
    public int findMul(int v) {
        int random = newrandom(v);
        int numoftries = 0;
        while (true) {
            if(random==0) {
                numoftries++;
                random = newrandom(v);
                continue;
            }
            if(v%random==0) {
                numoftries++;
                break;
            }else {
                numoftries++;
                random=newrandom(v);
            }

        }
        return numoftries;
    }
    public static void main(String[] args) {
        RandomFactor x = new RandomFactor();
        Scanner input = new Scanner(System.in);
        while(true) {
            try {
                System.out.print("\nEnter your guess: ");
                int guess = input.nextInt();
                System.out.println(x.findMul(guess));
            }catch(NumberFormatException e) {
                break;
            }
            
        }
        
    }
}

说明

因为 Math.random 返回一个双精度数,我们需要生成一个与猜测的位数相同的数字,然后转换为一个整数,以便正确比较。我们通过将其转换为字符串(方法数字)然后将结果乘以 10 来做到这一点,因为 Math.random 返回一个介于 0 和 1 之间的数字。但是,请记住,这不是有效的,因为数字的位数越多,它会做出更多的猜测。例如,如果我们猜测 5,它将从 0-9 猜测。如果我们猜测 10,它会从 10 到 100 猜测。

编辑:修复了一个导致它永远循环的致命错误,因为如果随机数是0,它不会重新生成数字。

【讨论】:

  • 没有随机包和 .nextInt() 怎么办
  • @romebobnomrio 我不确定您所说的“随机包”是什么意思,但我可以为您准备一个带有nextInt() 的示例。
  • 因为我想使用 Math.random() 而不是导入包来为我完成工作
  • @romebobnomrio 你必须这样做吗?与使用 Random 相比,Math.random 只是一个不必要的蠕虫罐
  • 是的,我必须使用 Math.random() 方法而不是随机包
猜你喜欢
  • 2018-06-06
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 2020-04-06
  • 2013-03-11
  • 1970-01-01
  • 2016-04-12
  • 2016-11-27
相关资源
最近更新 更多