简体   繁体   中英

A loop that prints even numbers?

我应该写一个代码,要求输入姓名,要求输入 1 到 10 之间的数字,然后将数字从 1 打印到用户输入的数字,除了每三个数字应该是在开头输入的用户名的程序。我的代码实现了这个目的。下一步是使用循环打印从 2 到用户号码的所有偶数。我不确定如何将其包含在我的代码中,甚至不确定我应该使用哪种循环。有人可以解释如何正确包含它吗?这是到目前为止的代码。

import java.util.Scanner;

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

        int number;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scan.nextLine();

        System.out.print("Please enter a number between 1 and 10: ");
        number = scan.nextInt();

        //asks for a number between one and ten until I get number within that range,
        while (number < 1 || number > 10) {
            System.out.print("No, between 1 and 10: ");
            number = scan.nextInt();
        }

        for (int i = 1; i <= number; i++) {
            if (i % 3 == 0) {
                System.out.print(name + " ");
            } else {
                System.out.print(i + " ");
            }
        }
    }
}

Running a loop that prints on every even number is no different then running a loop that prints on every third number. Simply use % 2 instead of % 3 :

for (int i = 2; i <= number; i++) {
    if (i % 2 = 0) {
        System.out.print(i + " ");
    }
}

Which can be re-written without using % at all:

for (int i = 2; i <= number; i += 2) {
    System.out.print(i + " ");
}
public static void main(String[]p){
   int limit=20;
   for(int i=2; i<=limit; i+=2)
   {
      System.out.println(i);
   }
}

Below code for your reference:

 public class NewClass1 {
     public static void main(String[] args) {
       int number;
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter your name: ");
       String name = scan.nextLine();

       System.out.print("Please enter a number between 1 and 10: ");
       number = scan.nextInt();

    //asks for a number between one and ten until I get number within that range,
      while (number < 1 || number > 10) {
          System.out.print("No, between 1 and 10: ");
          number = scan.nextInt();
      }

     for (int i = 1; i <= number; i++) {
         if (i % 3 == 0) {
             System.out.print(name + " ");
         }else {
             System.out.print(i + " ");
         }
     }
     System.out.println();
     for(int i =2; i<=number; i+=2)
         System.out.print(i + " ");
    }
 }
for (int i = 2; i <= number; i++) {
    if (i % 2 == 0) {
        System.out.print(i + " ");
    }
}

Here is a loop that goes up by even numbers.

for(int i=2;i<number;i+=2)
{
  System.out.println(i);
}

This is an example where you can print all the even numbers up to 100.

int number = 0;
while (number <= 100) {
  number += 1;

  if (number % 2 == 0) {
    System.out.println(number);
  }
}

Here is the solution with java 8 stream api and range function:

    System.out.print("Please enter a number between 1 and 10: ");
    Scanner scan = new Scanner(System.in);
    int number = scan.nextInt();
    IntStream stream = IntStream.rangeClosed(2, number);
    stream.filter( i -> i % 2 ==0).forEach( i -> System.out.format("%d\t",i));
for (int i = 2; i <= number; i++) {
    if (i % 2 = 0) {
        System.out.print(i + " ");
    }
}

The above is the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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