私が作成した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);
}
}
デモ:私のArrayListや私のforloopに何か問題が
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();
}
}
}
ありますか?私は確信していない、配列リストにちょっと新しいかもしれないので多分これは効果がないかもしれませんか?私はちょうど乱数を「x」の時間量を表示したい、「x」はユーザーが望む回数である。
あなたは 'KNW_MyList'クラスのコードを表示していません。 [mcve] – jontro
にお読みください。 'KNW_MyList'とは何ですか? –