Java 4_Loops

作者: 綿綿_ | 来源:发表于2018-09-02 15:45 被阅读0次

While loop

import java.util.Scanner; // Library that allows us to capture user input

public class LessonFour {
    
    // Creates a Scanner object that monitors keyboard input
    static Scanner userInput = new Scanner(System.in);
    
    public static void main(String[] args)
    {
        /*
         * The while loop : A while loop executes the code between {} until the 
          condition is no longer true
         */
        
        int i = 1;
        
        while(i < 20)
        {
            // Use continue to skip over printing 3
            if(i == 3)
            {
                i+=2; // When using continue, don't forget to change the iterator
                continue; // continue skips a loop iteration, but not the loop
            }
            
            System.out.println(i);
            i++;
            
            // Just print odd numbers
            if((i%2) == 0)
            {
                i++;
            }
            
            // i is greater than 10 jump out of the loop
            if(i > 10)
            {
                break; // Forces the program to leave the loop prematurely
            }
        }
}

Code that calculates the value for PI

     ```
    double myPi = 4.0; // My starting value for pi
    
    // Starting value for my loop iterator in the loop
    double j = 3.0; 
    
    // The while loop
    while(j<11)
    {
        // I calculate pi with this formula
        // PI = 4 - 4/3 + 4/5 - 4/7...
        myPi = myPi - (4/j) + (4/(j+2));
        j += 4;
        System.out.println(myPi);
    }

Execute while loop until the user quits it


        String contYorN = "Y";
        int h = 1;
        
        // equalsIgnoreCase checks if the input string is equal to y or Y
        while (contYorN.equalsIgnoreCase("y"))
        {
            System.out.println(h);
            System.out.print("Continue y or n?");
            contYorN = userInput.nextLine(); // Accepts a string input from the user
            h++;
            
        }

The do while loop

        int k = 1;
        
        do 
        {
            System.out.println(k);
            k++;
        } while (k <= 10); // Counts from 1 to 10

The for loop :

        for (int l=10; l >= 1; l--)
        {
            System.out.println(l);
        }
        
        for (m=1, n=2; m <= 9; m+=2, n+=2)
        {
            System.out.println(m + "\n" + n);
        }

Enhanced For Loop

int a[]={1,2,3,4,5}
sum=0;
for(int x: a){  //enhanced for loop just need the type 
                //the identifier and the array u wanna working through
sum+=a;
}
System.out.println(sum);

相关文章

  • Java 4_Loops

    While loop Code that calculates the value for PI Execute ...

  • Java(JavaEE)学习线路图1

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • Java学习线路图

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 大数据学习线路图

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 大数据学习教程

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 一篇文章,全面解读Android面试知识点

    Java Java基础 Java集合框架 Java集合——ArrayList Java集合——LinkedList...

  • java学习路线

    javaSE java基础语法 java文件操作 java网络操作 java多线程 java数据库操作 java ...

  • java编程分类

    一、java简介java开发环境配置java基础语法java对象与类java基本数据类型java变量类型java修...

  • Java-01初识Java

    Java的3大版本 Java有三大版本,Java SE, Java ME, Java EE Java SE(Jav...

  • JAVA循环语句(四)9.29

    Java条件 if Java条件if...else Java条件多重if Java条件语句 嵌套if Java条件...

网友评论

    本文标题:Java 4_Loops

    本文链接:https://www.haomeiwen.com/subject/rijmwftx.html