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

}

エラーは発生しませんが、配列から文字列の 1 つを選択しても何もしません。私が使用した 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

Choice を選択した値にバインドしていません。常に "" と比較します

于 2013-08-14T14:51:36.240 に答える