首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用开关和Try语句验证用户输入

问使用开关和Try语句验证用户输入
EN

Stack Overflow用户
提问于 2011-03-28 19:12:18
回答 2查看 2.7K关注 0票数 0

我已经做了一段时间了,它不会编译。这让我说错了

变量选择可能没有初始化开关(选择)^

但我在程序中设置了变量。所以我不知道问题出在哪里?真的是别的什么东西阻止了它的编译吗?

代码语言:javascript
复制
 import java.io.*;
    import javax.swing.JOptionPane;

    public class MyType
    {
        public static void main(String[] args)
        {
            String strChoice = "", strTryString, strTryInt, strTryDouble;
            Integer choice, tryInt;
            double tryDouble;
            boolean done = false;

            while(!done)
        {
                try
                {
                    String answer = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program");
                    choice = Integer.parseInt(strChoice);

                    //test for valid codes 1, 2, 3, or 4
                    if (choice<1 || choice>4) throw new NumberFormatException();
                    else done = true;
                }
                    catch(NumberFormatException e)
                {
                    JOptionPane.showInputDialog(null, "Please enter a 1, 2, 3, or 4", "Error", JOptionPane.INFORMATION_MESSAGE);
                    switch(choice)
                {
                        case 1:
                            JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
                            break;

                        case 2:
                            JOptionPane.showMessageDialog(null, "Correct!");
                            tryInt = Integer.parseInt(strChoice);
                            break;

                        case 3:
                            JOptionPane.showMessageDialog(null, "Correct!");
                            tryDouble = Integer.parseInt(strChoice);
                            break;

                        case 4:
                            done = true;
                            JOptionPane.showMessageDialog(null, "Exit.");
                            System.exit(0);
                            break;
                        default:  throw new NumberFormatException();
            }
            }

            }
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-28 19:14:15

如果showInputDialog抛出,则不会设置“选择”。

代码语言:javascript
复制
try
            {
                String answer = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program");
                choice = Integer.parseInt(strChoice);

                //test for valid codes 1, 2, 3, or 4
                if (choice<1 || choice>4) throw new NumberFormatException();
                else done = true;
            }
                catch(NumberFormatException e)
            {
票数 0
EN

Stack Overflow用户

发布于 2011-03-28 19:17:59

您应该在try块之前将选择变量具体化,因为在catch中引用它,在某些情况下,如果在初始化选择发生之前抛出异常,则变量选择将被未初始化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5463860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档