2017-03-11 16 views
-3

私は、特定の資格情報でログインしなければならないゲームを作ろうとしています。私はゲームをコーディングすることができますが、ゲームの間に選択のための入力をすることに固執しています。どんな助けもありがとう! (それは動作しないように思われる最後の行ですが、私は理由は分かりません)。複数の選択肢をユーザに作成

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(); 
     } 
    }  
} 
+0

あなたのクラスは判読不能ではありませんが、構造が正しくありません(たとえば、メソッド内でUserクラスを宣言したなど)。ここでjavaの型と式と構文を読んでください。問題なく動作するはずです:https://www.tutorialspoint.com/java/java_basic_syntax.htm –

+0

最後に実行しようとしていることは何ですか?ライン?今のところ、SkillsDemo3オブジェクトから非終了メソッドSkillsDemo3()を呼び出しています。 – Jonatan

答えて

0

あなただけの変数actionagainstaticを作成し、ワークフローの権利を取得し、あなたのクラスSkillsDemo3 のオブジェクトを必要としません。私はいくつかのワークフローを実装しようとしましたが、これがあなたに合っているかどうかわかりません。

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); 
     } 
    } 
} 
+0

あなたの助けてくれてありがとう、それは本当に物事をクリアする、私はなぜそれほど複雑なのか分からない –

関連する問題