为用户创建多项选择

时间:2017-03-11 00:23:23

标签: java string class void

我试图制作一个必须使用某些凭据登录的游戏,之后用户可以在两个游戏之间进行选择。我能够对游戏进行编码,但我仍坚持在游戏之间进行选择。任何帮助表示赞赏! (这是最后一行似乎不起作用,我不知道为什么)。

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; 

public class SkillsDemo3 {
    boolean again = true;
    int action;

    public static void main(String[] args) throws IOException {

        //***************************
        //Login
        //***************************

        class User {
            User (String username, String password) {
                this.username = username;
                this.password = password;
            }

            String GetUsername() { return username; }
            String GetPassword() { return password; }

            private String username;
            private String password;
        }       

        String greeting = "Hello";
        String username;
        String password;

        // Used to hold the instance of a user who successfully logged in
        User loggedInUser = null;

        // Create an empty list to hold users
        List<User> listOfUsers = new ArrayList<>();

        // Add 3 users to the list
        listOfUsers.add(new User("Gerry","spintown"));
        listOfUsers.add(new User("Evelyn","poker"));
        listOfUsers.add(new User("Joan","bonus"));

        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));  

        System.out.println("*** Welcome to the program ***\n");
        System.out.println(greeting);

        System.out.println("Please type your username :");
        username = br.readLine();   
        System.out.println("Please type your password :");
        password = br.readLine();

        for (User user : listOfUsers) {
            if (user.GetUsername().equals(username)) {
                if (user.GetPassword().equals(password)) {
                    loggedInUser = user;

                    // when a user is found, "break" stops iterating through the list
                    break;
                }
            }
        }

        // if loggedInUser was changed from null, it was successful
        if (loggedInUser != null) {
            System.out.println("User successfully logged in: "+loggedInUser.GetUsername());
        } else {
            System.out.println("Invalid username/password combination");
        }

        //**********************************
        //Choice of Games
        //**********************************   

        boolean again = true;
        int action = 0;     

        if (action == 1) {
            System.out.println("\nYou have chosen to play Rock, Paper, Scissors");
        } else if (action == 2) {
            System.out.println("\nYou have chosen to Play pick up sticks");
            again = false;
        }

        SkillsDemo3 what = new SkillsDemo3();

        while (what.again) {
            System.out.println("Please type 0 to continue or 1 to stop :");
            what.action = Integer.parseInt(br.readLine());
            System.out.println("You typed : "+what.action);
            what.SkillsDemo3();
        }
    }       
}

1 个答案:

答案 0 :(得分:0)

您不需要班级SkillsDemo3的对象 只需创建变量actionagain static,即可使您的工作流程正确。我试图实现一些工作流程,但我不知道这是否适合你。

public class SkillsDemo3 {
    private static boolean again = true;
    private static int action;


    public static void main(String[] args) throws IOException {

        //***************************
        //Login
        //***************************

        class User {
            User (String username, String password)
            {
                this.username = username;
                this.password = password;
            }

            String GetUsername() {return username;}
            String GetPassword() {return password;}

            private String username;
            private String password;
        }


        String greeting = "Hello";
        String username;
        String password;

        // Used to hold the instance of a user who successfully logged in
        User loggedInUser = null;

        // Create an empty list to hold users
        List<User> listOfUsers = new ArrayList<>();

        // Add 3 users to the list
        listOfUsers.add(new User("Gerry","spintown"));
        listOfUsers.add(new User("Evelyn","poker"));
        listOfUsers.add(new User("Joan","bonus"));


        BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));


        System.out.println("*** Welcome to the program ***\n");
        System.out.println(greeting);

        System.out.println("Please type your username :");
        username = br.readLine();
        System.out.println("Please type your password :");
        password = br.readLine();

        for (User user : listOfUsers)
        {
            if (user.GetUsername().equals(username))
            {
                if (user.GetPassword().equals(password))
                {
                    loggedInUser = user;
                    // when a user is found, "break" stops iterating through the list
                    break;
                }
            }
        }

        // if loggedInUser was changed from null, it was successful
        if (loggedInUser != null)
        {
            System.out.println("User successfully logged in: "+loggedInUser.GetUsername());
        }
        else
        {
            System.out.println("Invalid username/password combination");
        }

        //**********************************
        //Choice of Games
        //**********************************
        again = true;
        action = 0;

        while (again)
        {
            System.out.println("Please type 1 for Rock, Paper, Scissors or 2 for Play pick up sticks:");
            action = Integer.parseInt(br.readLine());
            if (action == 1)
            {
                System.out.println("\nYou have chosen to play Rock, Paper, Scissors");
            }
            else if (action == 2)
            {
                System.out.println("\nYou have chosen to Play pick up sticks");
                again = false;
            }
            System.out.println("Please type 0 to continue or 1 to stop :");
            action = Integer.parseInt(br.readLine());
            again = action == 0;
            System.out.println("You typed : "+action);
        }
    }
}