Java 程序:for循环

原文:https://www.studytonight.com/java-programs/java-for-loop-programs

在本教程中,我们将学习如何在不同的场景中实现 for 循环。但是在继续之前,如果你不熟悉 for 循环的概念,那么一定要查看 Java 中 Loops 的文章

程序 1: Java For 循环程序

在这个程序中,我们将看到如何在 java 中实现 for 循环程序。在这里,我们将考虑一个场景,我们将找到一个特定数字的乘法表。我们将为每个元素使用 for 循环,而不是为每个元素编写乘法表。我们将编写一次语句,并多次实现。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 宣布一个数字
  4. 要求用户初始化数字。
  5. 使用 for 循环打印该数字的乘法表。
  6. 显示结果。
  7. 停下来。

下面是 Java 中 for 循环的代码示例。

//Java Program to find the multiplication table of a given number
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter a number: ");
        int n=sc.nextInt();      //Declare and initialize the number
        System.out.println("The multiplication table of "+n+" is: ");
        //Print the multiplication table
        for(int i=1;i<=10;i++)
        {
            System.out.println(n+" * "+i+" = "+ (n*i));
        }
     }
}

输入一个数字:5 5 的乘法表为: 5 1 = 5 5 2 = 10 5 3 = 15 5 4 = 20 5 5 = 25 5 6 = 30 5 7 = 35 5 8 = 40 5 9 = 45 5 10 = 50

程序 2: Java For 循环程序

In this program, we will see how to implement a for each loop program in java.

算法

  1. 开始
  2. 创建扫描仪类的实例。
  3. 声明数组大小。
  4. 要求用户初始化数组大小。
  5. 声明一个数组。
  6. 要求用户初始化数组。
  7. 使用 for 循环初始化数组。
  8. 每个循环使用一个来打印数组元素。
  9. 打印数组元素。
  10. 停止。

下面是 Java 中 for 循环的代码示例。

//Java Program to see the implementation of for and for-each loop
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter the size of the array: ");
        int n=sc.nextInt();      //Declare and initialize the number
        int arr[]=new int[n];    //Declare an Array
        System.out.println("Enter the array elements: ");
        //Initialize the array        
        for(int i=0; i<n; i++)
        {
            arr[i]=sc.nextInt();
        }
        System.out.println("The array elements are: ");
        //Print the number infinite times
        for(int t:arr)
        {
            System.out.println(t);
        }
     }
}

输入数组大小:5 输入数组元素:1 2 3 4 5 数组元素为: 1 2 3 4 5

Java 程序:程序 3:实现循环

In this program, we will see how to implement an infinite for loop. A point to be noted here is that, in order to exit from an infinite loop, you need to press ctrl+c.

算法:

  1. 开始
  2. 创建扫描仪类的实例。
  3. 申报一个数字。
  4. 要求用户初始化数字。
  5. 使用 for 循环打印无限次。
  6. 在 for 循环中给出无限条件。
  7. 显示结果。
  8. 停止。

Below is the java code example for the for loop.

//Java Program to see the implementation of infinite for loop
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter a number: ");
        int n=sc.nextInt();      //Declare and initialize the number
        //Print the number infinite times
        for(int i=1;i<=10;i--)
        {
            System.out.println(i + " The entered number is "+n);
        }
     }

输入数字:5 1 输入的数字是 5 0 输入的数字是 5 -1 输入的数字是 5 -2 输入的数字是 5 -3 输入的数字是 5 -4 输入的数字是 5 ctrl+c

Java 程序:程序 4:实现循环

In this program, we will see how to print a number infinite times using a for loop. A point to be noted here is that, in order to exit from an infinite loop, you need to press ctrl+c.

算法:

  1. 开始
  2. 创建扫描仪类的实例。
  3. 申报一个数字。
  4. 要求用户初始化数字。
  5. 使用 for 循环打印无限次。
  6. 在 for 循环中给出无限条件。
  7. 显示结果。
  8. 停止。

Below is the code for the same.

//Java Program to see the implementation of infinite for loop
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter the number: ");
        int n=sc.nextInt();      //Declare and initialize the number
        //Infinite Loop Example      
        for(; ;)
        {
            System.out.println("Number is "+n);
        }

     }
}

输入数字:3 数字是 3 数字是 3 数字是 3 数字是 3 数字是 3 数字是 3 数字是 3 数字是 3 数字是 3 ctrl+c