[英]Using two integers to find the multiples inbetween them
我正在尝试使用'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.
非常感谢任何帮助,谢谢!
以及如果第一输入是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); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.