阅读了我书中我认为是相关的所有内容之后,我没有写任何帮助。我在Java中是一个完全新手,我必须解决以下问题: 我必须根据网球制作一个DialogProgram
。该程序必须询问用户“谁得分”,并且用户必须输入“A”或“B”,并且必须执行直到A或B赢得比赛。 在我有的代码中把我的问题作为笔记。我希望他们会这样做最有意义。While&for循环和JOptionPane数据输入连接到for循环
import java.awt.Color;
import acm.program.*;
import acm.graphics.*;
import javax.swing.JOptionPane;
public class A3 extends DialogProgram
{
public void run()
{
JOptionPane.showMessageDialog(null, "Playing: Who got the point?");
int A = 0;
int B = 0;
int C = 0;
/*
* How do I make a loop to execute the JOptionPane until the if or else statement
* in the for loop is achieved?
* I think it should be with a while loop but how do I tell it to ask for input
* until the for loop is achieved?
*/
while()
{
/*
* I need the JOptionPane to take in only A or B as an answer each time it pops out.
* How do I tell the JOptionPane to do that?
* How to connect the input of A or B in the JOptionPane to the for loop so that it
* counts the times A is inputed and the times B is inputed
*/
String input = JOptionPane.showInputDialog("Enter A or B");
for (C = 0; A >= 4 && B <= A-2; B++)
{
if (A >= 4 && B <= A-2)
{
// In this case A wins
JOptionPane.showMessageDialog(null,"Player A Wins!");
}
else if (B >= 4 && A <= B-2)
{
// In this case B wins
JOptionPane.showMessageDialog(null,"Player B wins!");
}
}
}
}
}