2014-04-08 66 views
0

问:数据类型选择

  1. 我的整数通话功能工作正常。但是,双重功能无法正常工作。需要帮忙。如果我输入数据选择3,它应该捕获double值并输出“您输入的数据类型正确,否则”您输入的数据类型不正确\ n请再试一次。“

  2. 需要为String值做同样的事情。如果我选择的数据类型1,则用户需要输入字符串值。除此之外,该程序必须输出“您输入不正确的数据类型\ n请再试一次”,如果下面的尝试字符串比输出正确的“数据类型您输入的是正确的“

主程序

import javax.swing.*; 

public class Q1DataType { 

    public static int a, b, c, i; 
    public static double d, e; 

    public static void main(String[] args) { 
     for (c = 1; c > 0; c++) { 
      String a = JOptionPane 
        .showInputDialog("Data Type Validation Program\n\n\n 1). String\n 2). Integer\n 3). Double\n 4). Quit the program"); 
      if ((b = Integer.parseInt(a)) == 2) { 
       callInteger mon = new callInteger(); 
      } 

      if ((d = Double.parseDouble(a)) == 3) { 
       callDouble mon1 = new callDouble(); 
      } 

      if ((i = Integer.parseInt(a)) == 4) { 
       System.exit(0); 
      } 
     } 
    } 
} 

整数类

import javax.swing.*; 

public class callInteger { 

    public static int u; 

    public callInteger() { 
     try { 
      u = Integer.parseInt(JOptionPane 
        .showInputDialog("Please input Integer Data Type")); 
      JOptionPane.showMessageDialog(null, 
        "The Data Type You Entered is Correct"); 
     } catch (Exception e) { 
      JOptionPane.showMessageDialog(null, 
        "You Entered invalid Data Type\n Please Try Again"); 
     } 
    } 
} 

双级

import javax.swing.*; 

public class callDouble { 

    public static double t; 

    public callDouble() { 
     try { 
      t = Float.parseFloat(JOptionPane 
        .showInputDialog("Please Enter Double Data Type")); 
      JOptionPane.showMessageDialog(null, 
        "The Data Type You Entered is Correct"); 
     } catch (Exception r) { 
      JOptionPane.showMessageDialog(null, 
        "You Entered incorrect Data Type\n Please try again"); 
     } 
    } 
} 
+1

为什么使用Float.parseFloat而不是Double.parseDouble? – Ted

+0

我尝试了双倍,但不工作...任何想法请。相同的字符串不知道如何声明和输入。 – user3504969

+0

任何人都可以尝试和帮助。 – user3504969

回答

0

它仍然是相同的功能,但在这里。它的工作原理就像你的代码工作:

import javax.swing.JOptionPane; 

public class Q1DataType { 

    public static void main(String[] args) { 
     while (true) { 
      String a = JOptionPane 
        .showInputDialog("Data Type Validation Program\n\n\n 1). String\n 2). Integer\n 3). Double\n 4). Quit the program"); 
      if (a.equals("1")) { 
       // String 
      } 
      if (Integer.parseInt(a) == 2) { 
       new callInteger(); 
      } 
      if (Double.parseDouble(a) == 3) { 
       new callDouble(); 
      } 
      if (Integer.parseInt(a) == 4) { 
       System.exit(0); 
      } 
     } 
    } 
} 

编辑:嗯,我想我现在想要做什么了解。问题是,它会始终将它自动加载到一个double值。你可以尝试这样的事情。

import javax.swing.JOptionPane; 

public class callDouble { 

    public static double t; 

    public callDouble() { 
     try { 
      String s = JOptionPane.showInputDialog("Please Enter Double Data Type"); 
      if (s.contains(".")) { 
       t = Double.parseDouble(s); 
       JOptionPane.showMessageDialog(null, "The Data Type You Entered is Correct"); 
      } else { 
       JOptionPane.showMessageDialog(null, "You Entered incorrect Data Type\n Please try again"); 
      } 
     } catch (Exception r) { 
      JOptionPane.showMessageDialog(null, "You Entered incorrect Data Type\n Please try again"); 
     } 
    } 

} 
+0

HI Klemens ...是的。但是如果我输入整数值,double值不会给出任何错误消息。 – user3504969

+0

我编辑了我的帖子,请尝试。 –

+0

哦,是的。得到它了。非常感谢。 – user3504969