在程序中输入不允许在Java中输入

时间:2015-11-19 01:43:10

标签: java

所以我前几天一直在玩循环,我做了一个程序,你从一个男人那里偷钱。如果你一次偷得太多,你就会被抓住。所以一切看起来都对我,但我不明白为什么我的程序不会让我输入第二个输入变量的字符串。我是编程新手,所以请尽可能简单地解释。谢谢,这是代码。

import java.util.Scanner;
public class Stealling {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int money = 1000;
        String userResponse = "Y";
        int totalStolen = 0;

        while((money > 0) && (userResponse.equalsIgnoreCase("Y"))) { 
            System.out.println("This guy has 1000 dollars, how much would you like to steal from him?");
            int moneyStolen = input.nextInt();
            money = money - moneyStolen;
            totalStolen = totalStolen + moneyStolen;
            System.out.println("Would you like to steal more from this man?");
            userResponse = input.nextLine();
            if (userResponse.equalsIgnoreCase("N")) {
                System.out.printf("Congratulations you have stolen %d dollars without getting caught! You have stolen %d so far", moneyStolen, totalStolen);}
            else if(money <= 0) {
                System.out.println("You have stolen all his money without getting caught!");
                userResponse = "N";}
            else if(moneyStolen >= 50) {
                System.out.println("You have stolen too much at one time you are going to jail!");
                userResponse = "N";}
        }
    }
}

0 个答案:

没有答案