Java基础练习题2

文章提供了一个Java程序,实现了猜数字游戏,包括if分支和循环结构,允许玩家最多猜10次。游戏结束后,用户可以选择是否重新开始。此外,还介绍了水仙花数的概念,并给出了一段Java代码来寻找3到7位的水仙花数。
摘要由CSDN通过智能技术生成

Java 流程控制

主要知识点:if 分支,for,while,do while 三种循环

课堂讲解:

Write a program that plays the game of “Guess the number” as follows:

Your program chooses the number to be guessed by selecting an integer at random

in the range 1 to 1000. The program then displays as follows:

I have a number between 1 and 1000.

Can you guess my number?

Please type your first guess.

The player then types a first guess. The program responds with one of the following:

1. Excellent! You guessed the number

2. Too Low. Try Again.

3. Too High. Try Again.(编写一个玩“猜猜数字”游戏的程序如下:您的程序通过在1到1000之间随机选择一个整数来选择要猜测的数字。然后程序显示如下:我有一个数字在1到1000之间。你能猜出我的数字吗?请输入你的第一个猜测。然后,玩家输入第一个猜测。该程序的响应方式为以下内容之一:1.“太好了!”你猜到了数字 2.太低了。再试一次。 3.太高了。再试一次)

Modify the above program:

If the player’s guess is incorrect, your program should loop until the player finally gets

the number right in which case you “break” from the loop. Your program should keep

telling the player Too high or Too low to help the player narrow the range of values

to guess.(修改上面的程序:如果玩家的猜测是不正确的,你的程序应该循环,直到玩家最终得到正确的数字,在这种情况下,你从循环中“打破”。你的程序应该不断告诉玩家太高或过低,以帮助玩家缩小猜测的范围。)

Modify the above program:

Player can play up to 10 times and give reminders of the remainning times. (修改上面的程序:玩家可以玩10次,并提醒剩余的次数。)

Modify the above program so that after the number is guessed the user of the

program can start a new game and can play as many games as they like until

the user quits the program. Use a do…while loop and print the message

“Would you like to play again (y or n)?” to play the game again. Declare a counter

variable to display the number of guesses it took the user to guess the random

number. (修改上面的程序,以便在猜测到这个数字后,这个程序的用户可以开始一个新的游戏,并可以玩尽可能多的游戏,直到用户退出这个程序。使用一个做……当循环并打印消息“你想再次播放(y或n)吗?”再来玩游戏。)

1.

package week2;

import java.util.Scanner;

public class GuessNumber4 {
    public static void main(String arg[]) {
        String choice;
        do {
            // 定义变量
            int radomNumber;
            int myGuessNumber;
            int time;

            // 变量赋值
            radomNumber = (int) (Math.random() * 1000) + 1;
            System.out.println(radomNumber);// 用于调试
            System.out.println("I have a number between 1 and 1000.");
            System.out.println("Can you guess my number?");
            System.out.println("Please type your first guess.");

            for (time = 0; time < 10; time++) {
                // 用户输入
                Scanner sca = new Scanner(System.in);
                myGuessNumber = sca.nextInt();
                if (myGuessNumber == radomNumber) {
                    System.out.println("Excellent! You guessed the number");
                    break;
                } else {
                    if (myGuessNumber > radomNumber) {
                        System.out.println("Too High. Try Again.");
                    } else {
                        System.out.println("Too Low. Try Again.");
                    }
                }
                System.out.println("你还剩下" + (9 - time) + "次机会");
            }
            if (time == 10) {
                System.out.println("很遗憾,游戏失败!");
            } else {
                System.out.println("恭喜你!游戏成功,共尝试了" + (time + 1) + "次");
            }
            System.out.println("Would you like to play again (y or n)?");
            Scanner sca1 = new Scanner(System.in);
            choice = sca1.nextLine();
        } while (choice.equalsIgnoreCase("y"));
    }
}

2.

Daffodils Numbers are also known as superperfect number invariants, narcissistic Numbers,

self-power Numbers, and Armstrong Numbers. Daffodils number refers to a number of n

digits (n≥3), the sum of the digits on each of its bits to the NTH power is equal to its own

requirements (eg:1^3 + 5^3+ 3^3 = 153;1^4+6^4+3^4+4^4=1634) ,write a program to calculate

the number of daffodils in n bits (3≤n≤7).

Input format:

Given a positive integer n (3≤n≤7).

Output format:

Output all n-bit daffodils in ascending order, one line for each digit.

Input sample:

Here's a set of inputs. Such as:

3

Output sample:

153

370

371

407

(水仙花数也被称为超完美数不变量、自恋数字、自幂数字和阿姆斯特朗数字。水仙花数是指n位数字(n≥3),每个位上的数字到NTH功率的总和等于它自己的要求(例如:1^3+5^3^3^3^3=153;1^4+644+3^4+4^4=1634),编写一个程序计算n位(3≤n≤7)。输入格式:给定一个正整数n(3≤n≤7)。输出格式:按升序输出所有n位水仙花,每位数字对应一行。输入示例:这里是一组输入。如:3输出样本: 153 370 371 407)

package exercise2;

import java.util.Scanner;

public class Narcissisticnumber {
    public static void main(String[] args) {
        int n;
        Scanner sca = new Scanner(System.in);
        n = sca.nextInt();
        // 遍历,逐个判断
        for (int number = (int) Math.pow(10, n - 1); number < (int) Math.pow(10, n); number++) {
            // 取number的每一位,并累加求和
            int sum = getEachBitSum(number, n);
            if (sum == number) {
                System.out.println(number);
            }
        }
    }

    // 方法的声明(权限+返回值类型+名称+参数列表)+方法体(实现代码)
    public static int getEachBitSum(int number, int n) {
        int sum = 0;
        int t = number;
        while (t != 0) {
            int a = t % 10;
            sum += (int) Math.pow(a, n);
            t /= 10;
        }
        return sum;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值