0

我正在编写一个类似于物理课的 java 程序:

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.");
            }
            }

我想要你在第一选择数组中选择的任何选择,然后去受人尊敬的班级。所以如果我选择运动学,它将调用运动学类,前几行是:

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 还是比较陌生,所以任何提示都将不胜感激。

4

4 回答 4

4

使用equals比较字符串,例如 -

if(Choice.equals(firstChoice[0])){...}
于 2013-08-14T14:47:54.673 回答
1

首先,您要将输入框的结果存储在某处:

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用来做你的字符串比较,而不是==

if (choice.equals(firstChoice[0])){
     Kinematics.IntroToKinematics();
}

循环不应该是必要的。

于 2013-08-14T14:51:55.050 回答
0

首先在 showInputDialog 你应该做这样的事情

String Choice = ""; 
     Choice=  (String) 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]);

首先声明变量Choice,然后将inputDialog值分配给choice,然后你的while应该是这样的

    int i = 0;
    while (i<=0){
    //do something
    }

因为它第一次运行i<0,因为 0 不低于 0(因为你正在声明int i =0;)不会运行。你可以继续使用"",但这样的东西应该更有效

if (Choice.equals(firstChoice[0])){
             Kinematics.IntroToKinematics();
             i++;

            }

最后尝试在您的变量中首先使用小写字母,例如:tableSize

于 2013-08-14T15:08:26.713 回答
0

您没有将 Choice 绑定到选定的值,它总是将其与“”进行比较

于 2013-08-14T14:51:36.240 回答