简体   繁体   English

使用两个整数查找它们之间的倍数

[英]Using two integers to find the multiples inbetween them

I am trying to display numbers divisible by two user-input integers using a 'for' loop statement. 我正在尝试使用'for'循环语句显示可被两个用户输入整数整除的数字。 For example, if I were to input 5 and 30, I would get an output of "5 10 15 30". 例如,如果我输入5和30,则输出将为“ 5 10 15 30”。 I've got the very basic setup so far, but I'm stuck right here. 到目前为止,我已经完成了非常基本的设置,但是我被困在这里。 How can I use the variables to divide by each other within the loop statement? 如何在循环语句中使用变量彼此除法?

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);  

    }   
   }
  }
}

An example output in-case I'm not clear enough: 一个示例输出,以防万一我还不够清楚:

----------- Sample run 1:

Enter the first number: 5
Enter the second number: 30
The numbers are:  5 10 15 30  
Bye

and

----------- Sample run 3:

Enter the first number: 7
Enter the second number: 25
The numbers are:
Bye.

Any help is greatly appreciated, thanks! 非常感谢任何帮助,谢谢!

well if the first input is 5 and the second is 30 and the output is 5 10 15 30 (you are incrementing by ( the first input )5 ) so if you input 10 and 25 the output should be 10 20 25 incrementing by ( the first input ). 以及如果第一输入是5,第二个是30,输出是5 10 15 30(你被( 第一输入 )递增5)因此,如果输入端10和25的输出应当由( 是10 20 25递增首先输入 )。 if this is what you are trying to explain then your code should look like this 如果这是您要解释的内容,则您的代码应如下所示

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM