Java-数组中的整数在有多个实例时显示为未找到
我编写了一个脚本,创建一个数组并用随机数填充它。用户输入一个数字,它显示该数字及其索引。有些数字是重复的,必须全部找到。但是,如果用户输入的号码不存在,则需要告知用户。我的问题是,大多数时候(不是所有时候),如果这个数字存在,它会像应该的那样列出它,但也会说找不到它。我相信现在发生的是,通过它的最后一个循环没有找到该数字的任何后续实例,因此它也认为它不存在。如果这个数字真的不存在,我怎么能让它只说没有找到呢?谢谢你的帮助Java-数组中的整数在有多个实例时显示为未找到,java,Java,我编写了一个脚本,创建一个数组并用随机数填充它。用户输入一个数字,它显示该数字及其索引。有些数字是重复的,必须全部找到。但是,如果用户输入的号码不存在,则需要告知用户。我的问题是,大多数时候(不是所有时候),如果这个数字存在,它会像应该的那样列出它,但也会说找不到它。我相信现在发生的是,通过它的最后一个循环没有找到该数字的任何后续实例,因此它也认为它不存在。如果这个数字真的不存在,我怎么能让它只说没有找到呢?谢谢你的帮助 int[] $myUnsorted = new int[1
int[] $myUnsorted = new int[10]; // Define a new array and fill it with ten elements
for(int x = 0; x < $myUnsorted.length; x++) { // Begin For Loop
$myUnsorted[x] = (int)(Math.random() * 20 + 1); // Make the elements random numbers between 1 & 20
} // End For Loop
int[] $mySorted = new int[$myUnsorted.length]; // Define a new array and fill it with the same amount of elements as the first array
for(int x = 0; x < $myUnsorted.length; x++)
$mySorted[x] = $myUnsorted[x]; // Copy the first array
Arrays.sort($mySorted); // Sort the second array
System.out.println("Unsorted Array \t \t \t Sorted Array"); // Print the labels with tabs separating them
for(int x=0; x<$myUnsorted.length; x++) { // Begin For Loop
System.out.printf("%d \t \t \t \t %d \n",$myUnsorted[x],$mySorted[x]); // Print the numbers with tabs separating them to fall under the respective labels
} // End For Loop
Scanner $myScan = new Scanner(System.in); // Load the Java Scanner Class
System.out.print("\nPlease enter number to search for: "); // Print the instructional text
int $mySearch = $myScan.nextInt(); // Define a new variable for the search
for(int x = 0; x < $myUnsorted.length; x++) { // Begin For Loop
if($myUnsorted[x] == $mySearch) { // If a number matches the search then...
System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the unsorted array"); // ...print the search results
} // End If Statement
else if(x == $myUnsorted.length - 1) { // If a number does NOT match the search then...
System.out.println("Search Value: " + $mySearch + " was not found"); // ...print the text
} // End ElseIf Statement
} // End For Loop
for(int x = 0; x < $mySorted.length; x++) { // Begin For Loop
if($mySorted[x] == $mySearch) { // If a number matches the search then...
System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the sorted array"); // ...print the search results
} // End If Statement
} // End For Loop
int[]$myUnsorted=new int[10];//定义一个新数组并用十个元素填充它
for(int x=0;x<$myUnsorted.length;x++){//Begin for循环
$myUnsorted[x]=(int)(Math.random()*20+1);//使元素的随机数介于1和20之间
}//循环结束
int[]$mySorted=新int[$myUnsorted.length];//定义一个新数组,并用与第一个数组相同数量的元素填充它
对于(int x=0;x<$myUnsorted.length;x++)
$mySorted[x]=$myUnsorted[x];//复制第一个数组
Arrays.sort($mySorted);//对第二个数组排序
System.out.println(“未排序数组\t\t\t排序数组”);//打印标签,标签之间用制表符隔开
对于(int x=0;x使用变量跟踪是否找到任何内容:
boolean found = false;
for(int x = 0; x < $myUnsorted.length; x++) {
if($myUnsorted[x] == $mySearch) {
found = true;
System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the unsorted array");
}
}
if(!found) {
System.out.println("Search Value: " + $mySearch + " was not found");
}
boolean-found=false;
对于(int x=0;x<$myUnsorted.length;x++){
如果($myUnsorted[x]=$mySearch){
发现=真;
System.out.println(“搜索值:+$mySearch+”位于未排序数组中的“+x+”位置);
}
}
如果(!找到){
System.out.println(“未找到搜索值:+$mySearch+”);
}
创建一个局部布尔变量,以跟踪是否发现了某些内容
boolean isFound = false;
for(int x = 0; x < $myUnsorted.length; x++) {
if($myUnsorted[x] == $mySearch) {
System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the unsorted array");
isFound = true;
}
}
if (!isFound) {
System.out.println("Search Value: " + $mySearch + " was not found");
} else {
for(int x = 0; x < $mySorted.length; x++) {
if($mySorted[x] == $mySearch) {
System.out.println("Search Value: " + $mySearch + " found at location: " + x + " in the sorted array"); // ...print the search results
}
}
boolean isFound=false;
对于(int x=0;x<$myUnsorted.length;x++){
如果($myUnsorted[x]=$mySearch){
System.out.println(“搜索值:+$mySearch+”位于未排序数组中的“+x+”位置);
isFound=true;
}
}
如果(!isFound){
System.out.println(“未找到搜索值:+$mySearch+”);
}否则{
对于(int x=0;x<$mySorted.length;x++){
如果($mySorted[x]=$mySearch){
System.out.println(“搜索值:+$mySearch+”位于排序数组中的位置:“+x+”);/…打印搜索结果
}
}
那些$
让我头疼不已。你的命名惯例也很奇怪。为什么每个变量都以my
作为前缀?啊,对不起,伙计们。我首先是一个PowerShell人,所以这就是“$”的来源。这有助于我了解这些都是变量/数组,因为我是Java新手。至于“my”去,这也是我对变量/数组的命名。在Java中这样做不好吗?这是非常非常规的。如果你希望其他人阅读你的代码,请避免这样做。请查看打印“未找到”的逻辑。如果号码与列表中的最后一个号码不匹配,则在最后一个循环中打印此号码。如果用户在列表中输入最后一个号码,则不会打印此号码。测试方法错误;正确的方法作为答案给出。