Java---作业习题(2)

1. 猜数字游戏

要求:用户输入数字,判断该数字是大于,小于,还是等于随机生成的数字,等于的时候退出程序。

解法一(使用分支结构,循环结构)

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

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

运行结果
在这里插入图片描述

解法二(使用方法)

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

public class Test {
    public static void main(String[] args) {
        guessNumberGame();
    }
    public static void guessNumberGame() {
        while (true) {
            //mnue
            //返回值表示玩家输入的选项
            //玩家输入1表示开始游戏
            //玩家输入0表示结束整个程序
            int choice = menu();
            if (choice == 1) {
                game();
                break;
            }else if (choice == 0) {
                System.out.println("GoodBye");
            }else {
                System.out.println("输入错误,请重新输入");
            }
        }
    }

    public static void game() {
        //1.先生成一个1-100之间的随机数
        Random random = new Random();
        int toGuess = random.nextInt(100)+1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("猜1-100之间的数");
        while (true) {
            //2.提示玩家输入整数
            int guess = scanner.nextInt();
            //3.判断
            if (guess < toGuess) {
                System.out.println("低了");
            }else if (guess > toGuess) {
                System.out.println("高了");
            }else {
                System.out.println("猜对了");
                break;
            }
        }
    }

    public static int menu() {
        System.out.println("=================");
        System.out.println("====1.开始游戏====");
        System.out.println("====0.退出游戏====");
        System.out.println("=================");
        System.out.println("==请输入你的选择:==");
        Scanner scanner = new Scanner(System.in) ;
        int choice = scanner.nextInt();
        return choice;
    }
}

运行结果
在这里插入图片描述

2. 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

解法一(使用分支结构,循环结构)

public class Test {
    public static void main(String[] args) {
        float sum = 0;
        for (int i = 1; i <=100 ; i++) {
            if (i %2 ==1) {
                sum += 1.0/i;
            }else{
                sum -= 1.0/i;
            }
        }
        System.out.println(sum);
    }
}

运行结果
在这里插入图片描述
解法二(使用方法)

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你想计算的数字:");
        int n = scanner.nextInt();
        int result = addFactor(n);
        System.out.println(result);
    }
    public static int addFactor(int n) {
        int result = 0;
        for (int i = 1; i <= n;i++){
            result += factorial(i);
        }
        return result;
    }
    public static int factorial(int n) {
        int result = 1;
        for (int i = 1; i <=n; i++){
            result *= i;
        }
        return result;
    }
}

运行结果
在这里插入图片描述

3. 求两个正整数的最大公约数

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 0;
        while (true){
            c = a % b;
            if (c == 0){
                break;
            } else {
                a = b;
                b = c;
            }
        }
        System.out.println(b);
    }
}

运行结果
在这里插入图片描述

4. 模拟登陆

要求:最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序

java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        String my_password = new String("123456");
        Scanner scanner = new Scanner(System.in);
        int count = 0;
        System.out.println("请输入6位数的密码:");
        for (count = 0; count < 3; count++) {
            String password = scanner.nextLine();
            if (my_password.equals(password)) {
                System.out.println("登录成功");
            } else if (!(my_password.equals(password)) && count <=1) {
                System.out.println("输入错误,请重新输入:");
            } else if (!(my_password.equals(password)) && count <=2) {
                System.out.println("连续三次错误,退出程序!");
            }
        }

    }
}

运行结果

在这里插入图片描述

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

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //外层循环
        for (int i = 1; i <= n; i++) {
            //内层循环
            for (int j = 1; j <= i; j++) {
                System.out.printf("%d x %d = %-2d  ",j,i,i*j);
            }
            System.out.println();
        }
    }
}

运行结果
在这里插入图片描述

6.求一个整数,在内存当中存储时,二进制1的个数。

public class TTest {
    public static void main(String[] args) {
        int num = 10; 
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if (((num >> i) & 1) == 1){
                count++;
            }
        }
        System.out.println(count);
    }
}

运行结果
在这里插入图片描述

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

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {3
        System.out.print("输入一个3位数的正整数:");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        for (int i = 0; i < 3; i++) {
            int n = num % 10;
            num = num / 10;
            System.out.print(n + " ");
        }
    }
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值