Question
The promptForDragon() method is not working. When the user inters an empty line, this should get printed to the screen Error - dragon prompt
/**
* YOUR DESCRIPTION OF THIS PROGRAM HERE
* @author YOUR NAME HERE
* @version DATE HERE
*/
import java.util.Random;
import java.util.Scanner;
public class DragonTrainers {
private static final String[] DRAGONS = { "Plant", "Water", "Fire" };
public static int getNumberOfMatches(Scanner keyboard) {
System.out.print("How many matches will we play?");
int matches=0;
int cc =0;
matches=keyboard.nextInt();
while (cc!= -1){
if(matches>0){
cc = -1;
break;
}
else{
System.out.println(" ERROR - number of matches must be positive!");
System.out.print("How many matches will we play?");
matches=keyboard.nextInt();
keyboard.nextLine();
}
}
return matches;
}
public static String promptForDragon(Scanner keyboard) {
System.out.print("Please select a dragon [Plant/Water/Fire]: ");
String s="";
while(true){
s=input.nextLine();
if(!s.equals("")){
break;
}
else{
System.out.print("ERROR - Dragon prompt cannot be empty.");
}
}
return s;
}
public static int dragonToNumber(char dragon) {
if(dragon=='P'){
return 0;
}else if(dragon=='W'){
return 1;
}else if(dragon=='F'){
return 2;
}
return -1;
}
public static int determineWinner(int player, int cpu) {
if(player==cpu){
return 0;
}else if(player==0){
if(cpu==1){
return 1;
}else{
return -1;
}
}else if(player==1){
if(cpu==2){
return 1;
}else{
return -1;
}
}else if(player==2){
if(cpu==0){
return 1;
}else{
return -1;
}
}
return -1;
}
public static void displayMatchResult(int player, int cpu, int winner) {
if(winner==1){
System.out.println(DRAGONS[player]+" defeats "+DRAGONS[cpu]+" - you win!");
}
else if(winner==-1){
System.out.println(DRAGONS[cpu]+" defeats "+DRAGONS[player]+" - you lose!");
}
else{
System.out.println("A Tie!");
}
}
public static void displayFinalResult(int wins, int losses, int ties) {
System.out.println("The tournament is over!");
System.out.println("We tied "+ties+" matches.");
System.out.println("I won "+losses+" matches.");
System.out.println("You won "+wins+" matches.");
if(losses>wins){
System.out.println("I am the winner!");
}
else if(losses
System.out.println("You are the winner!");
}
else{
System.out.println("Neither of us can claim victory here!");
}
}
public static void main(String[] args) {
// Prompt for a random number seed
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a random seed: ");
int seed = Integer.parseInt(keyboard.nextLine());
// Create a Random instance with the seed
Random rnd = new Random(seed);
// Prompt for number of matches to play
int totalMatches = getNumberOfMatches(keyboard);
// Start with wins, losses and ties at 0.
// Repeat until all matches have been played (use the sum of the
// results so we don't need another variable)
int wins = 0, losses = 0, ties = 0;
while ((wins + losses + ties) < totalMatches) {
// Ask the user for a dragon to use
String input = promptForDragon(keyboard);
// Get the first character of the user's input as an uppercase
// value.
char dChar = input.toUpperCase().charAt(0);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started