幻想测试游戏中的输入概率



我试着做一个简单的游戏...但是我遇到了这个问题,当我对任何事情说"不"时,我必须输入两次才能实际工作......下面是一个示例。你会攻击吗?(是(y)或否(n))输入:N输入:N如果你不攻击,你会使用魔法吗?

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

public class ZeNiMiGame {
    public static Scanner sc  = new Scanner(System.in);
    public static String name;
    public static String Class;
    public static String mon;
    public static int monhealth;
    public static int monattack;
    public static int health = 600;
    public static int mana = 150;
    public static Random r = new Random();
    public static void main(String[] args) {
        Story();
        PowerTable();
        Fight();
    }
    public static void Story() {
        System.out.println("Hello, warrior! What's your name?");
        name = sc.next();
        System.out.println("So your name is "+name+"... Well, I'm your Guide for this game.n You can call me Vestibulum magna ipsum ducem, or magna ipsum ducem for short.");
        try{
            Thread.sleep(5000);
        }catch (InterruptedException ex) {
        }
        System.out.println("Right now, we are traveling to the forgotten forest. Look! We're almost there!");
        try{
            Thread.sleep(5000);
        }catch (InterruptedException ex) {
        }
        System.out.println("Arrives at The Forgotten Forest");
        try{
            Thread.sleep(2000);
        }catch (InterruptedException ex) {
        }System.out.println("Look! An Orc! What do we do?");
        mon = "Orc";
        monhealth = 500;
        monattack = r.nextInt(30)+20;
        try{
            Thread.sleep(3000);
        }catch (InterruptedException ex) {
        }System.out.println("An Orc has appeared before you! Will you fight? Well, you really have no choice... FIGHT!");
    }
    public static void magic() {
            System.out.println("You can use 3 types of magic. Heal(h)nApollo's Arrows(a), and the classic Fireball(f)");
            System.out.println("Will you use Heal?(yes(y) or no(n))");
            if(sc.next().equalsIgnoreCase("y")) {
                System.out.println("You used: Heal");
                health = health+100;
                mana = mana-50;
                System.out.println("You healed yourself by 100 health using 50 mana.");
            }else if(sc.next().equalsIgnoreCase("n")) {
                System.out.println("Will you use Apollo's Arrows?(yes(y) or no(n))"); 
                if(sc.next().equalsIgnoreCase("y")) {
                    System.out.println("You used: Apollo's Arrows");
                    monhealth = monhealth-100;
                    mana = mana-100;
                    System.out.println("You attacked the"+mon+" for 100 damage.");
                    System.out.println("The "+mon+" now has "+monhealth+" left.");
                }else if(sc.next().equalsIgnoreCase("n")) {
                    System.out.println("Will you use the classic Fireball?(yes(y) or no(n))");
                    if(sc.next().equalsIgnoreCase("y")) {
                        System.out.println("You used: Fireball");
                        monhealth = monhealth - 50;
                        mana = mana-50;
                        System.out.println("You attacked the"+mon+" for 50" +" damage.");
                        System.out.println("The "+mon+" now has "+monhealth+" left.");
                    }
                }
            }
    }
    public static void PowerTable() {
    }
    public static void Fight() {
        for(int i=1;i<monhealth;i++) {
            System.out.println("Will you attack?(yes(y) or no(n))");
            int attack = r.nextInt(50)+30;
                if(sc.next().equalsIgnoreCase("y")) {
                    System.out.println("You attacked the "+mon+" for "+attack+" damage!");
                    monhealth = monhealth-attack;
                    System.out.println("The "+mon+" has "+monhealth+" health left!");
                    health = health - monattack;
                    System.out.println("You have "+health+" left.");
                    if(health  < 0) {
                        System.out.println("Your health is too low, therefore you died. GAME OVER");
                    }
                }else if(sc.next().equalsIgnoreCase("n")) {
                    System.out.println("Would you like to use magic?(yes(y) or no(n))");
                    if(sc.next().equalsIgnoreCase("y")) {
                    magic();
                    health = health - monattack;
                    System.out.println("You have "+health+" left.");
                    if(health  < 0) {
                        System.out.println("Your health is too low, therefore you died. GAME OVER");
                    }
                    }
                    else if(sc.next().equalsIgnoreCase("n")) {
                        System.out.println("Skipping turn...");
                        health = health - monattack;
                        System.out.println("You have "+health+" left.");
                        if(health  < 0) {
                            System.out.println("Your health is too low, therefore you died. GAME OVER");
                        }
                    }
                }
            }System.out.println("The "+mon+" has died! It's your win!!!");
        }
    }

那是因为你多次调用sc.next()。每次调用它时,它都会获得一个新输入。如果要将输入与多个值进行比较,则需要将其另存为变量。

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

public class ZeNiMiGame {
    public static Scanner sc  = new Scanner(System.in);
    public static String name;
    public static String Class;
    public static String mon;
    public static int monhealth;
    public static int monattack;
    public static int health = 600;
    public static int mana = 150;
    public static Random r = new Random();
    public static void main(String[] args) {
        Story();
        PowerTable();
        Fight();
    }
    public static void Story() {
        System.out.println("Hello, warrior! What's your name?");
        name = sc.next();
        System.out.println("So your name is "+name+"... Well, I'm your Guide for this game.n You can call me Vestibulum magna ipsum ducem, or magna ipsum ducem for short.");
        try{
            Thread.sleep(5000);
        }catch (InterruptedException ex) {
        }
        System.out.println("Right now, we are traveling to the forgotten forest. Look! We're almost there!");
        try{
            Thread.sleep(5000);
        }catch (InterruptedException ex) {
        }
        System.out.println("Arrives at The Forgotten Forest");
        try{
            Thread.sleep(2000);
        }catch (InterruptedException ex) {
        }System.out.println("Look! An Orc! What do we do?");
        mon = "Orc";
        monhealth = 500;
        monattack = r.nextInt(30)+20;
        try{
            Thread.sleep(3000);
        }catch (InterruptedException ex) {
        }System.out.println("An Orc has appeared before you! Will you fight? Well, you really have no choice... FIGHT!");
    }
    public static void magic() {
            System.out.println("You can use 3 types of magic. Heal(h)nApollo's Arrows(a), and the classic Fireball(f)");
            System.out.println("Will you use Heal?(yes(y) or no(n))");
            String ans = sc.next();
            if(ans.equalsIgnoreCase("y")) {
                System.out.println("You used: Heal");
                health = health+100;
                mana = mana-50;
                System.out.println("You healed yourself by 100 health using 50 mana.");
            }else if(ans.equalsIgnoreCase("n")) {
                System.out.println("Will you use Apollo's Arrows?(yes(y) or no(n))");
                ans = sc.next();
                if(ans.equalsIgnoreCase("y")) {
                    System.out.println("You used: Apollo's Arrows");
                    monhealth = monhealth-100;
                    mana = mana-100;
                    System.out.println("You attacked the"+mon+" for 100 damage.");
                    System.out.println("The "+mon+" now has "+monhealth+" left.");
                }else if(ans.equalsIgnoreCase("n")) {
                    System.out.println("Will you use the classic Fireball?(yes(y) or no(n))");
                    ans = sc.next();
                    if(ans.equalsIgnoreCase("y")) {
                        System.out.println("You used: Fireball");
                        monhealth = monhealth - 50;
                        mana = mana-50;
                        System.out.println("You attacked the"+mon+" for 50" +" damage.");
                        System.out.println("The "+mon+" now has "+monhealth+" left.");
                    }
                }
            }
    }
    public static void PowerTable() {
    }
    public static void Fight() {
        for(int i=1;i<monhealth;i++) {
            System.out.println("Will you attack?(yes(y) or no(n))");
            int attack = r.nextInt(50)+30;
                String ans = sc.next();
                if(ans.equalsIgnoreCase("y")) {
                    System.out.println("You attacked the "+mon+" for "+attack+" damage!");
                    monhealth = monhealth-attack;
                    System.out.println("The "+mon+" has "+monhealth+" health left!");
                    health = health - monattack;
                    System.out.println("You have "+health+" left.");
                    if(health  < 0) {
                        System.out.println("Your health is too low, therefore you died. GAME OVER");
                    }
                }else if(ans.equalsIgnoreCase("n")) {
                    System.out.println("Would you like to use magic?(yes(y) or no(n))");
                    ans = sc.next();
                    if(ans.equalsIgnoreCase("y")) {
                    magic();
                    health = health - monattack;
                    System.out.println("You have "+health+" left.");
                    if(health  < 0) {
                        System.out.println("Your health is too low, therefore you died. GAME OVER");
                    }
                    }
                    else if(ans.equalsIgnoreCase("n")) {
                        System.out.println("Skipping turn...");
                        health = health - monattack;
                        System.out.println("You have "+health+" left.");
                        if(health  < 0) {
                            System.out.println("Your health is too low, therefore you died. GAME OVER");
                        }
                    }
                }
            }System.out.println("The "+mon+" has died! It's your win!!!");
        }
    }

最新更新