如何使用数组找到直方图?

时间:2021-10-22 21:20:31

Build/Output Histograms using Arrays Where i'm wrong?

在我错误的地方使用数组构建/输出直方图?

This code takes 5 inputs from user in array and show the number of stars for example if user enter 3 then *** would be shown and so on .Where i'm wrong ?

这段代码从用户的数组中获取5个输入,并显示用户输入3时的星星数量,然后显示***,等等。我哪里错了?

  public class P20 {
  public static void main(String[] args) {

   int[] anArray;
   int Number;
//setup variable value
   anArray = new int [10];
   System.out.println("Enter some numbers between 1 and 100.");

  for (int i = 0; i < 10; i++) {
    System.out.println(i);

   anArray[0] = 1-9;
   anArray[1] = 10-19;
   anArray[2] = 20-29;
   anArray[3] = 30-39;
   anArray[4] = 40-49;
   anArray[5] = 50-59;
   anArray[6] = 60-69;
   anArray[7] = 70-79;
   anArray[8] = 80-89;
   anArray[9] = 90-100;
   if(anArray > 0) {
  System.out.println("*"+Number );   

  else if(anArray > 20) 
  {
   System.out.println("**"+Number ); 
  }
  else if (anArray > 30) 
  {
   System.out.println("***"+Number ); 
  }    
  else if (anArray > 40) 
  {
   System.out.println("****"+Number ); 
  }            
  else if (anArray > 50) 
  {
  System.out.println("*****"+Number ); } 
 }}

2 个解决方案

#1


0  

This code takes 5 inputs from user in array and show the number of stars for example if user enter 3 then *** would be shown and so on .Where i'm wrong ?

这段代码从用户的数组中获取5个输入,并显示用户输入3时的星星数量,然后显示***,等等。我哪里错了?

This code doesn't take inputs from the user.

此代码不接受用户的输入。

You want the user to input a value and then you print the number of stars he/she wrote. You could do this like that:

您希望用户输入一个值,然后打印他/她所写的星号。你可以这样做:

public class P20 //Why don't you try giving names that are easier to remember
{
    public static void main(String[] args)
    {

        for (int i=0; i<args[0]; i++)
        {
            System.out.print("*");
        }
}

Where args[0] is the first argument given to the program when called:

args[0]是在被调用时给出的第一个参数:

java P20 3

Anyway, let me try to point out what some of the mistakes of your code are:

不管怎样,让我试着指出你的代码中有些错误是:

When you wrote:

当你写道:

anArray[0] = 1-9;

anArray[0]= 1 - 9;

Did you really want to write that wich means "Let the 0th element of the array be the number 1 minus 9" (=-8)?

你是否真的想写“让数组的第0个元素是1 - 9”(=-8)?

Or did you intent to input an interval? (Meaning the numbers from 1 to 9).

或者你打算输入一个区间?(指从1到9的数字)。

Then later on you say

然后你说。

if(anArray > 0)

如果(anArray > 0)

Which doesn't make sense to me since anArray is an int[] variable and not an int variable. This means that anArray is not a variable which points directly to a number, but one pointing to an array object which holds several int values.

这对我来说没有意义,因为数组是int[]变量,而不是int变量。这意味着anArray不是一个直接指向数字的变量,而是指向一个包含多个int值的数组对象。

#2


0  

 if(anArray > 0) {

you always will be > 0 so you always get only 1 x *

你永远是>,所以你总是只得到1 x *。

you hve to modify your if clause like that:

你必须修改你的if从句:

if (anArray > 0 && anArray < 20){

and so on...

等等……

edit:-----------------------------------------------------------

编辑:- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

like you requested:

像你这样的要求:

public String stars(int n) {
    if (n == 1){
      return "*";
    }else{
      return "*" + stars(n-1);
    }
  }

#1


0  

This code takes 5 inputs from user in array and show the number of stars for example if user enter 3 then *** would be shown and so on .Where i'm wrong ?

这段代码从用户的数组中获取5个输入,并显示用户输入3时的星星数量,然后显示***,等等。我哪里错了?

This code doesn't take inputs from the user.

此代码不接受用户的输入。

You want the user to input a value and then you print the number of stars he/she wrote. You could do this like that:

您希望用户输入一个值,然后打印他/她所写的星号。你可以这样做:

public class P20 //Why don't you try giving names that are easier to remember
{
    public static void main(String[] args)
    {

        for (int i=0; i<args[0]; i++)
        {
            System.out.print("*");
        }
}

Where args[0] is the first argument given to the program when called:

args[0]是在被调用时给出的第一个参数:

java P20 3

Anyway, let me try to point out what some of the mistakes of your code are:

不管怎样,让我试着指出你的代码中有些错误是:

When you wrote:

当你写道:

anArray[0] = 1-9;

anArray[0]= 1 - 9;

Did you really want to write that wich means "Let the 0th element of the array be the number 1 minus 9" (=-8)?

你是否真的想写“让数组的第0个元素是1 - 9”(=-8)?

Or did you intent to input an interval? (Meaning the numbers from 1 to 9).

或者你打算输入一个区间?(指从1到9的数字)。

Then later on you say

然后你说。

if(anArray > 0)

如果(anArray > 0)

Which doesn't make sense to me since anArray is an int[] variable and not an int variable. This means that anArray is not a variable which points directly to a number, but one pointing to an array object which holds several int values.

这对我来说没有意义,因为数组是int[]变量,而不是int变量。这意味着anArray不是一个直接指向数字的变量,而是指向一个包含多个int值的数组对象。

#2


0  

 if(anArray > 0) {

you always will be > 0 so you always get only 1 x *

你永远是>,所以你总是只得到1 x *。

you hve to modify your if clause like that:

你必须修改你的if从句:

if (anArray > 0 && anArray < 20){

and so on...

等等……

edit:-----------------------------------------------------------

编辑:- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

like you requested:

像你这样的要求:

public String stars(int n) {
    if (n == 1){
      return "*";
    }else{
      return "*" + stars(n-1);
    }
  }