I want to display the number of appearances of each of the 100 0-9 integers displayed in random numbers in asterisk (*).

Asked 1 years ago, Updated 1 years ago, 62 views

source code

 int[] frequencies = new int[10];
int[] ar1 = new int[100];

for(inti=0;i<ar1.length;i++){
    ar1[i] = land.nextInt(10);
    System.out.println(ar1[i]);
    int array=ar1[i];
    frequencies [array]++;

I'd like to use the method to display the number of times each integer from 0 to 9 appears in *, but I don't know what to do...

example

6,8,5,3,2,5,7,5,6,9,3,0,6,6,6,6,3,4,3,8,6,, 
3, 3, 6, 0, 6, 5, 6, 1, 7, 3, 3, 6, 4, 1, 6, 1, 0, 7, 7, 2, 
2, 6, 5, 1, 1, 8, 4, 4, 6, 4, 4, 1, 6, 5, 3, 6, 8, 5, 9, 4, 
5, 9, 1, 9, 8, 6, 0, 7, 4, 3, 2, 5, 2, 6, 1, 8, 5, 8, 4, 3, 
3, 6, 8, 4, 2, 6, 9, 7, 6, 3, 7, 7, 2, 8, 5, 0, 9, 9, 2, 4
 0:*****
1: ********
2: *********
3: *************
4: ***********
5: ***********
6: *******************
7: ********
8: *********
9: *******

java

2022-09-30 21:47

2 Answers

"The questioner is gone, but I just made the part where ""*"" is put out, so I would appreciate it if you could use it as a reference for those who are interested in it."

(new java.util.Random()) .ints(100,0,10)
    .boxed()
    .collect(java.util.stream.Collectors.groupingBy(
        java.util.function.function.identity(), java.util.stream.Collectors.counting()))
    .entrySet()
    .stream()
    .map(e->e.getKey()+":"+"*".repeat(e.getValue().intValue()))
    .forEach (System.out:println);

Just a little explanation.

Random#ints() is the method for creating a random IntStream.I'm counting it while grouping it with collect(...) (I've written this part for a lot of reference, so I don't really understand it).Also, it seems that IntStream should be boxed in boxed() to stream (there may be a way to do it directly).All you have to do is make it a normal stream and use map(...) as a string and print it out.Also, the String#repeat() used to repeat the string seems to be from Java 11, so it will not work unless it is Java 11.

I think this is faster once you get used to it than using the for statement.However, Java's Stream is a little difficult to use, so I'm not sure if I use it on a daily basis.(I don't usually write Java, so I can't say anything.)


2022-09-30 21:47

Focus on the display and do not take the original value.
if necessary Please correct it.

public class Sample {

    public static void main(String[]args) {
        int[] frequencies = new int[10];
        Random = new Random (System.currentTimeMillis());

        for(inti=0;i<100;i++){
            ++ frequencies [Math.abs(land.nextInt()% frequencies.length)];
        }

        for (inti=0;i<frequency.length;i++){
            System.out.println(creteMessage(i, frequencies[i]);
        }

    }

    public static String createMessage(int index, int number) {
        String msg;
        StringBuilder stb = new StringBuilder();

        for(inti=0;i<number;i++){
            stb.append("*");
        }

        return msg = String.format("[%02d]:%s", index, String.valueOf(stb)));
    }
}

The results are as follows:

[00]:****
[01]:**********
[02]:**********
[03]:******
[04]:**************
[05]:************
[06]:*************
[07]:************
[08]:**********
[09]:********


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.