3.循环结构

2018-01-02  本文已影响0人  唐英钏

while循环结构

练习题

String input = "n";
        while(input.equals("n"))
        {
            System.out.println("合格了吗?(y/n):");
            Scanner scanner = new Scanner(System.in);
            //接收用户输入
            input = scanner.next();
            if(input.equals("n"))//说明不合格
            {
                System.out.println("商务阅读教材!下午上级编程!");
            }
        }
        System.out.println("完成学习任务!");

do-while循环结构

for循环结构

练习题

public class linshi {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入学生姓名");
        String name = scanner.next();
        int grade = 0;
        int total = 0;
        for(int i = 1; i <= 5; i++)
        {
            System.out.println("请输入5门功课中第"+i+"门课的成绩");
            score = scanner.nextInt();
            total = total + grade;
        }
        System.out.println(name+"的平均分是"+total/5);
    }
}
public class lingshi {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个值");
        int num = scanner.nextInt();//6
        int j = num;
        for(int i = 0; i <= num; i++)
        {
            System.out.println(i+"+"+j+"="+num);
            j--;
        }

    }
}

break

练习题

public class linshi {
    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        int sum = 0;
        int grade = 0;
        for (int i = 1; i <= 5;i++)
        {
            System.out.print("请输入第" + i + "门课的成绩:");
            grade = scanner.nextInt();
            if (grade < 0 || grade > 100) {
                System.out.println("抱歉,分数录入错误,请重新输入!");
                break;
            }
            sum += grade;
        }
        if (grade >= 0 && grade <= 100){
            System.out.print(name + "平均成绩是" + sum/5);
        }
        else {
        }
    }
}

2.老师的方法(运用boolean)

public class linshi {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        int sum = 0;
        int grade = 0;
        boolean error = false;//假设用户输入没有错误
        for (int i = 1; i <= 5;i++)
        {
            System.out.print("请输入第" + i + "门课的成绩:");
            grade = scanner.nextInt();
            if (grade < 0 || grade > 100) {
                error = true;
                break;
            }
            sum += grade;
        }
        if (error == false){
            System.out.print(name + "平均成绩是" + sum/5);
        }
        else {
            System.out.println("录入有错误,请重新输入");
        }
    }
}
public class linshi {
    public static void main(String[] args) {
        int sum = 0;
        int i = 1;
        for ( i = 1; i<=10;i++ )
        {
            sum += i;
            if (sum > 20)
            {
                break;
            }
        }
        System.out.println(i);
    }
}

continue

练习题

public class linshi {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System .in);
        System.out.print("请输入班级总人数:");
        int number = scanner.nextInt();
        int count = 0;
        for (int i = 1;i <= number;i++)
        {
            System.out.print("请输入第" + i + "位学生的成绩:");
            int grade = scanner.nextInt();
            if (grade < 80)
            {
                continue;
            }
            else
            {
                count++;
            }
        }
        System.out.println("80分以上的学生人数是:" + count);
        System.out.println("80分以上的学生所占的比例是:" + (double)count/number);
    }
}
public class linshi {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1;i <= 10;i++)
        {
            if(i%2 == 1)
            {
                continue;
            }
            else
            {
                sum += i;
            }
        }
        System.out.println("偶数和:" + sum);
    }
}

break和continue小结

上一篇下一篇

猜你喜欢

热点阅读