仅接受整数的代码

时间:2015-07-05 15:22:37

标签: java jcreator

import javax.swing.*;
import java.awt.*;

public class JCD {

    public static void main(String[] args) {

        String input, inputs;
        int input1, input2;

        input = JOptionPane.showInputDialog("Enter first number");
        inputs = JOptionPane.showInputDialog("Enter second number");

        input1 = Integer.parseInt(input);
        input2 = Integer.parseInt(inputs);

        JOptionPane.showMessageDialog(null, "The GCD of two numbers  " + input
                + "and" + inputs + " is: " + findGCD(input1, input2));

    }// close void

    private static int findGCD(int number1, int number2) {
        // base case
        if (number2 == 0) {
            return number1;

        }// end if

        return findGCD(number2, number1 % number2);
    }// end static

} // close class

我可以添加什么以便它只接受整数?如果没有给出一个整数,那么它会再回来询问.....

3 个答案:

答案 0 :(得分:0)

将输入请求放在while语句中,检查它是否为int,如果不重复循环,否则退出。为你的输入做这件事。

像这样的东西

2015-07-05 17:19:37.701 Pixym[1271:72192] CUICatalog: Invalid asset name supplied: 

2015-07-05 17:19:37.702 Pixym[1271:72192] Could not load the "" image referenced from a nib in the bundle with identifier "HP.Pixym" 

2015-07-05 17:19:37.705 Pixym[1271:72192] Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. 

(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(NSLayoutConstraint:0x7fa83b822a40 H:[UIImageView:0x7fa83b8529a0(300)],
 NSLayoutConstraint:0x7fa83b85ccb0 H:[UIImageView:0x7fa83b8529a0]-(10)-|   (Names: '|':UITableViewCellContentView:0x7fa83b871ff0 ),
 NSLayoutConstraint:0x7fa83b8643d0 H:|-(10)-[UIImageView:0x7fa83b8529a0]   (Names: '|':UITableViewCellContentView:0x7fa83b871ff0 ),
 NSLayoutConstraint:0x7fa83b80ab00 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7fa83b871ff0(375)])

Will attempt to recover by breaking constraint 
NSLayoutConstraint:0x7fa83b822a40 H:[UIImageView:0x7fa83b8529a0(300)]

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in UIKit/UIView.h may also be helpful.

请注意,此解决方案要求您在声明变量时初始化变量

public static void main(String[] args) {

    String input=null, inputs=null;
    int input1 = 0, input2=0;

    boolean err=true;
    do{
        try{
            input = JOptionPane.showInputDialog("Enter first number");
            input1 = Integer.parseInt(input);
            err=false;
        }catch(NumberFormatException e){
            e.printStackTrace();
        }
    }while(err);

    err=true;
    do{
        try{
            inputs = JOptionPane.showInputDialog("Enter second number");
            input2 = Integer.parseInt(inputs);
            err=false;
        }catch(NumberFormatException e){
            e.printStackTrace();
        }
    }while(err);

    JOptionPane.showMessageDialog(null, "The GCD of two numbers  " + input
            + "and" + inputs + " is: " + findGCD(input1, input2));

}

答案 1 :(得分:0)

你应该尝试“尝试并捕捉”。              input = JOptionPane.showInputDialog(“输入第一个数字”);
             inputs = JOptionPane.showInputDialog(“输入第二个数字”);

           try {

                     input1=Integer.parseInt(input);
                     input2=Integer.parseInt(inputs);
                    // establish and use the variables if the characters inserted are numbers
               }
         catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, e+ "is not a number");
                //display a warning to le the user know
               }

答案 2 :(得分:0)

您可以使用以下内容: -

boolean inputAccepted = false;
while(!inputAccepted) {
  try {
     input1=Integer.parseInt(input);
     input2=Integer.parseInt(inputs);
     inputAccepted = true;

  } catch(NumberFormatException e) {
    JOptionPane.showMessageDialog("Please input a number only");
  }

  ... do stuff with good input value
}