简体   繁体   中英

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 example, if I were to input 5 and 30, I would get an output of "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 ). 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); } } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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