创建一个java程序来显示一个数字的倍数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32877071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:57:28  来源:igfitidea点击:

Create a java program to display multiples of a number

java

提问by Kyle

I'm trying to Write the main method of a Java program that has the user enter two integers, i and n. If either integer is less than 2, output “Please enter numbers above 1.” Otherwise, output the n positive multiples of i, separated by spaces.

我正在尝试编写一个让用户输入两个整数 i 和 n 的 Java 程序的主要方法。如果任一整数小于 2,则输出“请输入大于 1 的数字”。否则,输出 i 的 n 个正倍数,以空格分隔。

I'm close but can't figure out how to do display the multiples.

我很接近,但不知道如何显示倍数。

Here's what a sample run should look like:

以下是示例运行的样子:

Enter i: 4

输入我:4

Enter n: 6

输入 n:6

6 multiples of 4 are: 8 12 16 20 24 28

4 的 6 倍数是:8 12 16 20 24 28

import java.util.*;
public class HW5Problem3 {
   public static void main (String [] args) {

      int i = 0;
      int n = 0;

      Scanner input = new Scanner(System.in);

         System.out.print("Enter i: ");
         i = input.nextInt();
         System.out.print("Enter n: ");
         n = input.nextInt();

         if ((i <= 1) || (n <= 1)) {
            System.out.println("Please enter numbers above 1");
            System.exit(1);
         }

         System.out.print(n + " multiples of " + i + " are: ");

   }
}

采纳答案by Cedric Achi

import java.util.Scanner;

public static void main(String[] args){
  Scanner input = new Scanner(System.in);

  int i = 0;
  int n = 0;

  //It's use to verify the inputs (i and n).
  do{

    System.out.print("Enter i :");
    i = input.nextInt();

    System.out.print("\nEnter n :");
    n = input.nextInt();

    if(i >= 1 || n <= 1){
      System.out.println("Please enter numbers above 1 \n");
    }    

  }while(i <= 1 || n <= 1);

  System.out.print(n + " multiples of " + i + " are: ");

  for (int counter = 0 ; counter < n ; counter++) {
    System.out.print(i*(2 + counter) + " ");
  }

}

回答by Paul Ostrowski

You'll need to create a loop (for loop or while loop) to iterate from 2 to n+1, and multiply i by your loop variable, outputting each value inside the loop

您需要创建一个循环(for 循环或 while 循环)以从 2 迭代到 n+1,并将 i 乘以循环变量,输出循环内的每个值

回答by Würgspa?

nis the multiplier, iis the factor, right? In programming the multiplier is the loop maximum:

n是乘数,i是因数,对吗?在编程中,乘数是循环最大值:

System.out.print(n + " multiples of " + i + " are: ");
for (int inc=1; inc<=n; inc++) {
    System.out.print(" " + i*inc);
}

This prints out: 4 8 12 16 20 24

这打印出来:4 8 12 16 20 24

If you really want to have this as output: 8 12 16 20 24 28 copy/paste this line:

如果你真的想把它作为输出: 8 12 16 20 24 28 复制/粘贴这一行:

for (int inc=2; inc<=(n+1); inc++)

回答by sherl

U can use following method in that class

您可以在该类中使用以下方法

   public static void mult(int i,int n){
   int[] arr=new int[n];
   int count=2;
   for(int x=0;x<n;x++){
       arr[x]=i*count++;
   }
   for(int y=0;y<arr.length;y++){
   System.out.print(arr[y]+" ");

}

}

and now your final code looks like

现在你的最终代码看起来像

import java.util.*;

public class HW5Problem3 {
    private int i = 0;
    private int n = 0;

    public static void main(String[] args) {

        int i = 0;
        int n = 0;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter i: ");
        i = input.nextInt();
        System.out.print("Enter n: ");
        n = input.nextInt();

        if ((i <= 1) || (n <= 1)) {
            System.out.println("Please enter numbers above 1");
            System.exit(1);
        } else {
            System.out.print(n + " multiples of " + i + " are: ");
            mult(i, n);
        }

    }

    public static void mult(int i, int n) {
        int[] arr = new int[n];
        int count = 2;
        for (int x = 0; x < n; x++) {
            arr[x] = i * count++;
        }
        for (int y = 0; y < arr.length; y++) {
            System.out.print(arr[y] + " ");
        }
    }
}