第三章流程控制学习

2018-11-18  本文已影响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);
}
}

上一篇下一篇

猜你喜欢

热点阅读