1

So I was playing around the other day with loops, and I made a program where you steal money from a man. If you steal too much at one time, you will get caught. So everything looks right to me, but I am not understanding why my program will not let me input a string for the second input variable. I am new to programming so please be as elementary with your explanation as possible. Thanks and here is the code.

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 Answers0