Day12_02

作者: yangyangyjj | 来源:发表于2016-12-03 16:20 被阅读0次

    一.循环结构与分支结构的嵌套练习

    1.输入一个数,判断是不是素数(只能被1和自身整除的数)

    import java.util.Scanner;
    public class Text1 {
            public static int  sort(int count) {
            count = (count + 1) * 2;
            return count;
        }
        public static void main(String[] args) {
            
             Scanner input = new Scanner(System.in); System.out.println("请输入一个数");
             *int a =input.nextInt(); boolean isPrime=true; for(int i =2;i<a;i++){
             if(a%i==0){ isPrime=false; break; } } System.out.println(a
              +(isPrime?"是":"不是")+"素数"); input.close();
             
    

    2.打印100以内的质数;

            for (int i = 1; i <= 100; i++) {
                boolean isPrime = true;
                for (int j = 2; j < Math.sqrt(i); j++) {
                    if (i % j == 0) {
                        isPrime = false;
                        break;
                    }
    
                }
                if (isPrime) {
                    System.out.println(i);
                }
    
            }
    
    

    3.九九乘法表

            for(int i=1;i<=9;i++){
                for(int j=1;j<=i;j++){
                    System.out.printf("%d*%d=%d\t",j,i,j*i);
                }
                System.out.println("");
            }
    
    

    4.猴子吃桃问题

            int count = 1;
            for (int i = 1; i < 10; i++) {
                sort(count);
            }
            System.out.println(count);
            
        
        }
        
    }
    

    .穷举法案列:100块钱买一百只鸡

    public class HomeWork1 {
        public static void main(String[] args) {
    
            for (int gj = 0; gj <= 20; gj++) {
                for (int mj = 0; mj <= 33; mj++) {
                    int xj = 100 - gj - mj;
                    if (5 * gj + 3 * mj + xj / 3 == 100 && (gj + mj + xj) == 100 && xj % 3 == 0) {
                        System.out.println("公鸡有 " + gj + "  母鸡有 " + mj + "  小鸡有 " + xj);
                    }
    
                }
            }
        }
    }
    

    二.两个典型案列

    1.Graps赌博游戏:

    <1>我的代码
    package comeDay12_01_2;
    
    import java.util.Scanner;
    
    public class Homework2 {
        public static void main(String[] args) {
            System.out.println("******************游戏规则**************************");
            System.out.println("**玩家总共1000分,每次赢加100分,每次输扣100分,平局分数不变**");
            System.out.println("**************************************************");
            System.out.println("准备好开始了吗,按\"1\"退出,其他任意键继续:");
            int fen = 200;
            int num = 0;
            int num1 = 0;
    
            Scanner input = new Scanner(System.in);
            String zb = input.nextLine();
            if (zb.equals("1")) {
                System.out.println("游戏结束,还剩" + fen + "分");
            } else {
                int a = (int) (Math.random() * 6 + 1);
                int b = (int) (Math.random() * 6 + 1);
                num = a + b;
                if (num == 7 || num == 11) {
                    fen += 100;
                    System.out.println("玩家胜利,摇出了" + num + "点,当前分数" + fen);
                } else if (num == 2 || num == 3 || num == 12) {
                    fen -= 100;
                    System.out.println("电脑胜利,摇出了" + num + "点,当前分数" + fen);
                } else {
                    System.out.println("平局,摇出了" + num + "点,当前分数" + fen);
                }
    
                System.out.println("按\"1\"退出,其他任意键继续:");
                String zb1 = input.nextLine();
                if (zb1.equals("1")) {
                    System.out.println("游戏结束");
                } else {
                    do {
                        int a1 = (int) (Math.random() * 6 + 1);
                        int b1 = (int) (Math.random() * 6 + 1);
    
                        num1 = a1 + b1;
    
                        if (num == num1) {
                            fen += 100;
                            System.out.println("玩家胜利,摇出了" + num1 + "点,当前分数" + fen);
                        } else if (num1 == 7) {
                            fen -= 100;
                            System.out.println("电脑胜利,摇出了" + num1 + "点,当前分数" + fen);
                        } else {
                            System.out.println("平局,摇出了" + num1 + "点,当前分数" + fen);
                        }
    
                        System.out.println("按\"1\"退出,其他任意键继续:");
                        String zb2 = input.nextLine();
                        if (zb2.equals("1")) {
                            System.out.println("游戏结束");
                        } else {
                            int a2 = (int) (Math.random() * 6 + 1);
                            int b2 = (int) (Math.random() * 6 + 1);
                            a1 = a2;
                            b1 = b2;
    
                        }
    
                    } while (fen > 0);
                    System.err.println("你已经输完了!!!无法继续了!!!");
                }
            }
            input.close();
    
        }
    
    }
    
    <2>教师代码
    • switch case 结构中,case后面若不跟break,程序会继续执行下去,直到出现break为止
    public class HomeWork__ {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int money = 1000;
            do {
                System.out.println("你还有"+money+"元!");
                
                int bet;
                
                do {
                    
                    System.out.println("请下注");
                    bet=input.nextInt();
                    
                } while (bet<=0||bet>money);
                
                int face1 = (int) (Math.random() * 6 + 1);
                int face2 = (int) (Math.random() * +1);
                int firsstPoint = face1 + face2;
                System.out.println("玩家摇出" + firsstPoint + "点");
                boolean needsGoOn = false;
                switch (firsstPoint) {
                case 7:
                case 11:
                    System.out.println("玩家胜");
                    money+=bet;
                    break;
                case 2:
                case 3:
                case 12:
                    System.out.println("庄家胜");
                    money-=bet;
                    break;
                default:
                    needsGoOn = true;   
                }
                while (needsGoOn) {
    
                    face1 = (int) (Math.random() * 6 + 1);
                    face2 = (int) (Math.random() * 6 + 1);
    
                    int currentPoint = face1 + face2;
                    System.out.println("玩家摇出了" + currentPoint + "点");
                    if (currentPoint == 7) {
                        System.out.println("庄家胜");
                        money-=bet;
                        needsGoOn = false;
                    } else if (currentPoint == firsstPoint) {
                        System.out.println("玩家胜");
                        money+=bet;
                        needsGoOn = false;
                    } 
                } 
                
            } while (money>0);
            System.out.println("你已经破产");
            input.close();
            
        }
    

    2.输入年月日,输出这是这一年的第几天

    • 如果程序中出现了重复的或者相对独立的功能,那么应该将这些功能单独写到一个方法中
    package comDay12_02_2;
    import java.util.Scanner;
    public class Text3 {
        // year作为因变量时,此方法需要返回一个判断是否为闰年的结果(是or不是),属于布尔类型;
                    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入年  月   日");
            int year = input.nextInt();
            int month = input.nextInt();
            int day = input.nextInt();
            int sum = 0;
            for (int i = 1; i < month; i++) {
    
                sum += daysOfMonth(year, i);
            }
            System.out.printf("有%d天", (sum + day));
            input.close();
        }
    //isLeapYear方法判断输入的年份是否属于闰年
        public static boolean isLeapYear(int year) {
    
            return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    
        }
    //daysOfMonth用于判断当前月份的天数
        public static int daysOfMonth(int year, int month) {
    
            if (month == 4 || month == 6 || month == 9 || month == 11) {
    
                return 30;
            } else if (month == 2) {
                if (isLeapYear(year)) {
                    return 29;
                } else {
                    return 28;
                }
    
            } else {
                return 31;
            }
        }
    }
    

    }

    相关文章

      网友评论

          本文标题:Day12_02

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