在java问题中执行倍数的数字与实现

时间:2016-02-25 22:27:14

标签: java

我不确定如何制作此代码..至少这部分内容。这是我现在的代码,我理解它背后的数学。我只是不确定如何实现它。我会使用循环吗?这是我现在的代码。我知道它不正确,但它是它的开始。

public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
    System.out.println("what number would you like me to multiply: ");
    int number = in.nextInt();
    System.out.println("how many multiples of "+number+" would you like to see: ");
    int multiple = in.nextInt();
    int total = 
    System.out.println("total :"+total);
  }

这是我想要的输出:

 What number would you like me to multiply? 4
 How many multiples of 4 would you like to see? 7
 The first 7 multiples of 4 are: 4, 8, 12, 16, 20, 24, 28    

2 个答案:

答案 0 :(得分:1)

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("what number would you like me to multiply: ");
    int number = in.nextInt();
    System.out.println("how many multiples of "+number+" would you like to see: ");
    int multiple = in.nextInt();
    System.out.print("The first " + multiple + " multiples of " + number + " are: ");
    for(int i=1; i<=multiple; i++){
        if(i>1){
            System.out.print(", ");
        }
        System.out.print(number*i);
    }
}

答案 1 :(得分:0)

不适用于此网站,但是:

String output = "The first " + multiples + " multiples of " + number + " are: " 
for(int x = 1; x <= multiples; x++){
   output = output + Integer.toString(x*number) + ","; 
}

这可以使用String.format进行改进,但生病了。 您的代码也可以通过执行错误检查来改进,因为现在您可以说&#39; a&#39;对于输入而言它会崩溃。

你应该查阅编程的基础知识,并且只是尝试一下你只是做随机事情所学到的东西。如果您需要有关尝试的内容的建议,请https://projecteuler.net/