2017-07-24 1 views
3

Hallo Leute, ich brauche Hilfe bei dieser Frage: Ein Würfel Rollenspiel wird mit zwei sechsseitigen Würfeln gespielt. Ein Benutzer, der das Spiel spielt, würfelt die zwei Würfel und zwei Zufallszahlen zwischen eins und sechs werden erzeugt. Die Summe der zwei Zahlen wird verwendet, um den nächsten Schritt zu entscheiden. Wenn die Summe 2,3 oder 12 ist, gewinnt der Spieler. Wenn die Summe 7 oder 11 ist, verliert er/sie. Wenn die Summe 4, 5, 6, 8, 9 oder 10 ist, würfelt das Programm automatisch erneut, bis der Spieler gewinnt oder verliert. Nach jedem Würfelwurf wird der Spieler aufgefordert, eine Eingabe zu machen. Der Spieler sollte entscheiden, ob das Spiel fortgesetzt werden soll oder nicht. Die Anzahl der gewonnenen und verlorenen Spiele sollte auch nach jedem Würfelwurf angezeigt werden. Ich habe es geschafft, den ersten Teil an die Arbeit, aber nicht in der Lage, herauszufinden, wie der Benutzer aufgefordert, wenn sie oder fortsetzen wollen, wie viele Spiele, die sie gewonnen haben/verlorenWie frage ich, ob der Benutzer das Spiel nach jedem Wurf in Java mit netbeans fortsetzen möchte

public class DiceGame { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     while (true) { 
      int dice1=(int)(Math.random()*6+1); 
      int dice2=(int)(Math.random()*6+1); 
      int sum = dice1 + dice2; 

      System.out.println("Roll: total = " + sum); 

      if (sum==2 || sum==3 || sum==12) { 
       System.out.println("Sorry with a " + sum + " you loose :("); 
       break; 
      } 
      else if(sum==7 || sum==11) { 
       System.out.println("With a " + sum + " you win :)"); 
       break; 
      } 
     } 
    } 

} 
+0

Make eine if-Anweisung, wo das Programm wird Sie fragen, dass, und wenn Sie sich entscheiden, dass Sie nur wollen, fügen Sie nicht weiter ein 'brechen;'. Ich nehme an, Sie wissen bereits, wie man Daten von der Standardeingabe und all dem Zeug erhält. –

+0

@Bec - meine Lösung hat beide Geschmacksrichtungen, die Sie brauchen (mit Benutzereingabe durch den Scanner und keine Benutzereingabe mit automatischem Rollen, wenn 4 5 6 8 9 10).Bitte akzeptieren Sie meine Antwort und stimmen Sie ab, wenn es für Sie funktioniert! – JRG

+0

@Bec - bitte akzeptieren Sie meine Antwort und stimmen Sie ab, wenn es Ihr Problem gelöst hat! – JRG

Antwort

3

Scanner kann verwendet werden, Benutzer aufzufordern, zu fragen, ob er/sie oder nicht fortsetzen will, .

Sie können sogar Nr. Verfolgen. von Zeiten wurden Würfel gerollt.

import java.util.Scanner; 

public class DiceGame { 

    public static int attempt = 1; 
    /** 
    * @param args 
    *   the command line arguments 
    */ 
    public static void main(String[] args) { 

     Scanner scanner = new Scanner(System.in); 
     int dice1 = (int) (Math.random() * 6 + 1); 
     int dice2 = (int) (Math.random() * 6 + 1); 
     int sum = dice1 + dice2; 

     while (true) { 
      System.out.println(); 
      System.out.println("Rolling dice for " + attempt + " time!"); 

      dice1 = (int) (Math.random() * 6 + 1); 
      dice2 = (int) (Math.random() * 6 + 1); 
      sum = dice1 + dice2; 

      System.out.println("Roll: total = " + sum); 

      if (sum == 2 || sum == 3 || sum == 12) { 
       System.out.println("Sorry with a " + sum + " you loose :(!"); 
       System.out.println(); 
       break; 
      } else if (sum == 7 || sum == 11) { 
       System.out.println("With a " + sum + " you win :)!"); 
       System.out.println(); 
       break; 
      } 
      System.out.println(); 
      System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT"); 
      if (!scanner.next().equalsIgnoreCase("y")) { 
       break; 
      } 
      attempt++; 
     } 
     System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!"); 
    } 
} 

EDIT: Wenn Sie die Würfel automatisch rollen soll, wenn die Summe 4 5 6 8 9 10 dann würden Sie nicht mehr benötigen Scanner heißt Benutzereingabe, ob fortzusetzen oder nicht.

Hier ist die Lösung für das gleiche.

public class DiceGame { 

    public static int attempt = 1; 

    public static void main(String[] args) { 

     int dice1 = 0; 
     int dice2 = 0; 
     int sum = 0; 

     while (true) { 
      System.out.println(); 
      System.out.println("Rolling dice for " + attempt + " time!"); 

      dice1 = (int) (Math.random() * 6 + 1); 
      dice2 = (int) (Math.random() * 6 + 1); 
      sum = dice1 + dice2; 

      System.out.println("Roll: total = " + sum); 

      if (sum == 2 || sum == 3 || sum == 12) { 
       System.out.println("Sorry with a " + sum + " you loose :(!"); 
       System.out.println(); 
       break; 
      } else if (sum == 7 || sum == 11) { 
       System.out.println("With a " + sum + " you win :)!"); 
       System.out.println(); 
       break; 
       // this will roll the dices automatically 
       // when sum is 4, 5, 6, 8, 9 or 10 
      } else { 
       System.out.println(); 
       System.out.println("With " + sum + " dices are rolled again automatically!!"); 
       attempt++; 
      } 
     } 
     System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!"); 
    } 
} 

Sample Run

Rolling dice for 1 time! 
Roll: total = 4 

With a 4, dices are rolled again automatically!! 

Rolling dice for 2 time! 
Roll: total = 6 

With a 6, dices are rolled again automatically!! 

Rolling dice for 3 time! 
Roll: total = 2 
Sorry with a 2 you loose :(! 

Thanks for playing dice game, you rolled the dice 3 times! 
+0

hallo das halb arbeitete es Staaten Versuch nicht existieren und ich bin nicht sicher, was ich hier setzen int Versuch =; damit es zeigt, wie oft Würfel gerollt wurden – Bec

+0

Sie müssen Versuche erklären und initialisieren wie 1, wie ich, public static int versuch = 1; – JRG

+0

Wenn die Summe 4, 5, 6, 8, 9 oder 10 ist, würfelt das Programm automatisch erneut, bis der Spieler gewinnt oder verliert. Weißt du wie das geht? – Bec

1

Sie Java-Klasse Scanner verwenden können, zu veran Benutzer für Eingaben.

Innerhalb Ihrer while-Schleife den Benutzer nach den Eingaben fragen. Möglicherweise müssen Sie Ihre while-Schleife ändern.

Ausgehend von den Benutzereingaben verwenden Sie "Bremse", um die Schleife zu verlassen. Das sollte es tun.

Scanner Reference

1

Ich habe eine playAgainMessage Methode, die einen int zurückgibt. Dies entscheidet, ob der Benutzer wieder spielen wird oder nicht. Check it out:

import java.util.Scanner; 

public class DiceGame { 
    private static final int PLAY = 1; 
    private static final int DO_NOT_PLAY = 2; 
    private static int choice = 1; 

    public static void main(String[] args) { 
     while (choice == PLAY) { 
      int dice1=(int)(Math.random()*6+1); 
      int dice2=(int)(Math.random()*6+1); 
      int sum = dice1 + dice2; 

      System.out.println("Roll: total = " + sum); 

      if (sum==2 || sum==3 || sum==12) { 
       System.out.println("Sorry with a " + sum + " you loose :("); 
       choice = playAgainMessage(); 
      } 
      else if(sum==7 || sum==11) { 
       System.out.println("With a " + sum + " you win :)"); 
       choice = playAgainMessage(); 
      } 
     } 
     if (choice == DO_NOT_PLAY) { 
      System.out.println("Good bye..."); 
     } 
    } 

    private static int playAgainMessage() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Would you like to play again? '1' for yes, '2' for no"); 
     int choice = input.nextInt(); 
     input.close(); 
     if(choice == PLAY){ 
      System.out.println("Rolling..."); 
      return PLAY; 
     } else { 
      return DO_NOT_PLAY; 
     } 
    } 
} 

Sie die playAgainMessage Methode aufrufen können, wenn Sie möchten, die es schön und leicht zu lesen macht.

Dies wird so lange wiedergegeben, bis der Benutzer etwas anderes als "1" eingibt. Versuchen Sie es auszuführen, um die genaue Ausgabe zu sehen.

1

können Sie verwenden, um eine do{}while(); Schleife, wird es spielen einmal, und dann bitten Sie den Benutzer, um es wieder:

Scanner sc = new Scanner(System.in); 
String input = ""; 
do { 
    int // ... 

    System.out.println("Roll: total = " + sum); 

    if (sum == 2 || sum == 3 || sum == 12) { 
     // ... 
    } else if (sum == 7 || sum == 11) {  
     // ... 
    } 

    System.out.println("If you want to continue, write 'y' : "); 
    input = sc.nextLine(); 
} while (input.equalsIgnoreCase("y")); 
Verwandte Themen