将for循环改为while循环。

huangapple go评论67阅读模式
英文:

Change for-loop to while loop

问题

如何将for循环改成while循环?

i = 0;
while (i < arr.length) {
    if (arr[i] == k) {
        found = true;
        break;
    }
    i++;
}

代码:

import java.util.Scanner;

class Algorithmus {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Geben sie den gesuchten Wert ein:");
        int k = s.nextInt();
        System.out.println("Geben sie die Länge des Arrays ein:");
        int n = s.nextInt();
        int arr[] = new int[n];
        int i = 0;
        System.out.println("Geben sie die Werte ein:");
        for (i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        boolean found = false;
        i = 0;
        while (i < arr.length) {
            if (arr[i] == k) {
                found = true;
                break;
            }
            i++;
        }
        if (found == true) {
            System.out.println((i + 1));
        } else {
            System.out.println((arr.length + 1));
        }
    }
}

结构应为:

BEGIN
Input: k1, ..., kn, k;
found := FALSE;
WHILE NOT found AND "not all elements were viewed" DO
"take the next element ki";
found := (ki = k)
END (* WHILE *);
IF found THEN
Output: i
ELSE
Output: n + 1
END (* IF *);
END
英文:

How to change the for-loop to a while loop?

for (i = 0; i &lt; arr.length; i++) {
            if (arr[i] == k) {
                found = true;
                break;
            }

        }

Code:

import java.util.Scanner;

class Algorithmus {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println(&quot;Geben sie den gesuchten Wert ein: &quot;);
		int k = s.nextInt();
		System.out.println(&quot;Geben sie die L&#228;nge des Arrays ein: &quot;);
		int n = s.nextInt();
		int arr[] = new int[n];
		int i = 0;
		System.out.println(&quot;Geben sie die Werte ein:&quot;);
		for(i = 0;i&lt;n;i++){
            arr[i] = s.nextInt();
		}
		boolean found = false;
		for (i = 0; i &lt; arr.length; i++) {
			if (arr[i] == k) {
				found = true;
				break;
			}
			
		}
		if (found==true) {
			System.out.println((i+1));
		} else {
			System.out.println((arr.length + 1));
		}
	}
}

The structure should be:

BEGIN
Input: k1, ..., kn, k;
found := FALSE;
WHILE NOT found AND „not all elements were viewed“ DO
„take the next element ki“;
found := (ki = k)
END (* WHILE *);
IF found THEN
Output: i
ELSE
OUtput: n + 1
END (* IF *)
END

答案1

得分: 0

以下是翻译好的内容:

等效的代码如下:

    int i = 0;

    while (i < arr.length) {
        if (arr[i] == k) {
            found = true;
            break;
        }
        ++i;
    }
英文:

Something equivalent would be:

int i = 0;

while (i &lt; arr.length) {
    if (arr[i] == k) {
        found = true;
        break;
    }
    ++i;
}

答案2

得分: 0

可以这样做:

int idx = 0;
while(idx < arr.length && a[idx] != k) {
    idx++;
}

if(idx == arr.length) {
    // 没找到元素,做一些操作
} else {
    // 找到元素
}
英文:

You can do something like this:

int idx = 0;
while(idx &lt; arr.length &amp;&amp; a[idx] != k) {
    idx++
}

if(idx == arr.length) {
    // element not found, do something
} else {
    // element found
}

答案3

得分: 0

尝试一下

int i = 0;
while(arr.length > i){
//代码
++i;
}
英文:

Try this

int i = 0;
while(arr.length &gt; i){
//code
++i;
}

答案4

得分: 0

int i = 0;
while (i < arr.length) {
    if (arr[i] == k) {
        found = true;
        break;
    }
    i++;
}
英文:
int i=0;
while (i &lt; arr.length) {
    if (arr[i] == k) {
        found = true;
        break;
    }
    i++;
}

答案5

得分: -1

尝试这段代码

import java.util.Scanner;

public class Algorithmus {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入要查找的值:");
        int k = s.nextInt();
        System.out.println("请输入数组的长度:");
        int n = s.nextInt();
        int arr[] = new int[n];
        int i = 0;
        System.out.println("请输入值:");
        for (i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        boolean found = false;
        i = 0;
        while (i < arr.length) {
            if (arr[i] == k) {
                found = true;
                break;
            }
            i++;
        }
        if (found == true) {
            System.out.println((i + 1));
        } else {
            System.out.println((arr.length + 1));
        }
    }
}
英文:

try this code

import java.util.Scanner;

public class Algorithmus  {
	    public static void main(String[] args) {
	        Scanner s = new Scanner(System.in);
	        System.out.println(&quot;Geben sie den gesuchten Wert ein: &quot;);
	        int k = s.nextInt();
	        System.out.println(&quot;Geben sie die L&#228;nge des Arrays ein: &quot;);
	        int n = s.nextInt();
	        int arr[] = new int[n];
	        int i = 0;
	        System.out.println(&quot;Geben sie die Werte ein:&quot;);
	        for(i = 0;i&lt;n;i++){
	            arr[i] = s.nextInt();
	        }
	        boolean found = false;
	        i=0;
	        while (i &lt; arr.length) {
	        	
	            if (arr[i] == k) {
	                found = true;
	                break;
	            }
	        i++;

	        }

	        
	        if (found==true) {
	            System.out.println((i+1));
	        } else {
	            System.out.println((arr.length + 1));
	        }
	    }
	}

huangapple
  • 本文由 发表于 2020年5月30日 13:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定