首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用if else语句调用不同的类

使用if else语句调用不同的类
EN

Stack Overflow用户
提问于 2013-08-14 22:45:07
回答 4查看 1.5K关注 0票数 0

我正在编写一个java程序,它就像一堂物理课:

代码语言:javascript
复制
import javax.swing.JOptionPane;
public class PhysicsClass {


    public static void main(String[] args) {
        int g = -1;
        while (g<0){
            String input = JOptionPane.showInputDialog("Welcome! What's your name? ");
            if(input.length() > 0){
                g++;
             System.out.println("Great! Lets begin.");
            }
            else{
                 System.out.println(" ok then.");
                System.exit(0);
            }
        }

         String[] firstChoice = {"Kinematics", "Dynamics", "Impulse/Momentum", "Energy/Power"}; 
            JOptionPane.showInputDialog(null,
                    "Which one of these Topic would you like to start with?", 
                    "Please pick a topic to start with",
                    JOptionPane.INFORMATION_MESSAGE, null,
                    firstChoice, firstChoice[0]);


            int i = 0;
            while (i<0){
            String Choice = "";
            if (Choice == firstChoice[0]){
             Kinematics.IntroToKinematics();
             i++;

            }
            else if (Choice == firstChoice[1]){
                Dynamics.DynamicsIntro();
             i++;
            }
            else if (Choice == firstChoice[2]){
                ImpulseAndMomentum.ImpulseAndMomentumIntro();
             i++;
            }
            else if (Choice == firstChoice[3]){
                EnergyAndPower.EnergyAndPowerIntro();
             i++;
            }
            else{
                System.out.println("Please pick one.");
            }
            }

我希望您在第一个选择数组中选择任何选项,然后转到受人尊敬的类。因此,如果我选择运动学,它将调用运动学类,它的前几行是:

代码语言:javascript
复制
import javax.swing.JOptionPane;

public class Kinematics {

    public static void IntroToKinematics() {
        JOptionPane.showInternalMessageDialog(null, "Kinematics is one of the most basic"
                + " ideas of Newtonian Physics. It encompasses: speed, distance, time"
                + " displacement, acceleration and many key base concepts that form the the "
                + " foundation for most physic subjects. It will poke its head in everything"
                + "we cover.", "intro", JOptionPane.INFORMATION_MESSAGE);
    }

}

它没有给我任何错误,但是当我从数组中选择一个字符串时,它没有做任何事情。我认为这可能与我使用的if else语句有关。感谢大家的帮助,我对Java还是比较陌生的,所以任何建议都将不胜感激。

EN

回答 4

Stack Overflow用户

发布于 2013-08-14 22:47:55

使用equals比较字符串,如-

代码语言:javascript
复制
if(Choice.equals(firstChoice[0])){...}
票数 4
EN

Stack Overflow用户

发布于 2013-08-14 22:51:55

首先,您希望将输入框的结果存储在某个位置:

代码语言:javascript
复制
String choice = JOptionPane.showInputDialog(null,
                "Which one of these Topic would you like to start with?", 
                "Please pick a topic to start with",
                JOptionPane.INFORMATION_MESSAGE, null,
                firstChoice, firstChoice[0]);

然后用.equals代替==进行字符串比较

代码语言:javascript
复制
if (choice.equals(firstChoice[0])){
     Kinematics.IntroToKinematics();
}

循环应该不是必需的。

票数 1
EN

Stack Overflow用户

发布于 2013-08-14 22:51:36

您没有将选项绑定到选定的值,它总是将其与"“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18234901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档