【问题标题】:Numbers aren't displaying for my output我的输出没有显示数字
【发布时间】:2017-11-13 15:18:37
【问题描述】:

我正在尝试将数字显示为我创建的 ArrayList 的输出。我问用户他们想要在 ArrayList 中有多少个数字,然后使用 for 循环生成 1-100 之间的随机数,并且无论用户想要多少次,它们都会被扔到 ArrayList 中。我只是无法显示,这是我的代码:

KNW_MyList 类:

   public class KNW_MyList<T extends Number>
{
  //Create the array list object of type T
  ArrayList<T> al = new ArrayList<T>();

  /**
   * The adds method, add a number of type T to 
   * array list.
   * @param number, the number to be added.
   * */
  public void add( T number)
  {
    al.add(number);
  }

  /**
   * The largest method, returns the largest value in the 
   * array list.
   * */
  public T largest()
  {
    T large = al.get(0);

    //For-loop to find the largest value
    for(int x = 0; x < al.size(); x++)
    {
      if(al.get(x).toString().compareTo(large.toString()) > 0)
      {
        large = al.get(0);
      }
    }
    return large;
  }

  /**
   * The smallest method, returns the smallest value in the 
   * array list.
   * */
  public T smallest()
  {
    T small = al.get(0);

    //For-loop to find the largest value
    for(int x = 0; x < al.size(); x++)
    {
      if(al.get(x).toString().compareTo(small.toString()) < 0)
      {
        small = al.get(0);
      }
    }
    return small;
  }

  /**
   * The show method, wil show the elements in the array 
   * list.
   * */
  public void show()
  {
    System.out.println(al);
  }
}

演示:

import java.util.*;
import java.lang.Math;

public class KNW_MyListDemo
{
  public static void main(String args[])
  {
    //Create random class
    Random rand = new Random();

int numbers;
Scanner scan = new Scanner(System.in);

//Create ArrayList object
KNW_MyList<Number> numList = new KNW_MyList<Number>();

//Ask the user how many numbers they want in the array
System.out.println("How many numbers do you want?: ");
numbers = scan.nextInt();

if(numbers <= 0)
{
  System.out.println("Not Valid!");
}
else
{
  for(int x = 1; x >= numbers; x++)
  {
    int num = rand.nextInt(100) + 1;
    numList.add(num);
    x++;
  }

//Call the show method
System.out.println("Numbers in the array: ");
numList.show();
}

  }
}

我的 ArrayList 或 forloop 有问题吗?我不太确定,对数组列表有点新,所以这可能会或可能不会有任何影响?我只想让随机数显示“x”次,“x”是用户想要的次数。

【问题讨论】:

标签: java for-loop arraylist random


【解决方案1】:

您的循环中有一些错误。应该是

for(int x = 0; x < numbers; x++)
{
    int num = rand.nextInt(100) + 1;
    numList.add(num);
}

【讨论】:

    【解决方案2】:

    我认为问题在于将您的号码保存到列表中。您的情况:

    for(int x = 1; x >= numbers; x++)
    

    使循环几乎永远不会运行,因为只有当 x 大于 numbers 时循环才为真。您从 x = 1 开始,所以只有当 numbers 正好是 1 时,您的循环才会起作用。有点,因为它将是无限循环。改成

    for(int x = 1; x <= numbers; x++)
    

    应该没问题。


    编辑
    Ayo K 提到了我的错误,我忘记了条件中的 equals。
    此外,您将列表增加两次,一次在循环声明中,一次在正文中。

    【讨论】:

    • 哇,我觉得自己笨!非常感谢,我一看到第一行就意识到了我的问题!
    • 这个答案会给出错误的输出并且只将numbers-1 数字添加到列表中
    • 是的,我还注意到您将x 增加了两次。 Ayo K 也是对的,我会编辑这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多