2016-02-27 105 views
0

我想显示可以被两个用户输入整数使用'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

以及如果第一输入是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); 
 

 
    } 
 
    } 
 
    }

+0

它需要的第一个整数的倍数,且整除第二,因此,例如验证码打印5 10 15 20 25 30,但20和25不能被30整除,因此需要将它们裁剪掉,我相信通过使用模函数,但我并不完全确定。 – lana

+0

我想通了你的例子,谢谢。 – lana

相关问题