Java基础循环while/do...while

2022-08-01,,

循环(一)while/do...while

1.输出1~100的所有数字。

public class Test01 {
    //1.输出1~100的所有数字。
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while (i <= 100) {
            sum = i;
            i++;
            System.out.println(sum);
        }
    }
}

2.输出1900~2020年所有的闰年和平年。
 

import java.util.Scanner;

public class Shangji2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 1990;
        int  = 0;
        while (year < 2020) {
            year++;
            a = year;
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                System.out.println(a + "是闰年");
            } else {
                System.out.println(a + "是平年");
            }
        }
    }
}

3.输出1~1000里所有的水仙花数。
水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

import java.util.Scanner;

public class Flauler {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int i=1;
        while (i<1000){
            int bai = i / 100;
            int shi = i / 10 % 10;
            int ge = i % 10;
            if(bai*bai*bai+shi*shi*shi+ge*ge*ge==i){
                System.out.println(i);
            }

            i++;
        }
    }
}

4.输出100~999内的个位数是十位数的2倍,十位数是百位数的3倍的数。

import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        //循环初始化
        int a = 100;
        //循环条件
        while (a<1000){
            int b =a/100;
            int s =a%100/10;
            int g =a%100%10;
            if (g==s*2 && s==b*3){
                System.out.println("这个三位数的值:"+a);
            }
            a++;
        }

    }
}

6.猜数游戏
    直到猜对为止。
        算出猜了多少次。
        一次就猜中:说牛逼。
        二次、三次猜中:说大神。
        三次到十次猜中:说菜鸟。
        超过十次说:唉呀,我的老哥呀,太累了。
    
        问:你还要继续再玩一次吗?y/Y:

package student;

import java.util.Scanner;
import java.util.Random;

public class Kehou1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int b = 1;
        String c = "y";

        do {
            Random r = new Random();
            int pc = r.nextInt(101);
            System.out.println(pc);
            System.out.println("请随机输入一个两位数");
            double a = input.nextDouble();
            while (a != pc) {
                b++;
                if (a > pc) {
                    System.out.println("您的数字大了");
                } else {
                    System.out.println("您的数字小了");
                }
                System.out.println("再猜:");
                a = input.nextInt();
            }
            if (a == pc) {
                System.out.println("您猜对了");
                System.out.println("你猜了:" + b + "次");
                if (b == 1) {
                    System.out.println("牛逼");
                } else if (b == 2 || b == 3) {
                    System.out.println("大神");
                } else if (b > 3 && b <= 10) {
                    System.out.println("菜鸟");
                } else {
                    System.out.println("哎呀,我的老哥啊,太累了");
                }
            }
            System.out.println("是否继续y/n");
            c = input.next();
        } while ("y".equals(c));
        System.out.println("不玩了溜了");

    }
}

7.猜拳游戏
  1.在原有的基础之上,增加功能
    
    1)每玩一局要提示:“你出的是石头,电脑出的是布,你输了,电脑赢了”。类似的结果。
    2)你还要继续玩游戏吗?如果用户输入"y"或"Y"就继续,否则就退出。
    3)当玩了几把,不想玩了,最后要有个英雄榜

======■游戏进行了5次■======

     ☆英雄榜☆

    对战方    赢    输
    电脑      3      2
    玩家      1      1
    平手      1      1
==============================
 

import java.util.Random;
import java.util.Scanner;
//张俊生
public class Zuoye6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random r = new Random();
        int num = 0;
        int win = 0;
        int lose = 0;
        int ping = 0;
        int cuo = 0;
        while (true) {
            num++;
            int pc = r.nextInt(3) + 1;
            String pc1 = "";
            switch (pc) {
                case 1:
                    pc1 = "石头";
                    break;
                case 2:
                    pc1 = "剪刀";
                    break;
                case 3:
                    pc1 = "布";
                    break;
            }
            System.out.println("请出拳:1:石头\t2:剪刀\t3:布");
            int me = input.nextInt();
            String me1 = "";
            switch (me) {
                case 1:
                    me1 = "石头";
                    break;
                case 2:
                    me1 = "剪刀";
                    break;
                case 3:
                    me1 = "布";
                    break;
            }
            if (me == pc) {
                ping++;
                System.out.println("你出的是:" + me1 + " 电脑出的也是:" + pc1 + " 所以是:平局");
            } else if (me == 1 && pc == 2 || me == 2 && pc == 3 || me == 3 && pc == 1) {
                win++;
                System.out.println("你出的是:" + me1 + " 电脑出的是:" + pc1 + " 所以是:你赢了");
            } else if (me == 1 && pc == 3 || me == 3 && pc == 2 || me == 2 && pc == 1) {
                lose++;
                System.out.println("你出的是:" + me1 + " 电脑出的是:" + pc1 + " 所以是:你输了");
            } else {
                cuo++;
            }
            int q = num % 10;
            if (q == 3 || q == 6 || q == 9) {
                System.out.println("你还要继续游戏吗?Y键继续/N键退出并查看结果");
                String con = input.next();
                if (con.equals("y") || con.equals("Y")) {

                } else if (con.equals("n") || con.equals("N")) {
                    System.out.println("======■游戏进行了" + (num - cuo) + "次■======");
                    System.out.println("         ☆英雄榜☆");
                    System.out.println("对战方\t\t赢\t\t输\t\t平手");
                    System.out.println("  电脑\t\t" + lose + "\t\t" + win + "\t\t" + ping);
                    System.out.println("  玩家\t\t" + win + "\t\t" + lose + "\t\t" + ping);
                    System.out.println("==============================");
                    System.exit(-1);
                }
            }

        }
    }
}

//答案2
package student;

import java.util.Random;
import java.util.Scanner;

public class Kehou2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String c;
        int b = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        do {
            Random r = new Random();
            int pc = r.nextInt(3);
            System.out.println(pc);
            System.out.println("请出拳(0石头,1剪刀,2布):");
            int a = input.nextInt();
            //三元运算符 条件?真:假
            if (pc == a) {
                d++;
                System.out.println("你出的是" + (a==0?"石头":(a==1?"剪刀":"布")) + ",电脑出的是" + (pc==0?"石头":(pc==1?"剪刀":"布")) + ",平局");
            } else if (a == 0 && pc == 1 || a == 1 && pc == 2 || a == 2 && pc == 0) {
                e++;
                System.out.println("你出的是" + (a==0?"石头":(a==1?"剪刀":"布")) + ",电脑出的是" + (pc==0?"石头":(pc==1?"剪刀":"布")) + "您赢了");
            } else {
                f++;
                System.out.println("你出的是" + (a==0?"石头":(a==1?"剪刀":"布")) + ",电脑出的是" +(pc==0?"石头":(pc==1?"剪刀":"布")) + "您输了");
            }
            b++;
            System.out.println("是否继续y/n");
            c = input.next();
        } while ("y".equals(c));
        System.out.println("英雄版");
        System.out.println("======■游戏进行了" + b + "次■======");
        System.out.println("☆英雄榜☆");
        System.out.println("对战方\t\t赢\t\t输");
        System.out.println("电脑\t\t" + f + "\t\t" + e);
        System.out.println("玩家\t\t" + e + "\t\t" + f);
        System.out.println("平手\t\t" + d);
        System.out.println("==============================");

    }
}

 

本文地址:https://blog.csdn.net/mtm001/article/details/107492850

《Java基础循环while/do...while.doc》

下载本文的Word格式文档,以方便收藏与打印。