我需要一些帮助诊断这段代码的问题。

huangapple go评论76阅读模式
英文:

I need some help diagnosing the problem with this code

问题

我遇到了一些代码问题。这段代码的目的只是接受用户输入的数字,将它们添加到一个字符串中,并创建一个直方图,表示从1到50的每个5个数字范围内的数字数量。例如:

1 - 5: ***
6 - 10: ********
11 - 15: *
等等。

以下是代码部分:

public class Ch10Ex4 {
    
    public static int number;
    public static ArrayList<Integer> numbers = new ArrayList<>();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            getNums(1, 50);
        }
        
        histogram(1, 50, 5);
        System.out.println();
    }
    
    public static void getNums(int low_num, int high_num) {
        Scanner sc = new Scanner(System.in);
        
        do {
            System.out.print("Enter a number between " + low_num + 
                    " and " + high_num + ": ");
            number = sc.nextInt();
            
        } while (number < 1 || number > 50);
        
        System.out.println(number + " has been added successfully.");
        
        numbers.add(number);
    }
    
    public static void histogram(int low, int high, int range) {
        int temp_low = low;
        int temp_high = low + (range - 1);
        
        for (int i = 0; i < high / range; i++) {
            System.out.print("\n" + temp_low + " - " + temp_high + ": ");
            for (int arr : numbers) {
                if (arr >= temp_low && arr <= temp_high) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            temp_low += range;
            temp_high += range;
        }
        
    }
    
}

我以前的代码版本中,我会用两个参数调用histogram()。这些参数将是最小数字和最大数字,与通常一样,但没有int range参数。我也没有最外层的for循环。我必须调用10次直方图,例如:

histogram(1, 5);
histogram(6, 10);
histogram(11, 15);
等等

基本上,我会为每组五个数字调用它。它能工作,但效率很低,也不太可重用。问题是,当我运行这段代码(输入数字1-10)时,我得到了这个结果:

1 - 5: **********
6 - 10: *****
11 - 15: 
16 - 20: 
等等。

前五个数字集的输出中,每个数字都会显示一个星号。

抱歉问题陈述很长。非常感谢你的帮助,提前谢谢。

英文:

I'm having trouble with some code. This code's purpose is just to take user-inputted numbers, append them to a string, and create a histogram representing the amount of numbers in each 5 number range from 1 to 50. Ex.

1 - 5: ***
6 - 10: ********
11 - 15: *
etc.

Here is the code:

public class Ch10Ex4 {
public static int number;
public static ArrayList&lt;Integer&gt; numbers = new ArrayList&lt;&gt;();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
for (int i = 0; i &lt; 10; i++) {
getNums(1, 50);
}
histogram(1, 50, 5);
System.out.println();
}
public static void getNums(int low_num, int high_num) {
Scanner sc = new Scanner(System.in);
do {
System.out.print(&quot;Enter a number between &quot; + low_num + 
&quot; and &quot; + high_num + &quot;: &quot;);
number = sc.nextInt();
} while (number &lt; 1 || number &gt; 50);
System.out.println(number + &quot; has been added sucsessfully.&quot;);
numbers.add(number);
}
public static void histogram(int low, int high, int range) {
int temp_low = low;
int temp_high = low + (range - 1);
for (int i = 0; i &lt; high / range; i++) {
System.out.print(&quot;\n&quot; + temp_low + &quot; - &quot; + temp_high + &quot;: &quot;);
for (int arr:numbers) {
if (arr &gt;= temp_low &amp;&amp; i &lt;= temp_high) {
System.out.print(&quot;*&quot;);
} else {
}
}
temp_low += range;
temp_high += range;
}
}
}

I had a previous version of this code where I would call histogram() with two parameters. These would be the lowest number and the highest number like usual but no int range parameter. And I didn't have the outermost for-loop. I would have to call histogram 10 times ex.

histogram(1, 5);
histogram(6, 10);
histogram(11, 15);
etc.

Basically, I would call it for every set of five numbers. It worked but it was super inefficient and not very reusable. The problem is that when I run this code (entering numbers 1- 10), I get this:

1 - 5: **********
6 - 10: *****
11 - 15: 
16 - 20: 
etc.

The first set of five outputs an asterix for every number in the array.

Sorry for the incredibly long post. Any help is much appreciated and thank you in advance.

答案1

得分: 1

错误在于你在这个if语句中使用了i

if (arr >= temp_low && i <= temp_high) {
    System.out.print("*");
} else {
}

将其改为arr,问题就会解决。

英文:

The bug is that you are using i in this if-statement.

if (arr &gt;= temp_low &amp;&amp; i &lt;= temp_high) {
System.out.print(&quot;*&quot;);
} else {
}

Switch it to arr and you should be good.

huangapple
  • 本文由 发表于 2020年10月20日 03:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64434364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定