数组值输出多于一个
Array Value Output more than one
这是我想出的代码。但是,我希望能够输出:
- (值)在插槽 x 中。
- (值)在插槽 x 中。
两个输出,如果(值)在两个插槽中可用 - 如 7.
或
- (num) 不在数组中。
但不能两者兼得。
有人可以帮忙吗?
public static void main(String[] args) {
int search, counter;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (counter = 0; counter < num.length; counter++ ){
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
}
}
if (counter == num.length)
{
System.out.println(search + " is not in the array.");
}
}
}
虽然我觉得您可能应该在另一个社区(例如 https://codereview.stackexchange.com/ 上提出这样的问题,但我可以提供一个建议:
使用布尔标志检查您之前是否找到过它。像这样:
public static void main(String[] args) {
int search;
boolean found = false;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (int counter = 0; counter < num.length; counter++ ) {
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
found = true;
}
}
if (!found) {
System.out.println(search + " is not in the array.");
}
in.close();
}
所以你只在线性遍历数组后找不到元素时才打印"not found"消息...
您的代码存在问题,您检查计数器是否已达到数组长度。总会发生的事情。您应该检查是否找到了一个值。
这应该可以解决问题:
public static void main(String[] args) {
int search, counter;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
boolean wasFound = false;
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (counter = 0; counter < num.length; counter++ ){
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
wasFound = true;
}
}
if (!wasFound)
{
System.out.println(search + " is not in the array.");
}
}
这是我想出的代码。但是,我希望能够输出:
- (值)在插槽 x 中。
- (值)在插槽 x 中。
两个输出,如果(值)在两个插槽中可用 - 如 7.
或
- (num) 不在数组中。
但不能两者兼得。 有人可以帮忙吗?
public static void main(String[] args) {
int search, counter;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (counter = 0; counter < num.length; counter++ ){
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
}
}
if (counter == num.length)
{
System.out.println(search + " is not in the array.");
}
}
}
虽然我觉得您可能应该在另一个社区(例如 https://codereview.stackexchange.com/ 上提出这样的问题,但我可以提供一个建议:
使用布尔标志检查您之前是否找到过它。像这样:
public static void main(String[] args) {
int search;
boolean found = false;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (int counter = 0; counter < num.length; counter++ ) {
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
found = true;
}
}
if (!found) {
System.out.println(search + " is not in the array.");
}
in.close();
}
所以你只在线性遍历数组后找不到元素时才打印"not found"消息...
您的代码存在问题,您检查计数器是否已达到数组长度。总会发生的事情。您应该检查是否找到了一个值。
这应该可以解决问题:
public static void main(String[] args) {
int search, counter;
int num[]={3, 4, 5, 6, 7, 8, 10, 7, 9, 13};
boolean wasFound = false;
System.out.print("Array: ");
for (int count=0; count<num.length; count++)
System.out.print(+num[count] + " ");
Scanner in = new Scanner (System.in);
System.out.print("\nValue to find: ");
search = in.nextInt();
for (counter = 0; counter < num.length; counter++ ){
if (num[counter] == search)
{
System.out.println(search + " is in slot " + (counter + 1) + ".");
wasFound = true;
}
}
if (!wasFound)
{
System.out.println(search + " is not in the array.");
}
}