Java程序逻辑控制题

1、按照上述办法通知学生成绩。

考虑到学生自尊,不公开分数排名,因此:

分数在 [90, 100] 之间的为优秀、   分数在 [80, 90) 之前的为良好、

分数在 [70, 80) 之间的为中等、     分数在 [60, 70) 之间的为及格

分数在 [ 0, 60) 之间的为不及格      错误数据

按照上述办法通知学生成绩。

    public static void main(String[] args) {
        int score=67;
        if (score>=90){
            System.out.println("成绩优秀");
        }else if(score>=80){
            System.out.println("成绩良好");
        }else if(score>=70){
            System.out.println("成绩中等");
        }else if(score>=60){
            System.out.println("成绩及格");
        }else if (score<60) {
            System.out.println("成绩不合格");
        }else{
            System.out.println("错误数据");
        }
    

2、判断一个数字是奇数还是偶数

public static void main(String[] args) {
        int a=10;
        if (a%2==0){
            System.out.println("a是偶数");
        }else{
            System.out.println("a是奇数");
        }

    }

3、判断一个数字是正数、负数还是零

public static void main(String[] args) {
        int num=0;
        if (num>0){
            System.out.println("num是正数");
        }else if(num==0){
            System.out.println("num是零");
        }else{
            System.out.println("num是负数");
        }
    }

4、判断一个年是不是闰年

 public static void main(String[] args) {
        int year=2004;
        if( ((year%4==0)&&year%100!=0)||(year%100==0) ){
            System.out.println("year是闰年");
        }else{
            System.out.println("year不是闰年");
        }
    }

5、输出1000~2000年当中的闰年

public static void main(String[] args) {
        int year;
        int ret=0;
        for (year=1000;year<=2000;year++){
            if( ((year%4==0)&&year%100!=0)||(year%100==0) ){
                System.out.println(year);
                System.out.print(" ");
                ret++;
                if(ret%10==0){
                    System.out.println();
                } 
            }
        }
    }

6、给定一个数字判断它是不是素数

int n=15;
        int i;
        boolean flag=true;
        for(i=2;i<n;i++){
            if(n%i==0){
                flag=false;
                break;
            }
        }
        if (flag){
            System.out.println(n+"是素数");
        }else{
            System.out.println(n+"不是素数");
        }

7、输出1~100之间的素数

 int i;
       int j=2;
            for (i=2;i<=100;i++) {
                boolean flag = true;
                for (j = 2; j < i; j++) {
                    if (i % j == 0) {
                        flag = false;
                        break;
                    }
                }

                if (flag) {
                    System.out.println(j + "是素数");
                }
            }

8、求100~999之间的水仙花数

public static void main2(String[] args) {
        int d1;
        int d2;
        int d3;
        int n;
        int sum=0;
        for (d1=1;d1<=9;d1++){
            for (d2=0;d2<=9;d2++){
                for (d3=0;d3<=9;d3++){
                    n=d1*100+d2*10+d3;
                    if (n==d1*d1*d1+d2*d2*d2+d3*d3*d3){
                        sum+=n;
                        System.out.println(n);
                    }
                }
            }
        }
    }

9、输出乘法口诀表

输出n*n的乘法口诀表,n由用户输入。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int i;
        int j;
        for(i=1;i<=n;i++){
            for (j=1;j<=i;j++){
                System.out.println(i + "*" + j + "=" + i * j );
            }
            System.out.println();
        }
    }

10、猜数游戏

  public static void main(String[] args) {
                Random random = new Random();
                Scanner sc = new Scanner(System.in);
                int toGuess = random.nextInt(100);
                // System.out.println("toGuess: " + toGuess);
                while (true){
                    System.out.println("请输入要输入的数字: (1-100)");
                    int num= sc.nextInt();
                    if (num < toGuess) {
                        System.out.println("低了");
                    } else if (num > toGuess) {
                        System.out.println("高了");
                    } else {
                        System.out.println("猜对了");
                        break;
                    }
                }
                sc.close();
                }

11、计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 。

 int i;
        double n=0.0;
        double ret=1.0;
        for (i=1;i<=100;i++){
            n+=ret/i;
            ret =-ret;
        }
        System.out.println(n);

12、求两个数的最大公约数

int a=12;
        int b=18;
        int n;
        while(b!=0){
             n=a%b;
            a=b;
            b=n;
        }
        System.out.println(a);

13、输出一个整数的每一位,如:123的每一位是1 , 2 , 3

int a = 9527;
        print(a);
    }
    
    public static void print(int a) {
        if (a > 9) {
            print(a / 10);
        }
        System.out.println(a % 10 + "");

14、二进制序列(取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列)

Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int num= scanner.nextInt();
        int count=0;
        int i;
        for (i=0;i<32;i++){
            if (((num>>i)&1)==1){
                count++;
            }
        }
        System.out.println(count);

 15、模拟登陆(编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序)

  String password = "123456";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入密码:");
        int retrytime = 3;
        for (int i = 0; i < 3; i++) {
            String input = scanner.nextLine();
            if (input.equals(password)) {
                System.out.println("输入正确!");
                break;
            }
            else {
                retrytime--;
                System.out.println("输入错误,请重新输入,还有" + retrytime + "次机会");
            }
        }
        if (retrytime == 0) {
            System.out.println("输入次数用完,退出程序");
        }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值