Zahlenratenspiel

BlackSalad

Bekanntes Mitglied
danke, ich bin schon dabei, aber muss erst diese aufgaben lösen, sonst brauch ich erst gar nicht weiter lernen..

kannst du (oder auch jemand anders ) mal den datenmist korrigieren den ich da habe? ich verstehe nämlich nicht woran es hängt. wär echt lieb







Java:
/**
 * In this game, I will first choose a secret random number, and one of us (either you or me) must guess it iteratively... <br/>
 * All I will reveal for a guess is whether the guess is higher or lower than the secret random number in my mind.<br/>
 * You may use everything that is provided to you in this class, but<br/>
 * DO NOT CHANGE ANYTHING except for the bodies of the methods marked with "ToDo"!
 * 
 */
public class ChildrensGame {
	/**
	 * The smallest possible number I may choose.
	 */
	protected final int MIN;

	/**
	 * The greatest possible number I may think of.
	 */
	protected final int MAX;

	/**
	 * Just for statistics...<br/>
	 */
	protected long numberOfGuesses = 0;

	/**
	 * The secret number between {@link #MIN} and {@link #MAX} (both inclusive) to be guessed.
	 */
	protected final int toBeGuessed;

	/**
	 * Let the games begin (here)!<br/>
	 * There is no need for you to change this method in any way (but you should test your code with different min/max-values...)!<br/>
	 * The secret number to be guessed is chosen between {@link #MIN} and {@link #MAX} (both inclusive).<br/>
	 * @see <a href="http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Math.html#random()">Math.random()</a>
	 * @param args If you don't provide one (whatever), I'll play by myself - otherwise you can puzzle it out...
	 */
	public static void main(String[] args) {
		int min = 1;
		int max = 100;
		int toBeGuessed = min + ((int)(Math.random() * (max - min + 1)));
		ChildrensGame childrensGame = new ChildrensGame(min, max, toBeGuessed);
		if (args.length > 0) {
			childrensGame.playWithYou();
		} else {
			childrensGame.playByMyself();
		}
	}

	/**
	 * Initialises a new game.<br/>
	 */
	protected ChildrensGame(int min, int max, int toBeGuessed) {
		this.MIN = min;
		this.MAX = max;
		this.toBeGuessed = toBeGuessed;
	}

	/**
	 * Checks whether {@code myGuess} is less than, equal or greater than the real secret number {@link #toBeGuessed} and returns the "encoded result" correspondingly.
	 * @param myGuess This is the guess to be evaluated.
	 * @return
	 * <table>
	 * <tr><td>-1</td><td>: if {@code myGuess} is less than the secret value {@link #toBeGuessed}.</td></tr>
	 * <tr><td>0</td><td>: if {@code myGuess} is exactly the the secret value <i>(yeah!)</i></td></tr>
	 * <tr><td>1</td><td>: if {@code myGuess} is greater than the secret value.</td></tr>
	 * </table>
	 */
	protected int checkGuess(int myGuess) {
		int result;

	  
				
				  if(myGuess < toBeGuessed)
				    result = -1;
				  else if(myGuess > toBeGuessed)
				    result = 1;
				  else
				    result = 0;
				 
			

		System.out.println("The " + ++numberOfGuesses + ". guess is \"" + myGuess + "\" and it is " + (result < 0 ? "too small." : result > 0 ? "too high." : "PERFECT!"));
		return result;
	}

	/**
	 * I'll choose the secret number {@link #toBeGuessed} and a human player must try to guess it.
	 */
	protected void playWithYou() {{


		    int toBeGuessed;    
		    int myGuess;        
		    int numberOfGuesses;       
		    boolean checkGuess = false;}     
		   
		   

		    public playWithYou()   
		      numberOfGuesses = 1;           
		        
		        generateRandomNum();    
		        System.out.println("Please tell mir your Guess "); 
		    
		    
		        
		        
		        while(checkGuess(0)!=toBeGuessed){          
	              
		            
		                readInput();    
		                compare();     
		        
		        }}
		    
		    private void compare() {
		
		
	}

			private void readInput() {
		
		
	}

			public void generateRandomNum(){
		        System.out.println("***Zufallszahl wurde erzeugt***");
		        Random ran = new Random();
		        toBeGuessed = ran.nextInt(101);
		         
		    
		    
		    public void readInput();{   
		                         
		        try{
	
		 myGuess = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
		        }catch(Exception e){
		            e.printStackTrace();
		        }
		    }
		    
		    public void compare();               
		    
		        if (myGuess < toBeGuessed){
		            System.out.println("The number is too small");
		            numberOfGuesses++;      
		        }
		        else if (myGuess > toBeGuessed){
		            System.out.println("Your guess is to high");
		            numberOfGuesses++;
		        }
		        else{
		            System.out.println("Perfect. The number of Guesses is " + numberOfGuesses );
		            numberOfGuesses++;
		            checkGuess = true;           

		        }}}
		    
		
	

	/**
	 * I'll just pretend that I don't know the value {@link #toBeGuessed} and puzzle myself over it just like a human player.<br/>
	 * I will do my best to find the secret number within as few steps as possible!
	 */
	protected void playByMyself(){

	}
			//  TODO
	/**
	 * Helper infrastructure, used to read human input.
	 */
	protected int inputInt() {
		try {
			java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			while(true) {
				System.out.print("Tell me your guess: ");
				String inputString = reader.readLine();
				if (inputString == null) throw new java.io.EOFException();
				try {
					return Integer.parseInt(inputString);
				} catch (NumberFormatException e) {
					System.out.println("- Your input (\"" + inputString + "\") is not a valid number! Try again...");
				}
			}
		} catch (Throwable throwable) {
			System.err.println();
			System.err.println("- Sorry, it just doesn't work here with you... I'll shut down now!");
			System.exit(1);
			return 0;
		}
	}
}
 
Zuletzt bearbeitet:

slawaweis

Bekanntes Mitglied
ich beende dann diese unendliche Geschichte. So sieht eine mögliche funktionierende Lösung aus, wobei man nur die TODO-Stellen verändert. Du musst jetzt nur noch schaffen, es per Copy & Paste in deinen Quelltext einzufügen und das Programm auszuführen.

Java:
 protected int checkGuess(int myGuess)
  {
  int result = 0;

  if(myGuess < toBeGuessed)
    result = -1;
  else if(myGuess > toBeGuessed)
    result = 1;
  else
    result = 0;

  System.out.println("The " + ++numberOfGuesses + ". guess is \"" + myGuess + "\" and it is "
                     + (result < 0 ? "too small." : result > 0 ? "too high." : "PERFECT!"));
  return result;
  }

 protected void playWithYou()
  {
  while(true)
       {
       int myGuess = inputInt();

       int result = checkGuess(myGuess);

       if(result == 0)
         {
         break;
         }
       }
  }

 protected void playByMyself()
  {
  int min = MIN;
  int max = MAX;

  while(true)
       {
       int myGuess = min + ((max - min + 1) / 2);

       int result = checkGuess(myGuess);

       if(result == -1)
         {
         min = myGuess;
         }
       else if(result == 1)
         {
         max = myGuess;
         }
       else
         {
         break;
         }
       }
  }

sonst werde ich es nie verstehen..
Dir fehlen die einfachsten Grundlagen. Wie man ein Programm startet, wie Parameter an ein Programm übergeben werden, was eine Funktion ist, was ein Block ist, wie ein Schleife funktioniert, wie if else funktioniert, was Klassenvariablen sind usw. Ich kann es mir vorstellen, dass man so eine Aufgabe schon nach 4 Stunden Vorlesung und 2 Stunden Tutorium bekommen kann. Wenn bei euch eine Vorlesung ausgefallen ist, dann muss es nachgeholt werden, bevor ihr so eine Aufgabe bekommt. Aber auch mit Büchern oder einfach Google konnte man es alles herausfinden, denn es sind wirklich die ersten Schritte, vor allem da alles komplizierte bereits in Rahmencode vorhanden war und Du nur die Sache wie in diesem Post zu schreiben hättest. Dazu braucht man auch einwenig Ehrgeiz. Das letzte vermisste ich bei Dir komplett. Du hast ständig "jetzt verstehe ich mehr" oder "jetzt wird es klarer" geschrieben, aber man konnte deinen Posts es direkt ansehen, dass es nicht der Fall war. Du warst höfflich und freundlich, aber letztendlich hast Du nur darauf gewartet, dass Dir jemand die ganze Lösung schreibt. Nach 5 Seiten und 3 Tagen hast Du es geschafft, Gratulation. Denke aber nicht, dass es auch weiterhin so funktioniert. Google mit den passenden Stichwörtern ist dein bester Freund. Ich habe es auch so gelernt, jedenfalls seit es Google gibt :)

Slawa
 

timbeau

Gesperrter Benutzer
Was studierst du eigentlich? Ich nehme mal an sowas wie Wirtschaftsinfo? ODer noch weiter von Informatik entfernt? Also musst du Java lernen oder nur einen Kurs paar Aufgaben abgeben?
 

Neue Themen


Oben