java课堂作业第六周

题目一

编写一个猜字游戏,随机产生一个单词,提示用户一次猜测一个字母,如运行实例所示,单词中的每个字母显示为一个星号,当用户猜测正确后,正确的字母显示出来,当用户猜出一个单词,显示猜错的次数,并且询问用户是否继续对另外一个单词进行游戏,声明一个数组来存储单词,如下所示。
在这里插入图片描述

package ch06;
import java.util.Random;
import java.util.Scanner;
public class b {
    //定义一个数组进行单词帅选
    public static String[] words={"word","telecommunication","programming","bupt"};
    //随机获取一个单词
    public static String word=null;
    //创建一个状态数组进行判断
    public static boolean[] sta=null;
    //定义一个输错的次数
    public static int missed=0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        word = words[random.nextInt(words.length)];
        sta = new boolean[word.length()];
        //用户输入字母,判断是否猜对
        while (true) {
            String password = Guesspassword();            //获取现在的状态
            System.out.print("(Guess)Enter a letter in word " + password + " > ");
            String letter = scanner.nextLine();           //把用户猜测的单词放入letter进行判断
            changeword(letter);                         //通过letter,判断是否改变现有状态
            if (TheEnd()) {
                System.out.println("The word is " + word + " . You missed " + missed + " times");
                System.out.println("Do you want to guess another word? Enter y or n>");
                if (scanner.nextLine().equals("y")) {      //判断是否要重新来一局
                    //随机获取一个单词
                    word = words[random.nextInt(words.length)];
                    //创建一个状态数组进行判断
                    sta = new boolean[word.length()];
                    //定义一个输错的次数
                    missed = 0;
                } else {
                    break;
                }
            }

        }

    }
    //获取当前状态的函数
    public static String Guesspassword(){
        String password="";
        for(int i=0;i<word.length();i++){          //依次遍历判断状态,并进行是否显示
            if(sta[i]==true){
                password+=word.charAt(i);
            }else{
                password+="*";
            }
        }
        return password;
    }
    //判断是否改变状态
    public static void changeword(String letter){
        //拿到输入单词,遍历当前状态,判断是否存在
        boolean flag=false;
        for(int i=0;i<word.length();i++){
            if((word.charAt(i)+"").equals(letter)){
                flag=true;
                if(sta[i]==false){
                    sta[i]=true;
                }else{
                    System.out.println("\t"+letter+" is already in the word");
                    return;
                }
            }
        }
        if(!flag){
            missed++;
            System.out.println("\t"+letter+" is not in the word");
        }

    }

    //判断是否终止,是否猜完
    public static boolean TheEnd(){
        //依次遍历,若均为true,则猜测完毕
        for(int i=0;i<sta.length;i++){
            if(sta[i]==false){
                return false;
            }
        }
        return true;
    }
}

题目二

回文素数是指一个数同时为素数和回文数,如:131是一个素数,同时也是一个回文素数,数字313和757也是如此,编写程序,显示前100个回文素数,每行显示10个数并且准确对齐,数字中间用空格隔开,如下所示。

package ch06;

import java.util.Scanner;

public class a {
    public static void main(String[] args) {
        int count = 0, i = 1;
        while (count < 100) {
            i++;
            if ((huiwen(fanzhuan(i),i))&sushu(i))
            {
                count++;
                System.out.printf("%8d",i);
                if(count%10==0)
                    System.out.println();
            }
        }
    }

    public static boolean sushu(int number) {
        boolean flag=true;
        for (int i = 2; i <=Math.sqrt(number); i++) {
            if (number % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static boolean huiwen(int x, int y) {
        if (x == y)
            return true;
        else
            return false;
    }

    public static int fanzhuan(int number) {
        int sum = 0;
        while (number > 0) {
            int x = number / 10;
            int y = number % 10;
            sum = sum * 10 + y;
            number /= 10;
        }
        return sum;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值