Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement 保龄球实验室:若非如此_If Statement_Joptionpane - Fatal编程技术网

If statement 保龄球实验室:若非如此

If statement 保龄球实验室:若非如此,if-statement,joptionpane,If Statement,Joptionpane,我想建立一个实验室来记录保龄球的得分。输入值必须介于0-10之间。如果数字不同,我想显示“无效”。然而,在我的代码中,如果我连续出错,我的程序不会将超出范围的数字检测为“无效”。如何编写代码,使程序使我在输入可能的值之前重试 import javax.swing.JOptionPane; import java.io.*; public class Driver14 { public static void main(String[] args) { int totalS

我想建立一个实验室来记录保龄球的得分。输入值必须介于0-10之间。如果数字不同,我想显示“无效”。然而,在我的代码中,如果我连续出错,我的程序不会将超出范围的数字检测为“无效”。如何编写代码,使程序使我在输入可能的值之前重试

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);
   }
}
import javax.swing.JOptionPane;
导入java.io.*;
公共类驱动程序14
{
公共静态void main(字符串[]args)
{
整型总分、框架、球;
总分=0;
帧=1;
球=1;
showMessageDialog(空,“欢迎来到计算机科学保龄球!”);

而(frame通常,您可以使用以下模式:

public static String askUserForInpute(String message){
    String input = null;
    try {
        input = JOptionPane.showInputDialog(message);
        if(/*anyLogic*/) // OR any method that throws exception for instance Integer.parseInt(input)
            throw new Exception("YOUR MESSAGE");
    } catch (Exception e) {
        //ANY INFO YOU WANT
        output = askUserForInpute(message);
    }
    return input;
}
您向用户请求输入,如果输入不正确,则显式或隐式(通过方法)抛出异常,然后捕获它并再次调用同一方法。这是一种可能性