Question

In: Computer Science

Rewrite this code of a game of Moropinzee so that it works as intended without the...

Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code.

Code:

import java.util.*;

public class Moropinzee

{

public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);

while(true)

{

System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");

int p1 = sc.nextInt();

while(p1<1 || p1>5)

{

System.out.println("Invalid choice, Player 1. Enter a number 1-5:");

p1 = sc.nextInt();

}

System.out.println("Player 2 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");

int p2 = sc.nextInt();

while(p2<1 || p2>5)

{

System.out.println("Invalid choice, Player 2. Enter a number 1-5:");

p2 = sc.nextInt();

}

if((p1==1 && p2==4)||(p1==4 && p2==1))

{

System.out.print("Monkey fools Ninja. ");

if(p1==1)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==1 && p2==2)||(p1==2 && p2==1))

{

System.out.print("Monkey unplugs Robot. ");

if(p1==1)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==2 && p2==4)||(p1==4 && p2==2))

{

System.out.print("Robot chokes Ninja. ");

if(p1==2)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==2 && p2==5)||(p1==5 && p2==2))

{

System.out.print("Robot crushes Zombie. ");

if(p1==2)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==3 && p2==2)||(p1==2 && p2==3))

{

System.out.print("Pirate drowns Robot. ");

if(p1==3)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==3 && p2==1)||(p1==1 && p2==3))

{

System.out.print("Pirate skewers Monkey. ");

if(p1==3)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==4 && p2==3)||(p1==3 && p2==4))

{

System.out.print("Ninja karate chops Pirate. ");

if(p1==4)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==4 && p2==5)||(p1==5 && p2==4))

{

System.out.print("Ninja decapitates Zombie. ");

if(p1==4)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==5 && p2==3)||(p1==3 && p2==5))

{

System.out.print("Zombie eats Pirate. ");

if(p1==5)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if((p1==5 && p2==1)||(p1==1 && p2==5))

{

System.out.print("Zombie savages Monkey. ");

if(p1==1)

{

System.out.println("Player 1 wins!");

}

else

{

System.out.println("Player 2 wins!");

}

}

else if(p1 == p2)

System.out.println("Nobody wins");

System.out.println("Would you like to play again?");

sc.nextLine();//goto next line

String s = sc.nextLine();//take input from user

if((s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y")) == false)

break;

}

System.out.println("Thanks for playing!");

}

}

Solutions

Expert Solution

If you don't want to use break when user entered other than y or yes then alternate solution is use do while loop.

first time code runs that is in do loop and continue runs until while condition satisfies.

Below is the modified code without the use of break satement.I have created java project named Moropinzee in NetBeans IDE.

package moropinzee;
import java.util.*;

public class Moropinzee {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //declare variable s that is used to store choice entered by user
        String s;
        do {
            System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
            int p1 = sc.nextInt();
            while (p1 < 1 || p1 > 5) {
                System.out.println("Invalid choice, Player 1. Enter a number 1-5:");
                p1 = sc.nextInt();
            }

            System.out.println("Player 2 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
            int p2 = sc.nextInt();
            while (p2 < 1 || p2 > 5) {
                System.out.println("Invalid choice, Player 2. Enter a number 1-5:");
                p2 = sc.nextInt();
            }

            if ((p1 == 1 && p2 == 4) || (p1 == 4 && p2 == 1)) {
                System.out.print("Monkey fools Ninja. ");

                if (p1 == 1) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 1 && p2 == 2) || (p1 == 2 && p2 == 1)) {
                System.out.print("Monkey unplugs Robot. ");
                if (p1 == 1) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 2 && p2 == 4) || (p1 == 4 && p2 == 2)) {
                System.out.print("Robot chokes Ninja. ");
                if (p1 == 2) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 2 && p2 == 5) || (p1 == 5 && p2 == 2)) {
                System.out.print("Robot crushes Zombie. ");
                if (p1 == 2) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 3 && p2 == 2) || (p1 == 2 && p2 == 3)) {
                System.out.print("Pirate drowns Robot. ");
                if (p1 == 3) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 3 && p2 == 1) || (p1 == 1 && p2 == 3)) {
                System.out.print("Pirate skewers Monkey. ");
                if (p1 == 3) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 4 && p2 == 3) || (p1 == 3 && p2 == 4)) {
                System.out.print("Ninja karate chops Pirate. ");
                if (p1 == 4) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 4 && p2 == 5) || (p1 == 5 && p2 == 4)) {
                System.out.print("Ninja decapitates Zombie. ");
                if (p1 == 4) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 5 && p2 == 3) || (p1 == 3 && p2 == 5)) {
                System.out.print("Zombie eats Pirate. ");
                if (p1 == 5) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if ((p1 == 5 && p2 == 1) || (p1 == 1 && p2 == 5)) {
                System.out.print("Zombie savages Monkey. ");
                if (p1 == 1) {
                    System.out.println("Player 1 wins!");
                } else {
                    System.out.println("Player 2 wins!");
                }
            } else if (p1 == p2) {
                System.out.println("Nobody wins");
            }
            System.out.println("Would you like to play again?");
            sc.nextLine();//goto next line
            s = sc.nextLine();//take input from user
            if ((s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y")) == false) {
                break;
            }

        } 
        //checks if user enterd y or yes then continue the game otherwise terminate the loop.
        while (s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y"));

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

OUTPUT:


Related Solutions

question is :rewrite the story completely in layman's terms or plain English so that someone without...
question is :rewrite the story completely in layman's terms or plain English so that someone without a medical or science background would be able to understand. Your translation must be clear and easy to understand. Part 1 – Jack and Jill Two individuals, one with a significant amount of testosterone and one with a large amount of estrogen went for a walk together up a hill. The male had genitalia that enabled the passing of semen and urine through the...
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the...
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the OOP mode (class mode). ########################### lambda trick ##### first: changing label properties: after you've created a label, you ##### may want to change something about it. To do that, use configure method: ## label.configure(text = 'Yes') ## label.configure(bg = 'white', fg = 'black') ### that change the properties of a label called label. ################################################### ## from tkinter import * abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def callback(x):...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
So pretty much I need my code without the arrays, or lists. Please and thank you!...
So pretty much I need my code without the arrays, or lists. Please and thank you! Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task. Checkpoint 3 is a continuation of the “Quiz” Programming Project. This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#. Implement a...
The clause below was written by the seller. You are the buyer. Rewrite it so it...
The clause below was written by the seller. You are the buyer. Rewrite it so it is to your advantage. Delivery; Title; and Risk of Loss. Unless otherwise stated in Exhibit A, the Seller shall deliver the Goods FOB the Seller’s facility and title to and risk of loss of the Goods will pass to the Buyer upon such delivery by the Seller. Any stated delivery dates are approximate. The Seller will not be liable for any losses, damages, penalties,...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Suppose a casino offers a game that costs $20 to play. The game works as follows:...
Suppose a casino offers a game that costs $20 to play. The game works as follows: the dealer will give you a hand (randomly chosen from a regular 52 card deck) of four cards, and you will then win 10X dollars, where X is the number of aces in your hand. What are the expected winnings for this game? The expected profit? Is this a fair game?
Rewrite Program to store the pairs of states and capitals so that the questions are displayed...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed randomly. import java.util.*; public class quiz { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String arr[][]= {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}}; int n =arr.length; int count=0; for(int i=0;i<n;i++) { System.out.printf("What is the capital of %s? ",arr[i][0]); String capital=sc.next(); if (arr[i][1].equalsIgnoreCase(capital)) { count++; System.out.println("Your answer is correct"); } else { System.out.printf("The correct answer should be %s\n",arr[i][1]); } } System.out.printf("The correct count is %d",count); } }
Simple code for a game on C coding.
Simple code for a game on C coding.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT