我正在尝试使用' for'来显示可被两个用户输入整数整除的数字。循环语句。例如,如果我输入5和30,我将获得" 5 10 15 30"的输出。到目前为止,我已经获得了非常基本的设置,但我已经卡在了这里。如何在循环语句中使用变量相互划分?
import java.util.Scanner;
public class practice4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N_small= 0, N_big = 0;
System.out.printf("Enter the first number: ");
N_small = in.nextInt();
System.out.printf("Enter the second number: ");
N_big = in.nextInt();
if (N_small < N_big) {
for (int i = N_small; i == N_big; i++){
//Issue here! ***
System.out.printf("The numbers are: %d\n", i);
}
}
}
}
示例输出以防止我不够清楚:
----------- Sample run 1:
Enter the first number: 5
Enter the second number: 30
The numbers are: 5 10 15 30
Bye
和
----------- Sample run 3:
Enter the first number: 7
Enter the second number: 25
The numbers are:
Bye.
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
如果第一个输入是5,第二个输入是30 输出为5 10 15 30(你递增(第一个输入)5) 因此,如果您输入10和25,则输出应为10 20 25递增(第一个输入)。 如果这是你要解释的,那么你的代码应该是这样的
Scanner in = new Scanner(System.in);
int N_small= 0, N_big = 0 ,i;
System.out.printf("Enter the first number: ");
N_small = in.nextInt();
System.out.printf("Enter the second number: ");
N_big = in.nextInt();
if (N_small < N_big) {
System.out.printf("The numbers are:");
for (i = N_small; i < N_big+1 ; i=i+N_small){
if(i > N_big) System.out.println(N_big); else System.out.println(i);
}
}
}
&#13;