所以我正在为我的科学课做一个项目,但问题一直没有解决。我想做的是,当我单击第一个问题的答案时,我希望它转到第二个问题,但转到最后一个问题。我对Java非常陌生(我从上周开始),但是我确实有其他语言的基本经验。我将不胜感激,谢谢。
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Window {
JFrame window;
Container con;
JPanel titlenamePanel, startButtonPanel, mainTextPanel, ChoiceButtonPanel;
JLabel titlenameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 28);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 18);
JButton startButton, choice1, choice2, choice3, choice4, choice5, choice6;
JTextArea mainTextArea;
String Progress, yourChoice, Wind, Rain, Location, Cloud;
int Temp, Hum;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler chandler = new ChoiceHandler();
public static void main(String[] args) {
new Window();
}
public Window(){
window = new JFrame();
//width x height
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titlenamePanel = new JPanel();
titlenamePanel.setBounds(100, 100, 600, 150);
titlenamePanel.setBackground(Color.black);
titlenameLabel = new JLabel("Weather Genie");
titlenameLabel.setForeground(Color.WHITE);
titlenameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("Start");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
titlenamePanel.add(titlenameLabel);
startButtonPanel.add(startButton);
con.add(titlenamePanel);
con.add(startButtonPanel);
}
public void createWindowScreen() {
titlenamePanel.setVisible(false);
startButtonPanel.setVisible(false);
// Text Panel
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
// Add Texts
mainTextArea = new JTextArea("Please answer the following questions.");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
// Button Panel
ChoiceButtonPanel = new JPanel();
ChoiceButtonPanel.setBounds(250, 350, 300, 150);
ChoiceButtonPanel.setBackground(Color.black);
ChoiceButtonPanel.setLayout(new GridLayout(3,2));
con.add(ChoiceButtonPanel);
// Buttons
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.DARK_GRAY);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.addActionListener(chandler);
ChoiceButtonPanel.add(choice1);
choice1.setActionCommand("c1");
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.DARK_GRAY);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.addActionListener(chandler);
ChoiceButtonPanel.add(choice2);
choice2.setActionCommand("c2");
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.DARK_GRAY);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.addActionListener(chandler);
ChoiceButtonPanel.add(choice3);
choice3.setActionCommand("c3");
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.DARK_GRAY);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.addActionListener(chandler);
ChoiceButtonPanel.add(choice4);
choice4.setActionCommand("c4");
choice5 = new JButton("Choice 5");
choice5.setBackground(Color.DARK_GRAY);
choice5.setForeground(Color.white);
choice5.setFont(normalFont);
choice5.addActionListener(chandler);
ChoiceButtonPanel.add(choice5);
choice5.setActionCommand("c5");
choice6 = new JButton("Choice 6");
choice6.setBackground(Color.DARK_GRAY);
choice6.setForeground(Color.white);
choice6.setFont(normalFont);
choice6.addActionListener(chandler);
ChoiceButtonPanel.add(choice6);
choice6.setActionCommand("c6");
varSetup();
}
public void varSetup() {
Temp = 80;
Hum = 65;
Location = "South";
Rain = "No";
Wind = "No";
Cloud = "Clear";
CloudQuestion();
}
public void CloudQuestion(){
Progress = "CloudQ";
mainTextArea.setText("What do the clouds look like?");
choice1.setText("High and Dark");
choice2.setText("High and Bright");
choice3.setText("Low and Dark");
choice4.setText("Low and Bright");
choice5.setText("Clear");
choice6.setText("Foggy");
}
public void TempQuestion(){
Progress = "TempQ";
mainTextArea.setText("What tmperature is it outside?");
choice1.setText("90-110");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("0-10");
}
public void HumQuestion() {
Progress = "HumQ";
mainTextArea.setText("How Humid is it outside? \nEx. The Average Household humidity is 65");
choice1.setText("90-100");
choice2.setText("70-89");
choice3.setText("50-69");
choice4.setText("30-49");
choice5.setText("10-29");
choice6.setText("");
}
public void WindQuestion(){
Progress = "WindQ";
mainTextArea.setText("Has it been windy outside today?");
choice1.setText("Yes");
choice2.setText("No");
choice3.setText("");
choice4.setText("");
choice5.setText("");
choice6.setText("");
}
public void RainQuestion() {
Progress = "RainQ";
mainTextArea.setText("Has it Rained this week?");
choice1.setText("1-2 Times");
choice2.setText("3-4 Times");
choice3.setText("5 or more Times");
choice4.setText("None");
choice5.setText("");
choice6.setText("");
}
public void LocQuestion() {
Progress = "LocQ";
mainTextArea.setText("What part of the US are you in? \nExample, florida would be East \nSomebody in Nebraska or Kansas would say Central. ");
choice1.setText("North");
choice2.setText("South");
choice3.setText("East");
choice4.setText("West");
choice5.setText("Central");
choice6.setText("");
}
public class ChoiceHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
String yourChoice = event.getActionCommand();
switch (Progress) {
case "CloudQ":
switch(yourChoice) {
case"c1":
Cloud = "HD";
TempQuestion();
break;
case"c2":
Cloud = "HB";
TempQuestion();
break;
case"c3":
Cloud = "LD";
TempQuestion();
break;
case"c4":
Cloud = "LB";
TempQuestion();
break;
case"c5":
Cloud = "Clear";
TempQuestion();
break;
case"c6":
Cloud = "Foggy";
TempQuestion();
break;
}
case "TempQ":
switch(yourChoice) {
case"c1":
Temp = 90110;
HumQuestion();
break;
case"c2":
Temp = 7089;
HumQuestion();
break;
case"c3":
Temp = 5069;
HumQuestion();
break;
case"c4":
Temp = 3049;
HumQuestion();
break;
case"c5":
Temp = 1029;
HumQuestion();
break;
case"c6":
Temp = 010;
HumQuestion();
break;
}
case "HumQ":
switch(yourChoice) {
case"c1":
Hum = 90100; WindQuestion();
break;
case"c2":
Hum = 7089; WindQuestion();
break;
case"c3":
Hum = 5069; WindQuestion();
break;
case"c4":
Hum = 3049; WindQuestion();
break;
case"c5":
Hum = 1029;
WindQuestion();
break;
case"c6":
HumQuestion();
break;
}
case "WinQ":
switch(yourChoice) {
case"c1":
Wind = "Yes";
RainQuestion();
break;
case"c2":
Wind = "No";
RainQuestion();
break;
case"c3":
WindQuestion();
break;
case"c4":
WindQuestion();
break;
case"c5":
WindQuestion();
break;
case"c6":
WindQuestion();
break;
}
case "RainQ":
switch(yourChoice) {
case"c1":
Rain = "12";
LocQuestion();
break;
case"c2":
Rain = "34";
LocQuestion();
break;
case"c3":
Rain = "5+";
LocQuestion();
break;
case"c4":
Rain = "None";
LocQuestion();
break;
case"c5":
RainQuestion();
break;
case"c6":
RainQuestion();
break;
}
case "LocQ":
switch(yourChoice) {
case"c1":
Location = "North"; results();break;
case"c2":
Location = "South"; results();break;
case"c3":
Location = "East"; results();break;
case"c4":
Location = "West"; results();break;
case"c5":
Location = "Central"; results();break;
case"c6":
LocQuestion();
break;
}
break;
}
}
}
public void results() {
}
public class TitleScreenHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
createWindowScreen();
}
}
}
答案 0 :(得分:0)
您在另一个switch语句中有一个switch语句,在内部switch语句中您正确使用了break
,但是您永远不会在使用内部switch语句后中断,请参见下面的代码(仅一部分您的代码)以查看丢失的内容。我已经用break
标记了// Add this
语句。
不使用break
会导致它执行与其匹配的case
下的每个case
。它将一直执行到到达第一个break
或switch
为止。
switch (Progress) {
case "CloudQ":
switch (yourChoice) {
case "c1":
Cloud = "HD";
TempQuestion();
break;
case "c2":
Cloud = "HB";
TempQuestion();
break;
case "c3":
Cloud = "LD";
TempQuestion();
break;
case "c4":
Cloud = "LB";
TempQuestion();
break;
case "c5":
Cloud = "Clear";
TempQuestion();
break;
case "c6":
Cloud = "Foggy";
TempQuestion();
break;
}
break; // Add this
case "TempQ":
switch (yourChoice) {
case "c1":
Temp = 90110;
HumQuestion();
break;
case "c2":
Temp = 7089;
HumQuestion();
break;
case "c3":
Temp = 5069;
HumQuestion();
break;
case "c4":
Temp = 3049;
HumQuestion();
break;
case "c5":
Temp = 1029;
HumQuestion();
break;
case "c6":
Temp = 010;
HumQuestion();
break;
}
break; // Add this
}