Day8.小测试:九九乘法表、打印三角形、简易计算器、猜数字游戏

  九九乘法表

    public static void main(String[] args) {
        //打印99乘法表

        //1.打印第一列
        //2.固定的y再用循环包起来
        //3.去掉重复项,x<=y
        //3.调整样式
        for (int y=1;y<=9;y++){
            for(int x=1;x<=y;x++){
                System.out.print(x+"*"+y+"="+(x*y)+"\t");
            }
            System.out.println();
        }

打印三角形 

    public static void main(String[] args) {
        /*从键盘输入一个数字作为行数,打出下列图像
            *
           ***
          *****
         */

        System.out.println("请输入要打印的行数:");
        Scanner scanner = new Scanner(System.in);
        int number =scanner.nextInt();

        for(int y=1;y<=number;y++){

            for(int x = number;x>y;x--){
                System.out.print(" ");
            }
            for(int x1 = 0;x1

判断年龄

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //判断年龄
        System.out.println("请输入年龄:");
        int x = scanner.nextInt();
        while (x>120|| x<0) {
            System.out.println("输入错误,请重新输入");
            x = scanner.nextInt();
        }
        if (x >= 18 && x <= 120) {
            System.out.println("您已成年。");
        } else if (x > 0 && x < 18) {
            System.out.println("您未成年");
        }
        scanner.close();
    }

 简易计算器

    public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);
            int isContinue = 1;
            while (isContinue == 1) {
                System.out.println("===请输入第一个数字===");
                double parm1 = scanner.nextDouble();
                System.out.println("===请输入第二个数字===");
                double parm2 = scanner.nextDouble();
                System.out.println("===请选择计算方式===");
                System.out.println("===加法请输入:0===");
                System.out.println("===减法请输入:1===");
                System.out.println("===乘法请输入:2===");
                System.out.println("===除法请输入:3===");
                int type = scanner.nextInt();
                switch (type) {
                    case 0:
                        System.out.println("结果为" + add(parm1, parm2));
                        break;
                    case 1:
                        System.out.println("结果为" + subtraction(parm1, parm2));
                        break;
                    case 2:
                        System.out.println("结果为" + multiplication(parm1, parm2));
                        break;
                    case 3:
                        System.out.println(division(parm1, parm2));
                        break;
                    default:
                        System.out.println("非法参数");
                }
                System.out.println("===继续计算? 1:继续, 2:停止===");
                isContinue = scanner.nextInt();
            }
            scanner.close();
            System.out.println("运算结束");
        }

        //运算方式
        public static double add(double a, double b) {
            return a + b;
        }   //加
        public static double subtraction(double a, double b) {
            return a - b;
        }  //减
        public static double multiplication(double a, double b) {
            return a * b;
        }  //乘
        public static String division(double a, double b) {
            double result = 0;
            if (a % b != 0) {
                double result2 = a % b;
                result = a / b;
                return "结果为" + result + "  余数为" + result2;
            } else {
                result = a / b;
            }
            return "结果为" + result;
        }  //除
}

猜数游戏

    public static void main(String[] args) {
        //猜数字
        //用户输入一个数字,猜大猜小有提示,输入错误重新输入
        Random r = new Random();              //随机数

        int number = r.nextInt(301) + (-100);
        //301表示:你希望生成的随机数的所有取值的可能性的数量
        //-100表示:该区间的最小取值
        System.out.println(number);

        System.out.println("请输入您所猜的数字:");

        int temp = number + 1;        //不与number重复
        
        Scanner scanner = null;       //如果在循环内定义则为局部变量,无法使用close()
        do {
            scanner = new Scanner(System.in);
            if (scanner.hasNextInt()) {
                temp = scanner.nextInt();
                if (temp < number) {
                    System.out.println("你输入的数字为:" + temp + "小了,请再猜一次:");
                } else if (temp > number) {
                    System.out.println("你输入的数字为:" + temp + "。大了,请再猜一次:");
                } else {
                    System.out.println("你输入的数字为:" + temp + "恭喜您!猜对了!");
                }
            } else {
                System.out.println("非法字符,请重新输入数字:");
            }
        } while (!(temp == number));
        
        scanner.close();
    }

你可能感兴趣的:(JavaSE,笔记,java,后端)