Java数字序列循环
大家好,我遇到了无限循环问题,我不知道我的代码有什么问题,我正在尝试制作一个数字序列格式,它在底部,我想问题是在我的条件下Java数字序列循环,java,for-loop,infinite-loop,Java,For Loop,Infinite Loop,大家好,我遇到了无限循环问题,我不知道我的代码有什么问题,我正在尝试制作一个数字序列格式,它在底部,我想问题是在我的条件下 import java.util.Scanner; public class tester { public static void main(String[] args) { Scanner x = new Scanner(System.in); int n; System.out.print
import java.util.Scanner;
public class tester {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int n;
System.out.print("Enter how many numbers to display");
n = x.nextInt();
while(n!=0) { //is this right?
for ( int i = 0; i<=n; i++) {
if(i%2==0) {
n += 2;
System.out.print(n);
} else {
n += 3;
System.out.print(n);
}
}
}
}
}
问题在这里:
while(n!=0)
和这里:for(int i=0;i使用“if”条件代替“while”循环两个问题:
int stop = n; // declare one local var to stop the for loop
if (n != 0) { //switch to if condition
for (int i = 0; i <= stop; i++) {
//loop's exit condition wasn't met because 'n' was also being incremented
if (i % 2 == 0) {
n += 2;
System.out.print(n+" ");
} else {
n += 3;
System.out.print(n+" ");
}
}
}
int stop=n;//声明一个本地变量以停止for循环
如果(n!=0){//切换到if条件
对于(int i=0;i您必须将while循环
替换为if条件,如下所示:
import java.util.Scanner;
public class tester {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int n;
System.out.print("Enter how many numbers to display");
n = x.nextInt();
int stop = n;
if(n!=0) { //if statement checks if n!=0
for ( int i = 0; i<=stop; i++) {
//stop replaces n because n is incremented in your for-loop
if(i%2==0) {
n += 2;
System.out.print(n);
} else {
n += 3;
System.out.print(n);
}
}
}
}
}
import java.util.Scanner;
公共类测试员{
公共静态void main(字符串[]args){
扫描器x=新扫描器(System.in);
int n;
System.out.print(“输入要显示的数字”);
n=x.nextInt();
int-stop=n;
if(n!=0){//if语句检查n!=0
对于(int i=0;i根据您的答案,我找到了一个有效的解决方案:
int n;
System.out.print("Enter how many numbers to display");
n = x.nextInt();
int k = -2; // so that it starts with 1 when i add +3
int stop = n-1;
if(n!=0) {
for ( int i = 0; i<=stop; i++) {
if(i%2==0) {
k += 3;
System.out.print(k+" ");
} else {
k += 2;
System.out.print(k+" ");
}
}
}
intn;
System.out.print(“输入要显示的数字”);
n=x.nextInt();
int k=-2;//所以当我加+3时,它以1开头
int stop=n-1;
如果(n!=0){
对于(int i=0;istill给出了一个无限循环,我认为这是if-else语句中我的条件?Ya wright,您正在更改循环中的“n”值,这也是无限循环的原因。在“for”循环中,“i”是增量一步,同时“n”是增量2或3步。最好使用另一个局部变量,而不是更改“n”va你是否也改变了for循环?它不应该无限循环当你把最终答案作为答案发布时,一定要把其他答案中的一个标记为已接受,无论哪个答案对你的答案最有帮助。好吧,然后把你的投票放在上面:D我的答案也应该去掉无限循环。。。
import java.util.Scanner;
public class tester {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int n;
System.out.print("Enter how many numbers to display");
n = x.nextInt();
int stop = n;
if(n!=0) { //if statement checks if n!=0
for ( int i = 0; i<=stop; i++) {
//stop replaces n because n is incremented in your for-loop
if(i%2==0) {
n += 2;
System.out.print(n);
} else {
n += 3;
System.out.print(n);
}
}
}
}
}
int n;
System.out.print("Enter how many numbers to display");
n = x.nextInt();
int k = -2; // so that it starts with 1 when i add +3
int stop = n-1;
if(n!=0) {
for ( int i = 0; i<=stop; i++) {
if(i%2==0) {
k += 3;
System.out.print(k+" ");
} else {
k += 2;
System.out.print(k+" ");
}
}
}