列出用户输入数字的倍数

时间:2014-08-02 08:56:20

标签: java loops numbers

任务是"编写一个程序,显示用户输入的整数的用户指示的倍数。"

我想我不需要一个完全直接的答案(虽然我确实想知道使用的方法/公式),因为我想用它作为学习经验,以便自己去完成和学习任务。我真的想知道过程和使用哪些方法,以及找到一个公式。 :||

我真的不确定如何编写显示用户输入的用户输入整数的代码。最难的部分似乎是编写循环公式。不知道从哪里开始。

到目前为止,我有:

import java.util.Scanner;

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

\\just stuff to base my code off of

      int integer;
      int numberMultiples;

      System.out.println("Enter an integer: ");

      integer = keyboard.nextInt();

      System.out.println("How many multiples of " + integer + " would you like to know?");
         numberMultiples = keyboard.nextInt();

      System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");

\\pretty much everything from here on out.. I'm not sure what to really do.
      int n = integer;
      int result = (integer * (numberMultiples));
      while (result > 0){}

      System.out.print(result);
   }
} \\at the moment this code doesn't seem to have any running errors

我真的不确定如何编写显示用户输入的用户输入整数的代码。最难的部分似乎是编写循环公式。不知道从哪里开始。

新问题 我也需要循环我的程序。 (首先向用户提问。)Mines不工作,因为它只是循环整数循环并且不允许我键入yes / no。     import java.util.Scanner;

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

       Scanner keyboard = new Scanner(System.in);
       int integer, numberMultiples;
       String repeat = "yes";

       while (repeat != "no")
       {
         System.out.println("Enter an integer: ");
         integer = keyboard.nextInt();

         System.out.println("How many multiples of " + integer + " would you like to know?");
         numberMultiples = keyboard.nextInt();

         System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");

         for (int i=1; i<=numberMultiples; i++){
         System.out.println(integer + " * " + i + "   =  " + i*integer );
         }


         System.out.println("Would you like to do this again? Enter yes or no: ");
         repeat = keyboard.nextLine();


   }  

} 

}

3 个答案:

答案 0 :(得分:0)

你想这样吗?以下是代码

package com.ge.cbm;


import java.util.Scanner;

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

//just stuff to base my code off of

      int integer;
      int firstEntered;
      int numberMultiples;

      System.out.println("Enter an integer: ");

      integer = keyboard.nextInt();
      firstEntered = integer;
      System.out.println("How many multiples of " + integer + " would you like to know?");
         numberMultiples = keyboard.nextInt();

      System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");

//pretty much everything from here on out.. I'm not sure what to really do.

     for (int i=0;i<numberMultiples;i++){
         integer=integer*firstEntered;
         System.out.println(integer);
     }
   }
}

输出:

Enter an integer: 
3
How many multiples of 3 would you like to know?
7
Listing the first 7 multiples of 3: 
9
27
81
243
729
2187
6561

答案 1 :(得分:0)

好的,所以你需要先了解问题才能解决问题

x = First input
n = Second input 

你需要计算x的x倍数     例如x = 3且n = 10     要计算3的10倍,我们需要做:

  1st multiple = x*1
  2nd multiple = x*2
  3rd multiple = x*3
  ...
  n   multiple = x*n

您可以注意到这些操作可以被一个for循环替换(注意每一行的第一个和最后一个字符,它可以是循环的索引)

回到java:)

for (int i=1; i<=numberMultiples; i++){
    System.out.println("Listing multiple N# " + i + "   =  "+ i*integer );
 }

使用以下内容替换您的代码并尝试以下代码:

import java.util.Scanner;

public class MultipleLooping{
    public static void main(String[] args){
        Scanner keyboard = new Scanner(System.in);
        int integer,numberMultiples;

        System.out.println("Enter an integer: ");
        integer = keyboard.nextInt();

        System.out.println("How many multiples of " + integer + " would you like to know?");
        numberMultiples = keyboard.nextInt();

        for (int i=1; i<=numberMultiples; i++){
            System.out.println("Listing multiple N# " + i + "   =  "+ i*integer );
        }
    }
}


Enter an integer: 
3
How many multiples of 3 would you like to know?
7
Listing multiple N# 1   =  3
Listing multiple N# 2   =  6
Listing multiple N# 3   =  9
Listing multiple N# 4   =  12
Listing multiple N# 5   =  15
Listing multiple N# 6   =  18
Listing multiple N# 7   =  21

答案 2 :(得分:0)

这应该有用

while(repeat.equals("yes"))
{
     System.out.println("Enter an integer: ");
     integer = keyboard.nextInt();

     System.out.println("How many multiples of " + integer + " would you like to know?");
     numberMultiples = keyboard.nextInt();

     System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");

     for (int i=1; i<=numberMultiples; i++)
     {
        System.out.println(integer + " * " + i + "   =  " + i*integer );
     }


     System.out.println("Would you like to do this again? Enter yes or no: ");
     repeat = keyboard.nextLine();
     repeat = keyboard.nextLine();
}