gpt4 book ai didi

java - 如何从数组中的 search() 方法返回单个打印语句?

转载 作者:行者123 更新时间:2023-11-29 04:49:35 25 4
gpt4 key购买 nike

我的搜索方法有问题。我想要做的是让我的搜索方法只打印一次语句。因此,如果我的数组不止一次包含“3”,我只想打印“3 was found”。一次而不是检查每个值并报告数组中该点是否存在“3”。我该怎么做?

澄清一下,这就是我所拥有的:

`0,0,0,0,0,0,0,0,0,0
4,9,6,9,0,8,5,2,8,3
Average Value: 5.4
Maximum Value: 9
Minimum Value: 0
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was not found.
3 was found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was not found.
2 was found.
2 was not found.
2 was not found.`

这就是我想要的:

0,0,0,0,0,0,0,0,0,0

4,9,6,9,0,8,5,2,8,3

Average Value: 5.4
Maximum Value: 9
Minimum Value: 0
3 was found.
2 was not found.

所以这是我的完整类(class)。我创建了一个名为 initialize 的方法,它将为数组中的每个元素分配一个 0 到 10 之间的随机整数;一个叫做 print 的方法打印出我的数组的内容;一个名为 printStats 的方法,用于查找并打印数组中的平均值、最大值和最小值;以及一个名为 search 的方法,它在我的数组中搜索(并打印结果)以查找传递给我的方法的整数参数。

一切正常。

public class ArrayLab
{
private int[] array;
public ArrayLab(int numElements)
{
array = new int[numElements];
}
public void initialize()
{
array[0] = (int) (Math.random()*11);
array[1] = (int) (Math.random()*11);
array[2] = (int) (Math.random()*11);
array[3] = (int) (Math.random()*11);
array[4] = (int) (Math.random()*11);
array[5] = (int) (Math.random()*11);
array[6] = (int) (Math.random()*11);
array[7] = (int) (Math.random()*11);
array[8] = (int) (Math.random()*11);
array[9] = (int) (Math.random()*11);
}
public void print() {
System.out.println(array[0] + "," + array[1] + "," + array[2] + "," + array[3] + "," + array[4] + "," + array[5] + "," + array[6] + "," + array[7] + "," + array[8] + "," + array[9]);
System.out.println();
}

public void printStats()
{
double sum = 0;
int max = 0;
int min = 0;
min = array[0];

for (int i = 0; i < array.length; i++)
{
sum = sum + array[i];

if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}

double average = sum/array.length;
System.out.println("Average Value: " + average);
System.out.println("Maximum Value: " + max);
System.out.println("Minimum Value: " + min);


}

public void search(int numChosen)
{
for(int i = 0; i < array.length; i++)
{
if(array[i] == numChosen)
{
System.out.println(numChosen + " was found.");
}
else
{
System.out.println(numChosen + " was not found.");
}
}
}
}

最佳答案

在您第一次成功搜索后,开始使用returnbreak 语句来中断循环。

此外,您不应在每次迭代数组时都打印Was Not Found。当您的数组完全耗尽并且找不到搜索查询时,您应该只在最后打印一次。

这是修改后的代码片段:

boolean flag = false;
for(int i = 0; i < array.length; i++)
{
if(array[i] == numChosen)
{
System.out.println(numChosen + " was found.");
flag = true;
break;
}
}

if(!flag) {
System.out.println(numChosen + " was not found.");
}

或者,您也可以执行以下操作:

for(int i = 0; i < array.length; i++)
{
if(array[i] == numChosen)
{
System.out.println(numChosen + " was found.");
return;
}
}
System.out.println(numChosen + " was not found.");

关于java - 如何从数组中的 search() 方法返回单个打印语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35924676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com