永发信息网

java 问题 QuickHit项目

答案:1  悬赏:40  手机版
解决时间 2021-03-16 22:46
  • 提问者网友:niaiwoma
  • 2021-03-16 19:39
java 问题 QuickHit项目
最佳答案
  • 五星知识达人网友:痴妹与他
  • 2021-03-16 20:43
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

//QuickHit输出字符串,根据玩家的输入和所用时间来判断是否继续游戏

class Game{
    private int count;    //游戏玩家的级别        
    public void setCount(int count){
        this.count = count;
    }
    public int getCount(){
        return count;
    }
    public void myPrint(int countinteg, int count, int temptime){
        System.out.println("输入正确!继续努力:积分" + countinteg
                        + "级别:" + count + " 用时:" + temptime + "秒");
    }
    public void myPrint(int temptime){
        System.out.println("输入正确,但是超时结束。用时:" 
                            + temptime + "秒");
    }
    public void myPrint(){
        System.out.println("输入错误,游戏结束!");
    }
    
}
class MyString{    //与字符串有关类,随机字符串
    private String allstring;
    public MyString(){
        allstring = 
            "helloeveryone,thisisatestprogram,nicetomeetyoueveryone" ;
    }
    public String randomString(int number){
        Random rand = new Random();
        int tmp = rand.nextInt(allstring.length() - number);    //产生一个随机整数
        String out = allstring.substring(tmp,  tmp + number);  //从allstring里截取一段
        return out;
    }    
}
class LevelParam{    //与级别和字符串长度有关类,每个级别需要输入的字符串长度            
     public int getStrLength(int count){
        int back = 0;
        switch(count){
            case 1:
                back = 6;   break;
            case 2:
                back = 9;   break;
            case 3:
                back = 12;  break;
            case 4:
                back = 17;  break;
            case 5:
                back = 22;  break;
            case 6:
                back = 28;  break;
            default:
                break;
        }
        return back;
    }
}

public class Test{
    public static void main(String[] args){
        Game player = new Game();
        player.setCount(1);            //初始级别设置为1
        Scanner sc = new Scanner(System.in);
        int number = 0;                //设置测试的字符数目
        int countinteg = 0;            //countinteg是积分累计
        int limittime = 5;             //设置初始限制时间是5秒
        
        while( player.getCount() < 7 ){      //最高级别是6

                number = new LevelParam().
                    getStrLength( player.getCount() );   //根据玩家的级别确定长度
                String target = new MyString().randomString(number);
                System.out.println( target
                            + "  :请输入与上面相同的字符串!");
                long time1 = new Date().getTime();
                String input = sc.nextLine();
                long time2 = new Date().getTime();
                long temptime = (time2 - time1) / 1000;  //得到输入时耗费的时间总量
                if( input.equals(target) && temptime <= limittime){
                    countinteg += 50;
                    player.myPrint(countinteg, player.getCount(), limittime);
                }else if( input.equals(target) ){
                    player.myPrint(limittime);
                    break;
                }else{
                    player.myPrint();
                    break;
                }   
            if(countinteg >= 200){    //积分累计大于200分重新计算
                countinteg = 0;
                player.setCount(player.getCount() + 1);
                limittime += 1;
            }  
        }    
    }
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯