2017-11-08 7 views
0

私はコンピュータサイエンス関連の分野で学士号を取得していますので、コードを書く必要があります。すべてが新しくなりましたが、私たちはすべてゼロから始めました。コイントスゲームでループがうまく動作しないのはなぜですか?

自分のコードを機能させるのに苦労しています。私はフリップコインゲームをプログラムする必要があります....乱数(偶数/奇数)を作成し、ユーザーの入力を使用して、その後、ユーザーは、彼が望む限り再生する必要があるので、私はwhileループを作成し、プロパティを作業する。私はすでに自分のコードを入れようとしましたが、どちらもうまくいきませんでした。また、私のIDEは、私がuserEingabeである自分のscanner.nextInt()に割り当てられた値を決してユーザとして使用していないことを伝えています。私は確信していますが、あなたの多くは解決しやすいものですが、少し苦労しています。助けを前にありがとう。

コード: メインクラス

class CoinObject { 

    public static void main(String[] args) { 

    Coin coinObject = new Coin(); 
    coinObject.throwCoin(); 
    } 
} 

セカンドクラス:

import java.util.Scanner; 
public class Coin { 

    public void throwCoin(){ 

     Scanner scanner = new Scanner(System.in); 
     System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:"); 
     System.out.println("Kopf=0"); 
     System.out.println("Zahl=1"); 
     int UserEingabe = scanner.nextInt(); 
     int randomNumber = (int) Math.random(); 

     String yes = "yes"; 
     String no = "no"; 
     int spiele = 1; 
     int victories = 1; 
     String play = scanner.next(); 

// if the input = the random #, cool!, otherwise false :) 
      if (UserEingabe == randomNumber){ 
       System.out.println("Sie haben richtig geraten"); 
       System.out.println("Moechten Sie weiter spielen (yes/no)"); 
       play = scanner.next(); 

      } else { 

       System.out.println("Sie haben falsch geraten"); 
       System.out.println("Moechten Sie weiter spielen (yes/no)"); 
       play = scanner.next(); 

      } if (UserEingabe != 0 || UserEingabe != 1){ 
       System.out.println("falsche Eingabe, versuchen Sie wieder"); 
       UserEingabe = scanner.nextInt(); 
      } 
// the loop will be repeat it as long as the player wants to play 
     while (play != no){ 
      UserEingabe = scanner.nextInt(); 
      if (play == yes){ 
       System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen"); 

       victories=victories +1; 
       spiele = spiele+1; 
      } 
     } 
    } 
} 
+1

可能な複製(https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OldProgrammer

+0

おそらくMath.random()良いとは言えませんが、私が理解している限り0から1(毎回0または1)のランダムが必要です – john

+0

エラーは何ですか?どのように苦労していますか?何が効いていないのですか? – zuif

答えて

0

CoinObject(Mainメソッド)

public class CoinObject { 

      public static void main (String[] args) { 


      Coin coinObject = new Coin(); 
      coinObject.initializeGame(); 
      } 
     } 

コイン(ゲームロジックは)

import java.util.Random; 
import java.util.Scanner; 
public class Coin { 


    // In this block we initialize the global varibales, that can be used and modified by all the methods in the class 
    Scanner scanner = new Scanner(System.in); 
    private int games = 0; 
    private int victories = 0; 
    private int rightInputGuess = 0; 
    private int wrongInputGuess = 0; 
    private int[] items = new int[]{0,1}; 
    private Random rand = new Random(); 


     // This method creates the gameheader, meaning it creates a random number and passes it to the game method 
     public void initializeGame() { 
      System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:"); 
      System.out.println("Kopf=0"); 
      System.out.println("Zahl=1"); 
      int randomNumber = rand.nextInt(items.length); 

      if (randomNumber == 1) { 
       rightInputGuess = 1; 
       wrongInputGuess = 0; 
      } else if (randomNumber == 0) { 
       rightInputGuess = 0; 
       wrongInputGuess = 1; 
      } 

      playGame(randomNumber); 
     } 

     // This method is the actual game logic 
     // It takes the generates randomNumber as parameter. 
     // if the user types something else as 0 or 1 he will be asked to try to guess the number again. 
     public void playGame(int randomNumber) { 

      int userInput = scanner.nextInt(); 
      String play; 


      if (userInput == rightInputGuess){ 
       System.out.println("Sie haben richtig geraten"); 
       System.out.println("Moechten Sie weiter spielen (yes/no)"); 
       play = scanner.next(); 

       if(play.equals("yes")) { 
        victories=victories +1; 
        games = games+1; 
        initializeGame(); 
       } 
       else if (play.equals("no")){ 
        victories=victories +1; 
        games = games+1; 
        System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen"); 
       } 

      } else if (userInput == wrongInputGuess){ 

       System.out.println("Sie haben falsch geraten"); 
       System.out.println("Moechten Sie weiter spielen (yes/no)"); 
       play = scanner.next(); 

       if(play.equals("yes")) { 
        games = games+1; 
        initializeGame(); 
       } 
       else if (play.equals("no")){ 
        games = games+1; 
        System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen"); 
       } 

      } else if (userInput != 0 || userInput != 1){ 
       System.out.println("falsche Eingabe, versuchen Sie wieder"); 
       // The playGame method is called with the current randomNumber. 
       // If the user types something else as 0 or 1 he gets the chance to type a valid guess 
       playGame(randomNumber); 
      } 
     } 

} 

これはあなたのゲームの要件を満たしています。ユーザーが '0'または '1'以外の無効な入力を試すと、別の入力を入力して現在の乱数を推測することができます。 [私はJavaで文字列を比較するにはどうすればよい?]の

+0

ありがとう、私は本当に助けに感謝します。私はコードの変更をチェックし、それに従うのは簡単です。私はあなたがループを使用していないことに気付いていますか? – Vas

+0

いいえ、ループは必要ありません。プレイヤーがゲームを再びプレイしたい場合、メソッドthrowCoin()が呼び出されます。 – melanzane

+0

ありがとう、それは私のものだ – Vas

0

は、私の知る限り理解し、重要なもの(3 IFS)は、あなたのwhile-内ではありませんループするので、それらは一度実行されてから再び実行されません。私は彼らもwhileループにいるはずだと思います。私の提案:

do { 
    if (UserEingabe == randomNumber){ 
     System.out.println("Sie haben richtig geraten"); 
     System.out.println("Moechten Sie weiter spielen (yes/no)"); 
     play = scanner.next(); 

    } else { 

     System.out.println("Sie haben falsch geraten"); 
     System.out.println("Moechten Sie weiter spielen (yes/no)"); 
     play = scanner.next(); 

    } if (UserEingabe != 0 || UserEingabe != 1){ 
     System.out.println("falsche Eingabe, versuchen Sie wieder"); 
     UserEingabe = scanner.nextInt(); 
    } 
    // the loop will be repeat it as long as the player wants to play 
    UserEingabe = scanner.nextInt(); 
    if (play == yes){ 
      System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen"); 

      victories=victories +1; 
      spiele = spiele+1; 
    } 
} while (play != no); 

私は括弧{}内のすべてが、その後少なくとも一度実行し(その後、あなたはしばらくの内部で最終的に複数回を書いたものに依存して)されますので、あなたは、代わりに、しばらくの間、やる使うべきだと思います。

+0

ありがとう、私は間違いなくそれを試してみます。 – Vas

関連する問題