英文:
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 < 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("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;
for (i = 0; i < 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 < 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 < arr.length && 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 > 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 < 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("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));
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论