2016-11-14 1 views
0

Ich erstelle ein Programm, mit dem Sie ein Spiel ähnlich wie Papier, Schere Rock spielen können.Mit While-Schleife in Programm

Das Spiel funktioniert, aber ich versuche, eine Schleife in den Code zu integrieren, der den Benutzer fragt, ob sie weiterspielen möchten.

Wenn ja, werden sie nach einem anderen Eingang gefragt. Wenn nein, wird das Programm einfach "Danke für die Wiedergabe von" state

Hier ist der Code:

import java.util.Scanner; 
import java.util.Random; 
public class OkekpeJMoropinzee 
{ 
    public static void main(String[]args) 
    { 

    String yourMove; 
    String compMove; 


    int compInt; 





    String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"}; 

    Scanner input = new Scanner(System.in); 
    Random rand = new Random(6); 

    compInt = rand.nextInt(5)+1; 

    if (compInt == 1) 
     compMove = "Monkey"; 
    else if (compInt == 2) 
     compMove = "Robot"; 
    else if (compInt == 3) 
     compMove = "Pirate"; 
    else if (compInt == 4) 
     compMove = "Ninja"; 
    else if (compInt == 5) 
     compMove = "Zombie"; 




    System.out.println("What do you choose?: "); 
    yourMove = input.next(); 
    //MONKEY 
    if(yourMove == "Monkey" || compInt == 1) 
     System.out.println("Tie"); 
    else if (yourMove== "Monkey" || compInt == 2) 
     System.out.println("You Win! Monkey Unplugs Robot!"); 
    else if (yourMove=="Monkey" || compInt == 3) 
     System.out.println("You Lose! Pirate Skewers Monkey!"); 
    else if (yourMove == "Monkey" || compInt==4) 
     System.out.println("You Win! Monkey fools Ninja!"); 
    else if (yourMove== "Monkey" || compInt==5) 
     System.out.println("You Lose! Zombie savages monkey!"); 

    //RoBOT 
    else if(yourMove == "Robot" || compInt == 2) 
     System.out.println("Tie"); 
    else if (yourMove== "Robot" || compInt == 1) 
     System.out.println("You Lose! Monkey Unplugs Robot!"); 
    else if (yourMove=="Robot" || compInt == 3) 
     System.out.println("You Lose! Pirate Drowns Robot!!"); 
    else if (yourMove == "Robot" || compInt==4) 
     System.out.println("You Win! Robot Chokes Ninja"); 
    else if (yourMove== "Robot" || compInt==5) 
     System.out.println("You win! Robot Crushes Zombie!"); 

    //PIRATE 
    else if(yourMove == "Pirate" || compInt == 3) 
     System.out.println("Tie"); 
    else if (yourMove== "Pirate" || compInt == 1) 
     System.out.println("You Win! Pirate Skewers Monkey!"); 
    else if (yourMove=="Pirate" || compInt == 2) 
     System.out.println("You Win! Pirate Drowns Robot!"); 
    else if (yourMove == "Pirate" || compInt==4) 
     System.out.println("You Lose! Ninja Karate Chops Pirate!"); 
    else if (yourMove== "Pirate" || compInt==5) 
     System.out.println("You Lose! Zombie Eats Pirate!"); 

    //NINJA 
    else if(yourMove == "Ninja" || compInt == 4) 
     System.out.println("Tie"); 
    else if (yourMove== "Ninja" || compInt == 1) 
     System.out.println("You Lose! Monkey Fools Ninja!"); 
    else if (yourMove=="Ninja" || compInt == 2) 
     System.out.println("You Lose! Robot Chokes Ninja!"); 
    else if (yourMove == "Ninja" || compInt==3) 
     System.out.println("You Win! Ninja Karate Chops Pirate!"); 
    else if (yourMove== "Ninja" || compInt==5) 
     System.out.println("You Win! Ninja Decapitates Zombie!"); 

    //ZOMBIE 
    else if(yourMove == "Zombie" || compInt == 5) 
     System.out.println("Tie"); 
    else if (yourMove== "Zombie" || compInt == 1) 
     System.out.println("You Win! Zombie Savages Monkey!"); 
    else if (yourMove=="Zombie" || compInt == 2) 
     System.out.println("You Lose! Robot Crushes Zombie!"); 
    else if (yourMove == "Zombie" || compInt==3) 
     System.out.println("You Win! Zombie Eats Pirate!"); 
    else if (yourMove== "Zombie" || compInt==4) 
     System.out.println("You Lose! Ninja Decapitates Zombie!"); 
    } 
} 

Antwort

0

Nun ... lassen Sie uns hier ehrlich sein. Dein Code funktioniert tatsächlich nicht. Ich meine, ja es läuft, aber es wird das Gameplay definitiv nicht richtig durchführen.

Ihr Zufallsgenerator für die Computerspiel-Zeichenauswahl funktioniert nicht richtig. Sie müssen es auf diese Weise tun:

Random rand = new Random(); 
compInt = rand.nextInt(5)+1; 

Lassen Sie die 6 aus der Initialisierung von Rand.

Die Logik in den Bedingungen für alle IF- und IF/ELSE-Anweisungen ist so eingerichtet, dass zu keiner Zeit Benutzereingaben über das MONKEY-Spielzeichen hinausgehen, weil Sie stattdessen den Operator OR (||) verwenden des AND (& &) Bedieners. Die Art und Weise haben Sie es einrichten, wenn der Computer ROBOT wählt und ein Benutzer wählt ZOMBIE dann die Bedingung darin enthaltenen:

else if (yourMove == "Monkey" || compInt == 2) { 
    System.out.println("You Win! Monkey Unplugs Robot!"); 
} 

wird immer wahr fallen und zeigt die Meldung:

You Win! Monkey Unplugs Robot! 

obwohl die Benutzer hat Zombie eingegeben. Denken Sie daran ... die Bedingung ist im Grunde entweder so eingestellt oder genau das, was Sie nicht wollen. Sie möchten, dass beide Bedingungen zutreffen, daher müssen Sie den UND-Operator (& &) verwenden.

Außerdem:

yourMove == "Monkey" 

immer zu unerwarteten Ergebnissen führen, und hier ist why. Verwenden Sie die String.Equals() -Methode statt, so etwas wie:

yourMove.equals("Monkey") 

Sie erklären die Zeichen Variable als String-Array noch nirgendwo nutzen Sie schon, dass Array. Warum überhaupt stören?So stellen Sie die Zeichen Variable ein Leerzeichen getrennte String und nutzen sie, um zu sehen, ob der Nutzer tatsächlich ein Zeichen innerhalb des Spiels, zum Beispiel geliefert hat:

String characters = "Monkey Robot Pirate Ninja Zombie"; 

yourMove = ""; 
while (yourMove.equals("")) { 
    System.out.println("What do you choose? --> "); 
    yourMove = input.nextLine().toLowerCase(); 
    if (yourMove.equals("quit")) { 
     System.out.println("Thanks for playing. Bye Bye"); 
     System.exit(0); 
    } 
    if (!characters.toLowerCase().contains(yourMove) || yourMove.equals("")) { 
     System.out.println("You entered an invalid Game Character! Try again...\n"); 
     continue; 
    } 
} 

In jedem Fall ist hier ein komplettes Arbeitsspiel:

package okekpejmoropinzee; 

import java.util.Random; 
import java.util.Scanner; 

public class OkekpeJMoropinzee { 
    static boolean playAgain = true; 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 


     while (playAgain == true) { 
      playGame(input); 
     } 

     System.out.println("Thanks for playing"); 
     input.close(); 
    } 

    private static void playGame(Scanner input) { 
     String yourMove = "", compMove = ""; 
     int compInt = 0; 
     String gameCharacters = "Monkey Robot Pirate Ninja Zombie"; 

     Random rand = new Random(); 
     compInt = rand.nextInt(5)+1; 

     if (compInt == 1) { compMove = "Monkey"; } 
     else if (compInt == 2) { compMove = "Robot"; } 
     else if (compInt == 3) { compMove = "Pirate"; } 
     else if (compInt == 4) { compMove = "Ninja"; } 
     else if (compInt == 5) { compMove = "Zombie"; } 
     System.out.println(compMove + " --- " + compInt); 

     yourMove = ""; 
     while (yourMove.equals("")) { 
      System.out.println("What do you choose? --> "); 
      yourMove = input.nextLine().toLowerCase(); 
      if (yourMove.equals("quit")) { 
       System.out.println("Thanks for playing. Bye Bye"); 
       System.exit(0); 
      } 
      if (!gameCharacters.toLowerCase().contains(yourMove) || yourMove.equals("")) { 
       System.out.println("You entered an invalid Game Character! Try again...\n"); 
       continue; 
      } 
     } 

     //MONKEY 
     if (yourMove.equals("monkey") && compInt == 1) { 
      System.out.println("Tie"); 
     } 
     else if (yourMove.equals("monkey") && compInt == 2) { 
      System.out.println("You Win! Monkey Unplugs Robot!"); 
     } 
     else if (yourMove.equals("monkey") && compInt == 3) { 
      System.out.println("You Lose! Pirate Skewers Monkey!"); 
     } 
     else if (yourMove.equals("monkey") && compInt == 4) { 
      System.out.println("You Win! Monkey fools Ninja!"); 
     } 
     else if (yourMove.equals("monkey") && compInt == 5) { 
      System.out.println("You Lose! Zombie savages monkey!"); 
     } 

     //ROBOT 
     else if (yourMove.equals("robot") && compInt == 2) { 
      System.out.println("Tie"); 
     } 
     else if (yourMove.equals("robot") && compInt == 1) { 
      System.out.println("You Lose! Monkey Unplugs Robot!"); 
     } 
     else if (yourMove.equals("robot") && compInt == 3) { 
      System.out.println("You Lose! Pirate Drowns Robot!!"); 
     } 
     else if (yourMove.equals("robot") && compInt == 4) { 
      System.out.println("You Win! Robot Chokes Ninja"); 
     } 
     else if (yourMove.equals("robot") && compInt == 5) { 
      System.out.println("You win! Robot Crushes Zombie!"); 
     } 

     //PIRATE 
     else if (yourMove.equals("pirate") && compInt == 3) { 
      System.out.println("Tie"); 
     } 
     else if (yourMove.equals("pirate") && compInt == 1) { 
      System.out.println("You Win! Pirate Skewers Monkey!"); 
     } 
     else if (yourMove.equals("pirate") && compInt == 2) { 
      System.out.println("You Win! Pirate Drowns Robot!"); 
     } 
     else if (yourMove.equals("pirate") && compInt == 4) { 
      System.out.println("You Lose! Ninja Karate Chops Pirate!"); 
     } 
     else if (yourMove.equals("pirate") && compInt == 5) { 
      System.out.println("You Lose! Zombie Eats Pirate!"); 
     } 

     //NINJA 
     else if(yourMove.equals("ninja") && compInt == 4) { 
      System.out.println("Tie"); 
     } 
     else if (yourMove.equals("ninja") && compInt == 1) { 
      System.out.println("You Lose! Monkey Fools Ninja!"); 
     } 
     else if (yourMove.equals("ninja") && compInt == 2) { 
      System.out.println("You Lose! Robot Chokes Ninja!"); 
     } 
     else if (yourMove.equals("ninja") && compInt == 3) { 
      System.out.println("You Win! Ninja Karate Chops Pirate!"); 
     } 
     else if (yourMove.equals("ninja") && compInt == 5) { 
      System.out.println("You Win! Ninja Decapitates Zombie!"); 
     } 

     //ZOMBIE 
     else if(yourMove.equals("zombie") && compInt == 5) { 
      System.out.println("Tie"); 
     } 
     else if (yourMove.equals("zombie") && compInt == 1) { 
      System.out.println("You Win! Zombie Savages Monkey!"); 
     } 
     else if (yourMove.equals("zombie") && compInt == 2) { 
      System.out.println("You Lose! Robot Crushes Zombie!"); 
     } 
     else if (yourMove.equals("zombie") && compInt==3) { 
      System.out.println("You Win! Zombie Eats Pirate!"); 
     } 
     else if (yourMove.equals("zombie") && compInt==4) { 
      System.out.println("You Lose! Ninja Decapitates Zombie!"); 
     } 

     //Ask if User wants to play the game again... 
     String playMore = ""; 
     while (!playMore.equals("y") && !playMore.equals("n")) { 
      System.out.println("\nDo you want to play another game? (y/n) "); 
      playMore = input.nextLine().toLowerCase(); 
     } 
     if (playMore.equals("n")) { playAgain = false; } 
    } 
} 
+0

Wow, das war extrem hilfreich. Vielen Dank! Ich dachte, dass mein Code funktionierte, bis ich tatsächlich versuchte, es zu spielen, und es funktionierte nicht, lol. Vielen Dank für Ihre Hilfe –

1

Sie die gesamte Logik in einem do-while-Schleife setzen könnten. Die Bedingung ist, wenn das eingegebene Zeichen == 'y' oder 'Y' ist. Der Pseudo-Code sollte sein:

char choice='n'; 
do 
{ 
    < Insert Game logic here > 

    System.out.println("Do you wanna continue? Enter y or Y for Yes")' 
    choice = <obtain input using Scanner here>; 
} 
while(choice=='y'||choice=='Y'); 

System.out.println("Thanks for Playing"); 
0

Sie so etwas tun könnte:

import java.util.Scanner; 
import java.util.Random; 
public class OkekpeJMoropinzee 
{ 
    public static void main(String[]args) 
    { 
     String playAgain; 
    do{ 
     String yourMove; 
     String compMove; 


     int compInt; 





     String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"}; 

     Scanner input = new Scanner(System.in); 
     Random rand = new Random(6); 

     compInt = rand.nextInt(5)+1; 

     if (compInt == 1) 
      compMove = "Monkey"; 
     else if (compInt == 2) 
      compMove = "Robot"; 
     else if (compInt == 3) 
      compMove = "Pirate"; 
     else if (compInt == 4) 
      compMove = "Ninja"; 
     else if (compInt == 5) 
      compMove = "Zombie"; 




     System.out.println("What do you choose?: "); 
     yourMove = input.next(); 
     //MONKEY 
     if(yourMove == "Monkey" || compInt == 1) 
      System.out.println("Tie"); 
     else if (yourMove== "Monkey" || compInt == 2) 
      System.out.println("You Win! Monkey Unplugs Robot!"); 
     else if (yourMove=="Monkey" || compInt == 3) 
      System.out.println("You Lose! Pirate Skewers Monkey!"); 
     else if (yourMove == "Monkey" || compInt==4) 
      System.out.println("You Win! Monkey fools Ninja!"); 
     else if (yourMove== "Monkey" || compInt==5) 
      System.out.println("You Lose! Zombie savages monkey!"); 

     //RoBOT 
     else if(yourMove == "Robot" || compInt == 2) 
      System.out.println("Tie"); 
     else if (yourMove== "Robot" || compInt == 1) 
      System.out.println("You Lose! Monkey Unplugs Robot!"); 
     else if (yourMove=="Robot" || compInt == 3) 
      System.out.println("You Lose! Pirate Drowns Robot!!"); 
     else if (yourMove == "Robot" || compInt==4) 
      System.out.println("You Win! Robot Chokes Ninja"); 
     else if (yourMove== "Robot" || compInt==5) 
      System.out.println("You win! Robot Crushes Zombie!"); 

     //PIRATE 
     else if(yourMove == "Pirate" || compInt == 3) 
      System.out.println("Tie"); 
     else if (yourMove== "Pirate" || compInt == 1) 
      System.out.println("You Win! Pirate Skewers Monkey!"); 
     else if (yourMove=="Pirate" || compInt == 2) 
      System.out.println("You Win! Pirate Drowns Robot!"); 
     else if (yourMove == "Pirate" || compInt==4) 
      System.out.println("You Lose! Ninja Karate Chops Pirate!"); 
     else if (yourMove== "Pirate" || compInt==5) 
      System.out.println("You Lose! Zombie Eats Pirate!"); 

     //NINJA 
     else if(yourMove == "Ninja" || compInt == 4) 
      System.out.println("Tie"); 
     else if (yourMove== "Ninja" || compInt == 1) 
      System.out.println("You Lose! Monkey Fools Ninja!"); 
     else if (yourMove=="Ninja" || compInt == 2) 
      System.out.println("You Lose! Robot Chokes Ninja!"); 
     else if (yourMove == "Ninja" || compInt==3) 
      System.out.println("You Win! Ninja Karate Chops Pirate!"); 
     else if (yourMove== "Ninja" || compInt==5) 
      System.out.println("You Win! Ninja Decapitates Zombie!"); 

     //ZOMBIE 
     else if(yourMove == "Zombie" || compInt == 5) 
      System.out.println("Tie"); 
     else if (yourMove== "Zombie" || compInt == 1) 
      System.out.println("You Win! Zombie Savages Monkey!"); 
     else if (yourMove=="Zombie" || compInt == 2) 
      System.out.println("You Lose! Robot Crushes Zombie!"); 
     else if (yourMove == "Zombie" || compInt==3) 
      System.out.println("You Win! Zombie Eats Pirate!"); 
     else if (yourMove== "Zombie" || compInt==4) 
      System.out.println("You Lose! Ninja Decapitates Zombie!"); 

     System.out.println("Would you like to play again? Type yes to play again."); 
     playAgain = input.next(); 
     } while(playAgain.equals("yes")); 
    } 

} 
+0

danke das hat perfekt funktioniert –

+0

Mein Vergnügen. Froh, dass ich helfen konnte :) – BlackHatSamurai