美文网首页
第三章流程控制学习

第三章流程控制学习

作者: 黄敬理 | 来源:发表于2018-11-18 14:36 被阅读0次

    顺序语句

    image.png

    选择语句

    if结构

    image.png

    if...else结构

    image.png

    循环语句

    while结构

    do..while结构

    package java_study.week1;

    import java.util.Scanner;

    public class GuessNumber {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int counter = 0;
    int answer = (int) (Math.random() * 100 + 1);
    int yorAnswer;
    do {
    counter++;
    System.out.println("你要输入的数字");
    yorAnswer = input.nextInt();
    if (yorAnswer == answer) {
    System.out.println("正确");
    System.out.println("你猜了" + counter + "次");

            } else if (yorAnswer > answer) {
                System.out.println("大了");
            } else {
                System.out.println("小了");
            }
        }
            while (yorAnswer != answer) ;
        }
    }
    

    for结构

    package java_study.week3;

    public class For {
    /**
    * for循环
    */
    public static void main(String[] args) {
    int sun = 0;
    for (int i =1;i<=100; i++){
    sun += i;
    }
    System.out.println(sun);
    }
    }

    相关文章

      网友评论

          本文标题:第三章流程控制学习

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