在if语句中使用Joptionpane字符串输入

时间:2018-02-21 09:37:43

标签: java string swing if-statement joptionpane

我开始学习java,我的一个程序出了问题。 我试图根据用户输入的天线类型获得天线的电感。 问题是我有对话框并输入输入,但之后我的代码停止了,它没有进行循环。我一直在网上看,我无法找到为什么if语句不能接受我的输入。 这是我的代码的一部分:

import javax.swing.*;
//import javax.swing.JOptionPane;

public class Antenna_Inductance {

    public static void main(String[] args) {
        // first we will define all the variable use of the problem set

        // for the lime antenna
       double M;
       int N1;
       int N2;
       double R1;
       double R2;
      // now we can start to programmed the main code 

       String Type= JOptionPane.showInputDialog("Type of Antenna");
        // this is where the code stop and i don't know why

    if (Type == "line") {
           String input_rmin= JOptionPane.showInputDialog("Enter minimum distance for r");
            double rmin = Double.parseDouble(input_rmin);
            String input_rmax= JOptionPane.showInputDialog("Enter maximum distance for r");
            double rmax = Double.parseDouble(input_rmax);
           I = 0.01;
           for (r=rmin; r==rmax ;r+=0.05) {

           B_line = (mu*I)/(2*pi*r);

        System.out.println("Inductance for line antenna =" + B_line);
           }

所以,我真的可以在这个上使用一些帮助。谢谢你,祝你有愉快的一天

1 个答案:

答案 0 :(得分:0)

如果我的问题是正确的,那么这应该是你要找的:

    import javax.swing.*;
    //import javax.swing.JOptionPane;

    public class Antenna_Inductance {

    public static void main(String[] args) {
        // first we will define all the variable use of the problem set

        // for the lime antenna
        double M;
        int N1;
        int N2;
        double R1;
        double R2;
        // now we can start to programmed the main code

        String type = (String) JOptionPane.showInputDialog(new JFrame(),
                "Enter Antenna or what ever:", "titleForJInputDialog" , JOptionPane.DEFAULT_OPTION);
        // this is where the code stop and i don't know why
        System.out.println("Input of JOptionPane: " + type);
        if (type.equals("line")) {
            System.out.println("Input of JOptionPane is the same like 'line'");

        }
    }
}