Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to make the Canceled Input Execute when a player inputs an invalid character that matches whatever null means. Is there a way this

I want to make the "Canceled Input" Execute when a player inputs an invalid character that matches whatever "null" means.

Is there a way this can be improved?

image text in transcribed

import java.util.*; import javax.swing.*;

public class Lab3 { static final boolean $DEBUG = true; static final String empty = ""; public static void main(String[] args) { boolean win = false, lose = false; boolean done = false, invalid = false; final int numGuesses = 3; int leftGuesses = numGuesses, count = 0; String outMsg=""; String outMsg2=""; if($DEBUG) System.out.println("starting main..."); String[] avatars = {"Batman", "Superman" , "Xman" , "Wonderwoman"}; int avNum = JOptionPane.showOptionDialog(null, "Choose player name", "buttons",0 ,0 ,null, avatars, avatars[3]); String playerName = avatars[avNum]; JOptionPane.showMessageDialog(null, "Welcome " + playerName + "!"); System.out.println("now Playing: " + playerName); // **add code: console input secret name (admin) Scanner input = new Scanner(System.in); JOptionPane.showInputDialog(null, "Admin enter secret name: "); String secretName; System.out.println("admin enter secret name: "); secretName = input.nextLine(); JOptionPane.showMessageDialog(null, "Your secret name is " + secretName); System.out.println("secret name= "+ secretName); //start "for" loop for(int i = 0; i 0 ) { outMsg = "incorrect"; System.out.println(outMsg); } else{ outMsg = "Incorrect! You LOST! Good-bye!"; System.out.println(outMsg); JOptionPane.showMessageDialog(null, outMsg); // Ask if they want to continue int cont = JOptionPane.showConfirmDialog(null, "Do you want to Try Again?"); switch (cont) { case 0: //yes-- cont lopp break; //end switch (not loop) case 1: /o-- exit loop JOptionPane.showMessageDialog(null, "Game Over! GoodBye"); done = true; //or use System.exit(0) break; //end switch default: //cancel or close-- }//end switch }//end loop if($DEBUG) System.out.println("Ending Main..."); }//end main }//end class

1/3 cases: win, incorrect, lose || //check for valid inputs - if(guessName.equals IgnoreCase( secretName) win = true; break; else if (guessName == null){ outMsg = "Canceled input"; JOptionPane.showMessageDialog(null, outMsg); invalid = true; else if (guessName.equals(empty)) { outMsg = "Blank input"; | JOptionPane.showMessageDialog(null, outMsg); invalid = true; if (invalid) { if (leftGuesses

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions