import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class JMUnit2Ch10
{
public static void main(String[] args)
{
ArrayList<Integer> randomNumbers = new ArrayList<>();
System.out.print("How many random numbers between 1 and 100:");
Scanner stdIn = new Scanner(System.in);
int numOfNumbers = stdIn.nextInt();
for(int i=0; i<numOfNumbers; i++)
{
randomNumbers.add((int)(Math.random()*100)+1);
}
System.out.print("Which random number are your searching for?:");
int searchNumber = stdIn.nextInt();
System.out.printf("%n");
for(int a: randomNumbers)
{
System.out.println("Unsorted List");
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(searchNumber));
}
Collections.sort(randomNumbers);
for(int b:randomNumbers)
{
System.out.println("Sorted List");
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(searchNumber));
}
System.out.println(randomNumbers.stream().mapToInt(value -> value).sum());
}
}
我尝试使用布尔值仅显示如果找到我使用的代码是:
boolean found = false;
然后在我提出的两个印刷语句中:
found=true;
并在底部放置:
if(!found)
{
System.out.println(randomNumbers.stream().mapToInt(value -> value).sum());
}
并且仍然为每个循环的每个迭代获得输出。我想要做的是在ArrayList
中获得单个输出,该数字出现在排序和未排序的ArrayList
中,然后是数组列表中所有数字的总和。
Edit1:我得到的当前输出是searchNumber
,其中有多少个数字,因此输出x
x
numOfNumers
的时间{{1}} }}。我希望它只输出它在两个数组中出现的次数。
编辑2:
因为我被建议不要使用我希望它显示的图片:
1到100之间有多少随机数:1000
你在搜索哪个随机数?:5
未排序列表 5在位置找到:502 5发现在位置:725 5在地点找到:856 5发现在位置:924
排序列表 5发现在位置:40 5发现在位置:41 5在地点找到:42 5发现在位置:43
所有数字的总和:49089
我得到的是 未排序的列表 在位置502 X1000处找到5 排序列表 5位于41 X1000
所有数字的总和:42013
答案 0 :(得分:2)
您需要添加一个条件,检查当前数字(a或b)是否等于searchNumber
。但是,当发现发生时,您现在尝试打印randomNumbers.indexOf(searchNumber)
。这将为每次出现返回相同的数字,即第一次出现的索引。唯一的方法是使用for (int i = 0; i < randomNumbers.size(); i++)
循环或使用计数器。
此外,我会在循环上方放置“未排序列表”和“排序列表”,以便这些行只打印一次。
如果您使用for (int i = 0; i < randomNumbers.size(); i++)
,您的代码将会显示如下内容:
System.out.println("Unsorted List");
for (int i = 0; i < randomNumbers.size(); i++) {
if (randomNumbers.get(i) == searchNumber) {
System.out.println(searchNumber + " found at location: " + i);
}
}
Collections.sort(randomNumbers);
System.out.println("Sorted List");
for (int i = 0; i < randomNumbers.size(); i++) {
if (randomNumbers.get(i) == searchNumber) {
System.out.println(searchNumber + " found at location: " + i);
}
}
或者如果您想使用计数器:
int loc = 0;
System.out.println("Unsorted List");
for(int a : randomNumbers) {
if (searchNumber == a) {
System.out.println(searchNumber + " found at location: "+ loc);
}
loc++;
}
Collections.sort(randomNumbers);
loc = 0;
System.out.println("Sorted List");
for(int b : randomNumbers) {
if (searchNumber == b) {
System.out.println(searchNumber + " found at location: " + loc);
}
loc++;
}
答案 1 :(得分:1)
我要做的是在每次获得单个输出 ArrayList表示该数字在排序和未排序中都出现 然后ArrayLists是数组列表中所有数字的总和。
只需添加if条件,如下所示:
for(int a: randomNumbers)
{
System.out.println("Unsorted List");
if(a == searchNumber)
{
System.out.println(searchNumber +
" found at location: "+ randomNumbers.indexOf(searchNumber));
}
}
答案 2 :(得分:1)
我认为你的问题在于:
int searchNumber = stdIn.nextInt();
for(int a: randomNumbers)
{
System.out.println("Unsorted List");
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(searchNumber));
}
Collections.sort(randomNumbers);
for(int b:randomNumbers)
{
System.out.println("Sorted List");
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(searchNumber));
}
您正在searchNumber
中读取一个数字,但从不使用它与randomNumbers
中的数字进行比较,而是打印数组中的每个数字。
因此,在for-each
个循环中,您可以:
if (searchNumber == a) {
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(a));
}
与b
变量相同。
我还会在循环之前放置System.out.println("Unsorted List");
...
所以,看起来应该是这样的:
System.out.println("Unsorted List");
for (int a : randomNumbers) {
if (searchNumber == a) {
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(a));
}
}
Collections.sort(randomNumbers);
System.out.println("Sorted List");
for (int b : randomNumbers) {
if (searchNumber == b) {
System.out.println(searchNumber + " found at location: "+ randomNumbers.indexOf(b));
}
}
感谢@ChrisWitteveen指出这一点:
这里的一个问题是randomNumbers.indexOf(a)和randomNumbers.indexOf(b)总是返回相同的数字。
在这种情况下,您可以使用简单的for
循环而不是for-each
循环来简化代码:
将上述建议更改为:
System.out.println("Unsorted List");
randomNumbers = new ArrayList<>(randomNumbers);
for (int i = 0; i < randomNumbers.size(); i++) {
if (searchNumber == randomNumbers.get(i)) {
System.out.println(searchNumber + " found at location: " + i);
}
}
Collections.sort(randomNumbers);
System.out.println("Sorted List");
for (int i = 0; i < randomNumbers.size(); i++) {
if (searchNumber == randomNumbers.get(i)) {
System.out.println(searchNumber + " found at location: " + i);
}
}
诀窍,例如我尝试并将随机生成从1-100更改为1-10以进行测试,我们得到以下输出,我还打印了整个数字列表,仅用于调试目的:
How many random numbers between 1 and 100:20
2
9
2
6
10
5
6
6
7
6
9
9
3
7
9
5
10
3
6
8
Which random number are your searching for?:2
Unsorted List
2 found at location: 0
2 found at location: 2
Sorted List
2 found at location: 0
2 found at location: 1
128