简体   繁体   English

如何在Java中打印给定变量的倍数?

[英]How to print multiples of a given variable in java?

There are 3 numbers that are given to the user. 有3个数字提供给用户。 These three numbers define the multiple, and the range. 这三个数字定义了倍数和范围。 For example if these three numbers are (3, 6 , 17) the program should print 6,9,12,15. 例如,如果这三个数字分别是(3,6,17),则程序应打印6,9,12,15。 The first number is the base multiple and the second and third numbers are the lowest and the highest numbers (the range). 第一个数字是基数,第二个和第三个数字是最低和最高数字(范围)。 I also know that I do not need all the import statements that I have. 我也知道我不需要所有的import语句。

This is what I have so far but I'm not sure how to continue. 这是我到目前为止的内容,但我不确定如何继续。

import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.Math.*;

public class printMultiplesOf{
    public static void main (String [] args){

        Scanner reader = new Scanner(System.in);
        int num1, num2, num3;

        System.out.println("Enter the 1st number");
        num1 = reader.nextInt();

        System.out.println("Enter the 2nd number");
        num2 = reader.nextInt();

        System.out.println("Enter the 3rd number");
        num3 = reader.nextInt();

        printMultiplesOf(num1, num2, num3);
    }

    public static void printMultiplesOf(int num1, int num2, int num3){
        int start = num2
        int end = num3
        for (int i = num1; i <= num1; i++){
            System.out.println(i + " ");
        }
    }
} 

Loop through all the numbers in the range, and check if it's a multiple. 遍历范围内的所有数字,并检查其是否为倍数。

 public static void printMultiplesOf(int num1, int num2, int num3) {

        for(int i=num2; i<= num3; i++){
            if(i % num1 == 0)
                System.out.print(i +" ");
        }

    }

The main issue here is finding the loop's start and end point. 这里的主要问题是找到循环的起点和终点。 The start point needs to the first number divisible by num1 equal or larger than num2 . 起点必须为第一个数字,该数字可被num1整除或等于num2 You can find it by dividing num2/num1 using floating-point division, ceil the result and multiply it back by num1 . 您可以通过将找到它num2/num1使用浮点除法, ceil结果,然后乘回来num1 Similarly, the loop's end point should be the largest number divisible by num1 which is less than or equal to num3 . 同样,循环的终点应为最大数字,可被num1整除,该数字小于或等于num3 You can find it by dividing num3/num1 using integer division (which would effectively floor the result and then multiplying it back again by num1 . From there on, it's just a matter of looping in steps the size of num1 . Eg: 您可以通过使用整数除法对num3/num1进行除法来找到它(这将有效floor累加结果,然后将其再次乘以num1 。从那以后,这只是逐步循环执行num1大小的问题。例如:

public static void printMultiplesOf(int num1, int num2, int num3) {
    int start = ((int) Math.ceil((double) num2 / num1)) * num1;
    int end = (num3 / num1) * num1;

    for (int i = start; i <= end; i+= num1) {
        System.out.println (i + " ");
    }
}

This is your function. 这是你的职责。 You simply add your num1 to i each time loop is executed. 您只需在每次执行循环时将num1添加到i中即可。 I tried it and it is working ;) 我试过了,它正在工作;)

 public static void printMultiplesOf(int num1, int num2, int num3){
          int multi = num1;
          int start = num2;
          int end = num3;

          for (int i = start; i <= end; i += multi){
               System.out.println(i + " ");
          }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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