Question

Using java use a while loop use JOptionPane Scenario – You go to an arcade with...

Using java

use a while loop

use JOptionPane

Scenario – You go to an arcade with 100 tokens, and each game costs a variable number of tokens. Before the loop begins, display how many tokens you have available. Inside the loop, ask the user how many tokens does the next game cost, and subtract that amount from the total tokens if it is less than or equal to the total tokens. Then, state how many tokens are still left.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Arcade.java

import javax.swing.JOptionPane;

public class Arcade {

      public static void main(String[] args) {

            // initializing tokens to 100

            int tokens = 100;

            // displaying a message showing tokens before game begins

            JOptionPane.showMessageDialog(null, "You have " + tokens

                        + " tokens, the game is about to start");

            // loops as long as there is a token available

            while (tokens > 0) {

                  try {

                        //reading number of tokens using an input dialog box

                        int num = Integer

                                    .parseInt(JOptionPane

                                                 .showInputDialog("How many tokens does the next game cost? ("

                                                             + tokens + " available)"));

                        //checking if number exceeds the available tokens

                        if (num > tokens) {

                              //displaying error message

                              JOptionPane.showMessageDialog(null,

                                           "Sorry, that's more than what you have!");

                        } else {

                              //subtracting num from tokens

                              tokens -= num;

                        }

                  } catch (Exception e) {

                        //non numeric input

                        JOptionPane.showMessageDialog(null,

                                    "Sorry, that's an invalid input!");

                  }

            }

            //after exiting loop, displaying a good bye greeting.

            JOptionPane.showMessageDialog(null,

                        "You have no tokens left, thank you for playing!");

      }

}

/*OUTPUT (partial)*/

Add a comment
Know the answer?
Add Answer to:
Using java use a while loop use JOptionPane Scenario – You go to an arcade with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please do num 3,4 and 5. Practice on Selecting the Appropriate Loop: You are playing a she guess lfavorite color variab...

    please do num 3,4 and 5. Practice on Selecting the Appropriate Loop: You are playing a she guess lfavorite color variable guess the favorite color, and com user N e up began. In the loop, display w WaS COfrect or not the 3.) Scenario-You go to an arcade with 100 tokens, and each game costs a variable number of tokens. What loop should you use, and why? Before the loop begins, display how many tokens you have available. Inside the...

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • A simple game of chance Using a while loop write a simple coin flip game. Specifics:...

    A simple game of chance Using a while loop write a simple coin flip game. Specifics: The user starts with a bank of $10.00 It costs a dollar to play A correct guess pays $2.00 The while loop is used for the game loop. It should ask if the user wants to play. The while loop will check for a value of 'Y' or 'N'. You should also allow for lower case letters to be input. You MUST prime the...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • This question deals with extending already existing Java code via a while loop. Ask the user...

    This question deals with extending already existing Java code via a while loop. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop). Check for each year input whether that year is a leap year or...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • use  JOptionPane to display output Can someone please help write a program in Java using loops, not...

    use  JOptionPane to display output Can someone please help write a program in Java using loops, not HashMap Test 1 Grades After the class complete the first exam, I saved all of the grades in a text file called test1.txt. Your job is to do some analysis on the grades for me. Input: There will not be any input for this program. This is called a batch program because there will be no user interaction other than to run the program....

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT