I want to create a lab that can keep track of bowling scores. The input value has to be between 0-10. If the number differs, I want to display "Invalid". However, in my code, if I make consecutive errors, my program won't detect numbers out of the range as "Invalid". How can I code so that the program will make me try again until I put in a possible value?
import javax.swing.JOptionPane;
import java.io.*;
public class Driver14
{
public static void main(String[] args)
{
int totalScore, frame, ball;
totalScore = 0;
frame = 1;
ball = 1;
JOptionPane.showMessageDialog(null,"Welcome to Computer Science Bowling!");
while(frame<11){
int score1 = Integer.parseInt(
JOptionPane.showInputDialog("Score " + totalScore + "\n" + "Frame " + frame + ", Ball " + ball));
if(0<=score1 && score1<11){
totalScore = totalScore + score1;
ball++;
}
else{
score1 = Integer.parseInt(
JOptionPane.showInputDialog("Invalid!\n" + "Score " + totalScore + "\n" + "Frame " + frame + ", Ball " + ball));
totalScore = totalScore + score1;
ball++;
}
int score2 = Integer.parseInt(
JOptionPane.showInputDialog("Score " + totalScore + "\n" + "Frame " + frame + ", Ball " + ball));
if(0<=score2 && score2<11){
totalScore = totalScore + score2;
ball++;
}
else{
score2 = Integer.parseInt(
JOptionPane.showInputDialog("Invalid!\n" + "Score " + totalScore + "\n" + "Frame " + frame + ", Ball " + ball));
totalScore = totalScore + score2;
ball++;
}
frame++;
ball = 1;
}
JOptionPane.showMessageDialog(null,"Finished bowling!\nScore "+ totalScore);
}
}
Aucun commentaire:
Enregistrer un commentaire