我的搜索方法有问题。我想要做的是让我的搜索方法只打印一次语句。因此,如果我的数组不止一次包含“3”,我只想打印“3被发现”。一次而不是检查每个值并报告阵列中该点处存在或不存在“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.
所以这是我的完整课程。我创建了一个名为initialize的方法,它将为我的数组中的每个元素分配一个0到10之间的随机整数;一个叫print的方法来打印出我的数组的内容;一个名为printStats的方法,用于查找并打印数组中的平均值,最大值和最小值;以及一个名为search的方法,它搜索我的数组(并打印结果)以获取传递给my方法的整数参数。
一切正常。
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.");
}
}
}
}
答案 0 :(得分:1)
嗯,一旦找到号码,你就不需要继续循环。此外,你想要打印&#34;没有找到&#34;在它没有找到任何东西的情况下,意味着循环完成而没有打印任何东西。 所以这就是你应该如何实现的:
public void search(int numChosen)
{
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.");
}
在发现某些内容的情况下,它会打印该消息并退出该方法,并且永远不会打印第二条消息。它只会在循环结束时打印第二条消息。
答案 1 :(得分:1)
在您第一次成功搜索后,开始使用return
或break
语句来打破循环。
此外,在迭代数组时,不应每次都打印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.");
答案 2 :(得分:1)
您正在$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$picture_name = $userModel->username . '.' . pathinfo($upload_file, PATHINFO_EXTENSION);
$personModel->picture = $picture_name;
if (isset($upload_file)) {
$upload_file->saveAs(Yii::app()->basePath . '/../img/avatar/' . $picture_name);
}
$personModel->save();
功能中显示结果。在你的情况下,不是每次遇到匹配时打印,而是放置一个计数器,然后打印一次:该计数器与你的其余部分一起判刑。
试试这个:
public void search(int numChosen)