【问题标题】:Turn Scanner into JOptionPane将 Scanner 变成 JOptionPane
【发布时间】:2017-01-07 08:14:44
【问题描述】:

我要从用户那里得到一个整数半径,然后显示圆的面积和周长。如果用户输入负整数或整数以外的其他值(a 或 5.6),它会要求用户重新输入。

下面是捕获错误输入的代码,但它使用的是 Scanner,我需要使用 JOptionPane。

import java.util.Scanner;

public class GetCircle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
    int radius = 0;
    System.out.print("Please enter size of radius (Must be integer): ");
    while (true){
        if(input.hasNextInt()){ 
        radius = input.nextInt();
        if (radius < 0){
            System.out.println("That is not a valid number, please try again :");

        }   else {
        System.out.println("The radius is: " + radius);
        }
        } else {
            System.out.println("That is not a valid number, please try again :");

            input.next();

        }
    }   
}
}

我不知道如何将 hasNextInt 与 JOptionPane 一起使用....有人可以帮忙吗?

这是我现在拥有的 JOptionPane:

import javax.swing.JOptionPane;

public class Radius
{
public static void main(String[] args)
{

   String radius = JOptionPane.showInputDialog(null,"Enter 
Radius","Radius",JOptionPane.QUESTION_MESSAGE);

   Integer.parseInt(radius);

   JOptionPane.showMessageDialog(null,"the string you entered is: " + radius, "results",JOptionPane.PLAIN_MESSAGE);

    }  
}

【问题讨论】:

  • 您的问题有点不清楚,JPanel 和 hasNextInt 之间的关系是什么,以及,您到底想做什么?
  • 当我使用 JPanel 时,我不知道如何像使用扫描仪和 hasext.Input 那样只接受正整数
  • 你的意思是 JOptionPane 而不是 JPanel 对吧?
  • 是的,谢谢,我对问题进行了更改......我很抱歉。如您所知,我是初学者
  • 如果你明白你需要这样的东西? alvinalexander.com/sites/default/files/users/user3/…

标签: java java.util.scanner joptionpane


【解决方案1】:

第一个解决方案是Scanner 你想要它的图形用户界面,所以想用JPanel?关键是Scanner的next()或者hasNextInt()是空白标识,所以可以重复输入或测试,但是在JPanel中像LAIDANI优素秀http://alvinalexander.com/sites/default/files/users/user3/joptionpane-show-input-dialog-example-1.png 只能输入一次。

【讨论】:

    【解决方案2】:

    我认为你需要这样的东西:

    您需要检查您的输入,如果输入是整数则显示成功消息,否则显示错误消息:

    我认为你的代码应该是这样的:

    public static void main(String[] args) {
        while (true) {
            String radius = JOptionPane.showInputDialog(null, "Enter Radius", "Radius", JOptionPane.QUESTION_MESSAGE);
            if (radius.equals("#")) {
                break;
            } else {
                if (test(radius)) {
                    JOptionPane.showMessageDialog(null, "the string you entered is: " + radius, "results", JOptionPane.PLAIN_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "the string you entered is: " + radius, "results", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    
    }
    
    public static boolean test(String s) {
        try {
            Integer i = Integer.parseInt(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    

    注意

    如果你想退出,你需要检查一个特定的字符串,例如#

    编辑

    public static void main(String[] args) {
        while (true) {
            String radius = JOptionPane.showInputDialog(null, "Enter Radius", "Radius", JOptionPane.QUESTION_MESSAGE);
            System.out.println(radius.length());
            if (radius.equals("#")) {
                break;
            } else {
                if (test(radius, 10)) {
                    if(Integer.parseInt(radius)<0){
                        JOptionPane.showMessageDialog(null, "Negative int: " + radius, "results", JOptionPane.ERROR_MESSAGE);
                    }else{
                        JOptionPane.showMessageDialog(null, "the string you entered is: " + radius, "results", JOptionPane.PLAIN_MESSAGE);
                    }
    
                } else {
                    JOptionPane.showMessageDialog(null, "the string you entered is: " + radius, "results", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    
    }
    
    public static boolean test(String s, int radix) {
        Scanner sc = new Scanner(s.trim());
        if (!sc.hasNextInt(radix)) {
            return false;
        }
        sc.nextInt(radix);
        return !sc.hasNext();
    }
    

    您可以在此处了解更多信息:Determine if a String is an Integer in Java

    希望对你有帮助。

    【讨论】:

    • 谢谢您,先生。不幸的是,我是一个初学者,我们还没有学会 try and catch,我认为我不应该使用它。我希望有一种类似的方式,我可以用 Scanner 来做,我可以用 JOptionPane 来做......
    • @Brow 我编辑我的答案你现在可以检查我是否使用扫描仪测试输入,希望这可以帮助你。
    • @Brow 我已经检查了负整数,你现在可以检查了,祝你好运
    【解决方案3】:

    如果字符串不是可解析的整数,Integer#parseInt(String s) 将引发未经检查的异常 NumberFormatException。这就是你需要的。

    try{
        int radiusInt = Integer.parseInt(radius);
        // everything's OK, it's an integer 
    } catch(NumberFormatException e){
        // it is not an integer 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2016-01-19
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多