dimanche 4 mars 2018

Java: How to continuously prompt for input using sentinel and display message after each input using JOptionPane

I am trying to create a program that continuously requests the entry of a technique name that an athlete will perform until the user has determined they are finished entering techniques. As each technique is entered, I need to print a message to the user indicating the name of the technique entered and the number of points possible by performing the technique. This process should repeat until the user has indicated they are finished entering input. At the end of the program it should calculate the total number of techniques entered and the total number of points across all techniques.

My program compiles but when I run it, it is stuck on the message displaying the initial technique entry and points, but it does not re-prompt for a new technique. Any guidance would be greatly appreciated. Here is my code below:

import javax.swing.JOptionPane;
public class OlympicsProgram {
   public static void main(String[] args) {

 double technique; 
 double numPoints = 0;
 int techniqueCount = 0;

 // Prompt user to enter technique name, input "Q" when finished
 String techniqueEntry = JOptionPane.showInputDialog ("Enter technique name or 'Q' to quit");

    if (techniqueEntry.equalsIgnoreCase("Q")) {
        JOptionPane.showMessageDialog(null, "Input has ended.");
         System.exit(0);
    } else {
        while (!techniqueEntry.equalsIgnoreCase("Q")) {
            try {
                technique = Double.parseDouble(techniqueEntry);
            }
            // Catch any invalid values
            catch (NumberFormatException e) {
                technique = -1;
            }

if (techniqueEntry.equalsIgnoreCase ("Gorilla Grab")) {
     numPoints = 0.9;
     techniqueCount++;

 } else if (techniqueEntry.equalsIgnoreCase ("720 Degree Spin")) {
   numPoints = 1.2;
   techniqueCount++;

 } else if (techniqueEntry.equalsIgnoreCase ("Chicane Flip and Roll")) {
     numPoints = 1.3;
     techniqueCount++;

 } else if (techniqueEntry.equalsIgnoreCase ("Andrecht Hand Plant")) {
     numPoints = 0.6;
     techniqueCount++; 
  }
  // Display name of technique entered and points possible after each input
  JOptionPane.showMessageDialog(null, String.format("The technique name is: " + techniqueEntry + "The number of points is: " + numPoints));
  }
 // Continously prompt user for input 
 techniqueEntry = JOptionPane.showInputDialog("Enter another tecnique name:");
 }
 //Calculate the total number of techniques and total number of points across all techniques     
           if (numPoints > 0) {

           double totalPoints = numPoints++;
           int techniqueTotal = techniqueCount++;

           JOptionPane.showMessageDialog(null, String.format ("The number of techniques performed is" + techniqueTotal + "The total number of points is " + totalPoints));
           }
           // Error message
           else {
              JOptionPane.showMessageDialog(null, "You entered an invalid value. Please try again.");
             System.exit(0);
                }
       }
  }

Aucun commentaire:

Enregistrer un commentaire