变量循环不循环
Variable loop not looping
package Example;
import java.util.Scanner;
public class solisxdddd {
public static void main(String[] args) {
Scanner Lenijs = new Scanner(System.in);
int a;
int b;
int c;
int solis = 0;
int skaitlis = 0;
do {
System.out.println("Input starting number: ");
a = Lenijs.nextInt();
System.out.println("Input end number: ");
b = Lenijs.nextInt();
System.out.println("Input increment: ");
c = Lenijs.nextInt();
solis = solis + c;
skaitlis = a + solis;
System.out.println("From A to B is: " + skaitlis );
} while (skaitlis <= b);
}
}
我的循环有问题。我正在制作一个程序,用户输入起始编号 (A) 和结束编号 (B),增量为 (C)。因此,如果用户输入 A=2 B=8 C=2,则程序输出 2,4,6,8。我可以让它输出 A 和 B,但它不会执行循环,它会输出 2;4;8 而不是 2;4;6;8,即使 while 设置为在 int skaitlis <= B 时停止所以它应该打印直到它到达数字 B,即 8。
您正在尝试读取您在循环内的输入。不要那样做。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input starting number: ");
int a = sc.nextInt();
System.out.println("Input end number: ");
int b = sc.nextInt();
System.out.println("Input increment: ");
int c = sc.nextInt();
System.out.println("From A to B is: ");
for (int i = a; i <= b; i += c) {
System.out.print(i + " ");
}
}
或
while (a <= b) {
System.out.print(a + " ");
a += c;
}
或
do {
System.out.print(a + " ");
a += c;
} while (a <= b);
package Example;
import java.util.Scanner;
public class solisxdddd {
public static void main(String[] args) {
Scanner Lenijs = new Scanner(System.in);
int a;
int b;
int c;
int solis = 0;
int skaitlis = 0;
do {
System.out.println("Input starting number: ");
a = Lenijs.nextInt();
System.out.println("Input end number: ");
b = Lenijs.nextInt();
System.out.println("Input increment: ");
c = Lenijs.nextInt();
solis = solis + c;
skaitlis = a + solis;
System.out.println("From A to B is: " + skaitlis );
} while (skaitlis <= b);
}
}
我的循环有问题。我正在制作一个程序,用户输入起始编号 (A) 和结束编号 (B),增量为 (C)。因此,如果用户输入 A=2 B=8 C=2,则程序输出 2,4,6,8。我可以让它输出 A 和 B,但它不会执行循环,它会输出 2;4;8 而不是 2;4;6;8,即使 while 设置为在 int skaitlis <= B 时停止所以它应该打印直到它到达数字 B,即 8。
您正在尝试读取您在循环内的输入。不要那样做。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input starting number: ");
int a = sc.nextInt();
System.out.println("Input end number: ");
int b = sc.nextInt();
System.out.println("Input increment: ");
int c = sc.nextInt();
System.out.println("From A to B is: ");
for (int i = a; i <= b; i += c) {
System.out.print(i + " ");
}
}
或
while (a <= b) {
System.out.print(a + " ");
a += c;
}
或
do {
System.out.print(a + " ");
a += c;
} while (a <= b);