我想从java

时间:2018-02-08 09:31:03

标签: java

我编写了一个程序来打印一系列数字中的偶数(由用户输入),我希望程序做的是在一行中打印7个偶数,然后在第二行打印7个偶数,等等,直到达到指定的范围。

这是我编写的程序,但我想要输出如上所定义:

import java.util.Scanner;

class EvenNumbersScanner
{
    public static void main(String args[])
    {
        int number,a;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter the first number of range");
        a = s.nextInt();
        System.out.println("Enter the second numberof range");
        number = s.nextInt();

        System.out.println("Range is: "+a+" to "+number);

        for(int i=a;i<=number;i++)
        {
            if(i%2==0)
            {
                System.out.println("Even number is: "+i);

                }
            }
        }
    }

请告诉我在一行中获得7个偶数的代码,

4 个答案:

答案 0 :(得分:0)

您可以使用另一个变量来跟踪打印的偶数。

int count = 0;
for(int i=a;i<=number;i++)
{
     if(i%2==0)
    {
           count = count + 1;
           if (count%7==0) {
              System.out.println("Even number is: "+i);
           } else {
              System.out.print("Even number is: "+i);
           }
     }
 }

对于a = 1和number = 40,输出将如下:

Even number is: 2Even number is: 4Even number is: 6Even number is: 8Even number is: 10Even number is: 12Even number is: 14
Even number is: 16Even number is: 18Even number is: 20Even number is: 22Even number is: 24Even number is: 26Even number is: 28
Even number is: 30Even number is: 32Even number is: 34Even number is: 36Even number is: 38Even number is: 40

如果您只想打印偶数,

int count = 0;
    for(int i=1;i<=40;i++)
    {
        if(i%2==0)
        {
            count = count + 1;
            if (count%7==0) {
                System.out.println(" "+i);
            } else {
                System.out.print(" "+i);
            }
        }
    }

对于a = 1和number = 40,输出将如下:

2 4 6 8 10 12 14
16 18 20 22 24 26 28
30 32 34 36 38 40

答案 1 :(得分:0)

这对我有用:

import java.util.Scanner;

class EvenNumbersScanner
{
    public static void main(String args[])
    {
        int number,a, count;

    Scanner s = new Scanner(System.in);

    System.out.println("Enter the first number of range");
    a = s.nextInt();
    System.out.println("Enter the second numberof range");
    number = s.nextInt();
    count = 0;
    System.out.println("Range is: "+a+" to "+number);
    System.out.println("Even numbers are : ");

    for(int i=a;i<=number;i++)
    {
        if(i%2==0)
        {
            count++;
            System.out.print(i+", ");
            if(count >= 7)
            {
                count = 0;
                System.out.println("");
            }

        }
     }
  }
}

生成此输出以输入10100

Enter the first number of range
10
Enter the second numberof range
100
Range is: 10 to 100
Even numbers are : 
10, 12, 14, 16, 18, 20, 22, 
24, 26, 28, 30, 32, 34, 36, 
38, 40, 42, 44, 46, 48, 50, 
52, 54, 56, 58, 60, 62, 64, 
66, 68, 70, 72, 74, 76, 78, 
80, 82, 84, 86, 88, 90, 92, 
94, 96, 98, 100, 

答案 2 :(得分:0)

您可以放置​​一个计数器,检查它们是否最多为7,然后将内容移至下一行。试试这个:

public static void main(String[] args) {
    int number, a;

    Scanner s = new Scanner(System.in);

    System.out.println("Enter the first number of range");
    a = s.nextInt();
    System.out.println("Enter the second numberof range");
    number = s.nextInt();

    System.out.println("Range is: " + a + " to " + number);

    int count  = 0;//Changes here

    System.out.print("Even number is: ");//Changes here

    for (int i = a; i <= number; i++) {
        if (i % 2 == 0) {
            System.out.print(" "+i);
            count++; //Changes here

            if(count % 7 == 0){//Changes here
                System.out.println();//Changes here
                System.out.print("Even number is: ");//Changes here
            }//Changes here
        }
    }

}

然后你应该把它作为输出:

Enter the first number of range
1
Enter the second numberof range
50
Range is: 1 to 50
Even number is:  2 4 6 8 10 12 14
Even number is:  16 18 20 22 24 26 28
Even number is:  30 32 34 36 38 40 42
Even number is:  44 46 48 50

答案 3 :(得分:0)

这是Java 8 IntStream soltuion:

    int rangeStart = 1;
    int rangeEnd = 50;
    AtomicInteger count = new AtomicInteger(0);
    IntStream.range(rangeStart, rangeEnd)
        .filter(i->i%2==0)
        .peek(i->{if(count.getAndIncrement()%7==0)System.out.println("");})
        .forEach(i->System.out.print(" " + i));