我正在编写一个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还是比较陌生的,所以任何建议都将不胜感激。
发布于 2013-08-14 22:47:55
使用equals比较字符串,如-
if(Choice.equals(firstChoice[0])){...}发布于 2013-08-14 22:51:55
首先,您希望将输入框的结果存储在某个位置:
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-14 22:51:36
您没有将选项绑定到选定的值,它总是将其与"“
https://stackoverflow.com/questions/18234901
复制相似问题