美文网首页Java从零开始
Java流程控制练习题

Java流程控制练习题

作者: chad_it | 来源:发表于2016-11-02 21:22 被阅读630次

    1、输入两个整数,打印两数之差的绝对值。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("输入两个整数,用回车或者空格分隔");
            Scanner scan = new Scanner(System.in);
            int one = scan.nextInt();
            int two = scan.nextInt();
            
            if (one - two >= 0) {
                System.out.println(one - two);
            }else {
                System.out.println(two - one);
            }
        }
    }
    

    2、编写一个四则运算器,计算加减乘除,例如输入10 + 20,输出30。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("请输入一个四则运算,用空格分隔");
            
            Scanner scan = new Scanner(System.in);
            int numOne = scan.nextInt();
            String str = scan.next();
            int numTwo = scan.nextInt();
            
            switch(str){
            case "+":
                System.out.println(numOne + numTwo);
                break;
            
            case "-":
                System.out.println(numOne - numTwo);
                break;
                
            case "*":
                System.out.println(numOne * numTwo);
                break;
                
            case "/":
                System.out.println(numOne / numTwo);
                break;
                
                default:
                    System.out.println("格式错误");
            }
        }
    }
    
    

    3、输入年月日,计算是一年的第几天,考虑闰年。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo3 {
        
        public static void main(String[] args){
            
            System.out.println("请输入年月日,用空格分隔");
            
            Scanner scan = new Scanner(System.in);
            int year = scan.nextInt();
            int month = scan.nextInt();
            int day = scan.nextInt();
            int total = 0;
             
            for (int i = 1; i < month; i++) {
                switch (i) {
                case 1:
                    total += 31;
                    break;
                case 2:
                    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                        total += 29;
                    } else {
                        total += 28;
                    }
                    break;
                case 3:
                    total += 31;
                    break;
                case 4:
                    total += 30;
                    break;
                case 5:
                    total += 31;
                    break;
                case 6:
                    total += 30;
                    break;
                case 7:
                    total += 31;
                    break;
                case 8:
                    total += 31;
                    break;
                case 9:
                    total += 30;
                    break;
                case 10:
                    total += 31;
                    break;
                case 11:
                    total += 30;
                    break;
                case 12:
                    total += 31;
                    break;
                }
            }
            System.out.println(total+day);
        }
    }
    
    

    4、判断一个三位数是否是水仙花数,如果是,输出YES,如果不是,输出NO。
    说明:水仙花就是一个数的每个数字的立方和等于它本身的数,例如153就是一个水仙花数111+555+333=153。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo4 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("请输入一个三位数");
            
            Scanner scan = new Scanner(System.in);
            int num = scan.nextInt();
            
            int hundreds = num/100;
            int tens = num%100/10;
            int ones = num%10;
            
            if (num == hundreds*hundreds*hundreds + tens*tens*tens + ones*ones*ones) {
                System.out.println("YES");
            }else {
                System.out.println("NO");
            }
        }
    }
    
    

    5、输入一个时间,输出时间的下一秒。例如输入的是10 20 59,表示是10点20分59秒,下一秒就是10点21分0秒。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo5 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("请输入时分秒,用空格分隔");
            
            Scanner scan = new Scanner(System.in);
            int hours = scan.nextInt();
            int minutes = scan.nextInt();
            int second = scan.nextInt();
            
            if (hours > 12 || minutes > 60 || second > 60) {
                System.out.println("格式错误");
            }else {
                if (second + 1 == 60) {
                    minutes ++;
                    second = 0;
                }else {
                    second ++;
                }
                
                if (minutes == 60) {
                    hours ++;
                    minutes = 0;
                }else {
                    
                }
                System.out.println(hours + "时" + minutes + "分" + second + "秒");
            }
        }
    }
    
    

    6、输入三个整数,x,y,z,把这三个数由小到大输出。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo6 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("请输入三个整数,用空格隔开");
            
            Scanner scan = new Scanner(System.in);
            int a = scan.nextInt();
            int b = scan.nextInt();
            int c = scan.nextInt();
            
            if (a > b) {
                a = a + b;
                b = a - b;
                a = a - b;
            }
            if (b > c) {
                b = b + c;
                c = b - c;
                b = b - c;
            }
            if (a > b) {
                a = a + b;
                b = a - b;
                a = a - b;
            }
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
        }
    }
    
    

    7、求1! + 2! + 3! +…+ 5!。

    package com.javawu.demo;
    
    public class Demo7 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            int num = 0;
            for (int i = 1; i <= 5; i ++) {
                for (int j = 1; j <= i; j ++) {
                    num = num + i * j;
                }
            }
            System.out.println(num);
        }
    }
    
    

    8、输入一个整数,计算该数有多少位 ,例如123有3位。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo8 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("请输入一个整数");
            
            Scanner scan = new Scanner(System.in);
            int num = scan.nextInt();
            
            int a = 1;
            int time = 0;
            while (num/a != 0) {
                a = a * 10;
                time ++;
            }
            System.out.println(time);
        }
    }
    
    

    9、整数逆序输出,例如输入一个整数12345,输出整数54321。

    package com.javawu.demo;
    
    import java.util.Scanner;
    
    public class Demo9 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            System.out.println("请输入一个整数");
            
            Scanner scan = new Scanner(System.in);
            int num = scan.nextInt();
            
            int a = 1;
            int time = 0;
            while (num/a != 0) {
                a = a * 10;
                time ++;
            }
            
            System.out.println(time);
            
            int result = 0;
            
            for (int i = 0; i <= time; i ++) {
                int b = num / i * 10;
                System.out.println(b);
                result = result + (time - i+1) * b;
            }
            System.out.println(result);
        }
    }
    
    

    10、打印九九乘法表。

    package com.javawu.demo;
    
    public class Demo10 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            for (int i = 1; i <= 9; i ++) {
                for (int j = 1; j <= i; j ++) {
                    System.out.print(i + "*" + j + "=" + i * j + "\t");
                }
                System.out.println();
            }
        }
    }
    
    

    11、打印图形。

    等腰三角形
    package com.javawu.demo;
    
    public class Demo11 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            System.out.println();
            System.out.println();
            for (int i = 1; i <= 6; i ++) {
                System.out.print(" ");
                System.out.print(" ");
                for (int j = 1; j <= 6 - i; j ++) {
                    System.out.print(" ");
                }
                
                for (int k = 1; k <= i *2 -1; k ++) {
                    System.out.print("*");
                } 
                System.out.println();
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Java流程控制练习题

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