直方图频率帮助 Java

发布于 2024-08-14 02:07:18 字数 1952 浏览 2 评论 0原文

public class Histogram
{
private int lo_;
private int hi_;
private int[] frequency_;

public Histogram(int lo, int hi)
{
    lo_ = lo;
    hi_ = hi;
    int range = hi_-lo_+1;
    frequency_ = new int[range];
    for(int i =0; i <range; range++)
        frequency_[i] = 0;
}

public void ReadValue()
{
    Scanner in = new Scanner(System.in);
    int value= in.nextInt();
    while(value != -1)
    {
        if(value >= lo_ && value <= hi_)               
        {
            frequency_[value - lo_]++;
            value = in.nextInt();
        }
    }
}

private String starPrinter(int value)
{
    String star = "*";
    for(int i = 0; i <= value ;i++)
    {
        star +="*";
    }
    return star;
}

public String Printer()
{
    String print = new String();
    int range = hi_-lo_+1;
    int i = 0;
    while(i<range)
    {
        print += (lo_+i)+" : "+ starPrinter(i)+ "\n";
        i++;
    }
    return print;
}


public int query(int value)
{
    if (value >= lo_ && value <= hi_)
    {
        value -= lo_;
        return starPrinter(value).length();
    }
    else
        return -1;
}

public static void main(String[] args)
{
    Histogram test = new Histogram(3, 9);
    test.ReadValue();
}

}

我需要有关此直方图的帮助。

构造函数是由低数和高数生成的(所以如果我把 3 到 9:这些都是它期望的数字,其他任何数字都被忽略)

readValue 方法将继续循环,直到用户输入 -1。意义 如果我输入 3, 4, 6, 4, 6, 9 , 5, 9, 4, 10 -1... 那么它将存储 所有这些都在频率[]中。我该如何做才能使每个值都可以 在频率[]中跟踪?

3 出现一次,4 出现三次,7 从未出现,9 出现两次

Printer() 会给我一个看起来像这样的直方图 (使用之前输入的数字...)

3: *
4: ***
5: *
6: **
7:
8:
9: **

如何使用频率所具有的数字来打印数量 数字出现在哪些星星?

查询方法将询问用户他们想要什么号码, 告诉他们它出现了多少次:

types 3“3 出现 2 次”

types 10“10 超出范围。”

我拥有大部分代码,我只需要帮助实现某些部分。

public class Histogram
{
private int lo_;
private int hi_;
private int[] frequency_;

public Histogram(int lo, int hi)
{
    lo_ = lo;
    hi_ = hi;
    int range = hi_-lo_+1;
    frequency_ = new int[range];
    for(int i =0; i <range; range++)
        frequency_[i] = 0;
}

public void ReadValue()
{
    Scanner in = new Scanner(System.in);
    int value= in.nextInt();
    while(value != -1)
    {
        if(value >= lo_ && value <= hi_)               
        {
            frequency_[value - lo_]++;
            value = in.nextInt();
        }
    }
}

private String starPrinter(int value)
{
    String star = "*";
    for(int i = 0; i <= value ;i++)
    {
        star +="*";
    }
    return star;
}

public String Printer()
{
    String print = new String();
    int range = hi_-lo_+1;
    int i = 0;
    while(i<range)
    {
        print += (lo_+i)+" : "+ starPrinter(i)+ "\n";
        i++;
    }
    return print;
}


public int query(int value)
{
    if (value >= lo_ && value <= hi_)
    {
        value -= lo_;
        return starPrinter(value).length();
    }
    else
        return -1;
}

public static void main(String[] args)
{
    Histogram test = new Histogram(3, 9);
    test.ReadValue();
}

}

I need help on this Histogram.

The constructor is generated by low number and high number (so if I put 3
to 9: these are all the numbers it expects, anything else is ignored)

the readValue method will keep looping until the user types -1. Meaning
that if I type 3, 4, 6, 4, 6, 9 , 5, 9, 4, 10 -1... then it will store
all that in the frequency[]. How do I make it so that each value can be
tracked in the frequency[]?

3 occurs once, 4 occurs three times, 7 never occurs, 9 occurs twice

Printer() will make me a Histogram diagram that looks like this
(Using the numbers inputted before...)

3: *
4: ***
5: *
6: **
7:
8:
9: **

How do I use the numbers the frequency has to print the number of
stars the numbers occured in?

the query method will ask the user for what number they want and
tells them how many times it occurs:

types 3 "3 occurs 2 times"

types 10 "10 is out of range."

I have most of the code, I just need help implementing some parts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2024-08-21 02:07:18

你几乎已经完成了,还有一些愚蠢的错误。您的 starPrinter 方法打印的星星比应有的多了两颗。你应该这样写:

private String starPrinter(int value)
{
    String star = "";
    for(int i = 0; i < value ;i++)
    {
        star +="*";
    }
    return star;
}

并且你将错误的参数传递给starPrinter。应该是这样的:

print += (lo_+i)+" : "+ starPrinter(frequency_[i])+ "\n";

最后你要记得调用它。只需在 main 末尾添加一行即可:

public static void main(String[] args)
{
    Histogram test = new Histogram(3, 9);
    test.ReadValue();
    System.out.println(test.Printer());   // Add this line.
}

现在可以了! (只要您不输入超出范围的数字即可。)

You've almost done it, there's a few silly mistakes. Your starPrinter method prints two more stars than it should. You should write this:

private String starPrinter(int value)
{
    String star = "";
    for(int i = 0; i < value ;i++)
    {
        star +="*";
    }
    return star;
}

and you pass the wrong parameter to starPrinter. It should be this:

print += (lo_+i)+" : "+ starPrinter(frequency_[i])+ "\n";

Finally, you have to remember to call it. Just add one more line at the end of main:

public static void main(String[] args)
{
    Histogram test = new Histogram(3, 9);
    test.ReadValue();
    System.out.println(test.Printer());   // Add this line.
}

Now it works! (As long as you don't type an out of range number.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文