0

I've started a bachelor degree in a computer science related field so I ought to code and everything is quite new for me, but I guess we all started from scratch.

I'm having a hard time making my code function the way it should. I have to program a flip-coin game....create random number (even/odd), using user input and then the user should play as long as he wants, therefore I created a while-loop and it doesn't seem to work property. I already tried to put my code inside it and it didn't work neither. My IDE is also telling me that i never user the value assigned to my scanner.nextInt(), which is UserEingabe. I'm quite sure is something quite easy to solve for many of u but I'm struggling a bit. Thanks in advance for the help.

Code: Main class

class CoinObject {

    public static void main(String[] args) {

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

Second class:

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;
            }
        }
    }
}
Vas
  • 13
  • 4

2 Answers2

0

As far as I understand, the important stuff (the three ifs) are not inside your while-loop, so they are executed once and then never again. I suppose they should be in the while loop too. My suggestion:

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);

I think you should use do while instead of while, because everything inside the brackets {} will then be executed at least once (and then dependant on what you wrote inside the while eventually multiple times).

Yasin
  • 72
  • 7
0

CoinObject (Main method)

public class CoinObject {

            public static void main (String[] args) {


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

Coin (game logic)

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

}

This fulfils the requirements of your game. Should the user try some invalid input that is not '0' or '1', he'll get a chance to type another input and guess the current random number.

melanzane
  • 483
  • 1
  • 6
  • 16
  • Thanks a lot I really appreciate the help. I checked the code changes and they are easy to follow. I've noticed you didn't use a loop to see whether the user wants to keep playing or not – Vas Nov 08 '17 at 22:38
  • No, there is no loop needed. If the player wants to play the game again the method throwCoin() is called. – melanzane Nov 09 '17 at 07:34
  • The code is working now, with your suggestions, but this condition is not functioning correctly, do you happen to know why? I can type values between (2&9) and that shouldn't be allowed: if (UserEingabe>1) { System.out.println("falsche Eingabe, versuchen Sie wieder"); flipCoin(); – Vas Nov 09 '17 at 19:13
  • Lets say the random number is '0'. If the user types '4' as input he will be redirected to play the game again. If you want the player to try again to guess the present random numer '0' we'll need some more modifications to the code. I didn't want to change your whole work in my previous example. But if you want I can help you with the modified working version. – melanzane Nov 09 '17 at 19:51
  • I just want to that my Programm only accepts 0 or 1 as valid values and if the user type 4, he will get a false input warning and then he can try to input something again, which means my method will be called – Vas Nov 09 '17 at 20:04
  • I edited the answer, but I had to slightly modify your architecture. – melanzane Nov 09 '17 at 21:00