Java 仅在最后一次迭代时输出For循环的值

Java 仅在最后一次迭代时输出For循环的值,java,if-statement,for-loop,sequence,Java,If Statement,For Loop,Sequence,我试图为循环索引(I)输出I的值,但仅在最后一次迭代中。我要么得到一个错误,要么它输出I的所有值。以下是我到目前为止的情况: boolean sequentialSearch(int x) { for(int i = 0; i < n; i++) //n comes from function length() if(n != 0) { if(list[i] == x) { r

我试图为循环
索引(I)
输出
I
的值,但仅在最后一次迭代中。我要么得到一个错误,要么它输出
I
的所有值。以下是我到目前为止的情况:

boolean sequentialSearch(int x) {

       for(int i = 0; i < n; i++)  //n comes from function length()

           if(n != 0) {

               if(list[i] == x) {

                   return true;
               }

               return false;
           }
}
布尔顺序搜索(int x){
对于(int i=0;i
试试:

for(int i=0;i
我想你需要这样的东西:

boolean sequentialSearch(int x) {
    for(int i=0;i<n;i++) { //n comes from function length()
        if(list[i]==x){   // list comes from somewhere too
            System.out.println(i); // print i
            return true;
        }
    }
    return false;
}
为什么你检查我=0, 而不是从
i=1迭代

for(int i = 1; i < n; i++) 
 {
    if(list[i] == x) 
    { 
        return true;
    }
    return false;
 }
for(int i=1;i

这要容易得多。:-)

是否确实要打印
i
并返回布尔值?既然
-1
经常用于not found,为什么不将其分为两种方法呢。现在,您可以搜索任何您想要的目的,即使它是一个系统,
system.out
在web应用程序中不可用

boolean sequentialSearch(int x) {
    for(int i = 0, n = list.length; i < n; i++) { 
        if(list[i] == x) {
            return i;
        }
    }
    return -1;
}

// Later
int index = sequentialSearch(y);
if (index != -1) {
    System.out.println(y + " found at index " + index);
} else {
    System.out.println(y + " not found");
}    
布尔顺序搜索(int x){
对于(inti=0,n=list.length;i
如何单独打印最后一个值

import java.util.Scanner;

public class Fibonacci {

    public static void main(String[] arguments) {
        int n1 = 0, n2 = 1, n3;

        @SuppressWarnings("resource")
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = s.nextInt();
        for (int i = 0; i < n; ++i) {   
            n3 = n1 + n2;

            n1 = n2;
            n2 = n3;
        System.out.println(""+n3);
        }
    }
}
import java.util.Scanner;
公共类斐波那契{
公共静态void main(字符串[]参数){
int n1=0,n2=1,n3;
@抑制警告(“资源”)
扫描仪s=新的扫描仪(System.in);
System.out.print(“输入n的值:”);
int n=s.nextInt();
对于(int i=0;i
你的代码有几个问题,你能解释一下你想做什么吗?我也想输出I,到目前为止,你的代码和我的代码完全一样。我编辑了我的答案。如果您希望代码返回索引,请返回
i
而不是
true
,并返回
-1
而不是
false
。太好了,我确实有一个无聊的时刻。哈哈,谢谢!我想这比那更复杂。很乐意帮忙!如果你能接受我的回答,我将不胜感激。
boolean sequentialSearch(int x) {
    for(int i = 0, n = list.length; i < n; i++) { 
        if(list[i] == x) {
            return i;
        }
    }
    return -1;
}

// Later
int index = sequentialSearch(y);
if (index != -1) {
    System.out.println(y + " found at index " + index);
} else {
    System.out.println(y + " not found");
}    
import java.util.Scanner;

public class Fibonacci {

    public static void main(String[] arguments) {
        int n1 = 0, n2 = 1, n3;

        @SuppressWarnings("resource")
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = s.nextInt();
        for (int i = 0; i < n; ++i) {   
            n3 = n1 + n2;

            n1 = n2;
            n2 = n3;
        System.out.println(""+n3);
        }
    }
}