如何实现重播功能? [英] How do i implement a play again feature?

查看:66
本文介绍了如何实现重播功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在游戏结束时得到提示; 如果我想再玩一次. 并输入Y/N:退出游戏或重复游戏.

I would like to be prompted when the game finishes; If I would like to play again. And with a Y/N input: either exiting the game or repeat it.

如何以最有效的方式解决这个问题?

How do I go about this in the most efficient way?

说明资源路径位置类型 对于类型Main Main,未定义方法playAgain().java/ScaredyCat/src/se/gruppfisk/scaredycat/main line 12 Java问题

Description Resource Path Location Type The method playAgain() is undefined for the type Main Main.java /ScaredyCat/src/se/gruppfisk/scaredycat/main line 12 Java Problem

public boolean playAgain(Scanner keyboard) {
    try {
        System.out.print("Do you want to play again? (Y/N): ");
        String reply = keyboard.nextLine();
        return reply.equalsIgnoreCase("Y");
    } finally {
        keyboard.close();

推荐答案

在主方法中添加循环:

public static void main(String[] args) {
    do {
        ScaredyCat game = new ScaredyCat(new Player("Urkel",23),new Player("Steve", 18));
        game.play();
    } while(playAgain());
}

private static boolean playAgain() {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Play again? (Y/N): ");
    String replay = keyboard.nextLine();
    return replay.equals("Y");
}

这篇关于如何实现重播功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆