【问题标题】:Use a Joptionpane string input inside a if statement在 if 语句中使用 Joptionpane 字符串输入
【发布时间】:2018-02-21 09:37:43
【问题描述】:

我开始学习 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);
           }

所以,我真的可以在这方面使用一些帮助。谢谢你,祝你有美好的一天

【问题讨论】:

  • JOptionPane 用于 JFrame 窗口。字符串比较:if (Type.equals("line")) {。查看演示。

标签: java string swing if-statement joptionpane


【解决方案1】:

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

    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'");

        }
    }
}

【讨论】:

  • 谢谢。很有帮助
猜你喜欢
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 2018-08-09
相关资源
最近更新 更多