Hello! Tell me by code, I can not figure out where it is wrong, sometimes in my program it incorrectly shows the highest number of a three-digit number. The program is composed on the instructions:

Create a program that displays a randomly generated three-digit natural number and its largest digit.

public class Test { public static void main(String args []) { final int min = 100; final int max = 999; int first; int second; int third; int value_max; int value = (int) (min + Math.random() * (max - min)) ; if(value > min && value < max) { first = (int) Math.floor(value / 100); second = (int) Math.floor((value - first * 100) / 10); third = (int) Math.floor(value - first * 100 - second * 10); System.out.println("Трехзначное число = " + value); if(first > second) { value_max = first; if(value_max < third) { value_max = third; } else { value_max = second; } } else { value_max = second; } if(value_max < third) value_max = third; System.out.println("Первое число = " + first); System.out.println("Второе число = " + second); System.out.println("Третье число = " + third); System.out.println("Максимальное число = " + value_max); } } } 

And there is another question: by what formula can one calculate also the largest digit of four and five-digit numbers, etc.

    3 answers 3

     public class test { public static void main(String[] args) { final int min = 100; final int max = 999; int value = (int) (min + Math.random() * (max - min)); int value_max = 0; int digit; System.out.println(value); do { digit = value%10; if ( digit > value_max ) value_max=digit; value = (value - digit)/10; } while (value > 0); System.out.println(value_max); } 

    }

    • It works for any number of numbers, the only thing you can not take away the value of digit if you are sure that the value will be cast to type int - Dex
    • not sure by tradition - renegator
    • It is not necessary to take away precisely (neither in Si, nor in Java) - avp
    • The phrase "do not" can be understood as "wrong." From the point of view of the clarity of the algorithm, it will be better as I wrote. Because the algorithm is as follows: decimal number is decomposed (abc) = (a * 10 + b) * 10 + c from where c = (abc)% cb = ((abc) - c)% 10 a = ((ab) - b) % 10 etc. - renegator September

    In general, the logic for numbers of any significance is as follows. Suppose there are 3 numbers, we take the first, and we believe that it is maximum, compare it with the second, if 1 is greater, then compare with the third, if not, then consider the second maximum and compare with the third. Again, if the second is greater, then it is the maximum of all, if less, then the maximum is the third.

    In Java, I am not strong, but in PHP this is done by an elementary single min () function, and if without using it but following the logic described above, it is done like this:

     <?php function min_ch($ch){ $min = $ch[0]; foreach ($ch as $key=>$val){ if ($min < $val) { continue; } else { $min = $val; } } echo $min; } ?> 

    In this function, changing the sign of <to sign> you can find, respectively, the minimum and maximum numbers in the original array.

    • Demos, tell me, please, if I want to find the smallest digit of any number, then by what logic does it work, describe? - turtles
    • Well, just as I wrote, only with accuracy and vice versa. We consider the 1st minimum, compare with the second, if the 1st is less, compare with the third, if more, then consider the second as the minimum and compare with the third. If the 2nd is greater, then the minimum of all is the 3rd, if less, then the second is the minimum. - DemoS

    You have not considered the option when the numbers are equal.
    Better this way:

     value_max = first; if(value_max < second) { value_max = second; } if (value_max < third) { value_max = third; }