【发布时间】:2020-12-03 06:07:49
【问题描述】:
我对 Java 和 GUI 很陌生,但我正在尝试将“猜数字游戏”应用到应用程序中。 我想在这里更改 max 和 min 变量 n = Integer.parseInt(JOptionPane.showInputDialog ("Choose a number between [%d,%d]:"), min, max); 使游戏更轻松。 显然这是行不通的,但这是最终目标。有什么建议吗?谢谢
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class guessTheNumber {
public static void main(String[] args) {
int nAttempts= 0, min = 0, max = 100;
int n = 0, nRandom = (int)( Math.random() * (( max - min ) + 1 )) + min;
do
{
nAttempts++;
n = Integer.parseInt(JOptionPane.showInputDialog ("Choose a number between [%d,%d]:"), min, max);
if ( n != nRandom)
{
if( n > nRandom )
{
JTextField.
JOptionPane.showMessageDialog(null, "Too High","Wrong Number", JOptionPane.INFORMATION_MESSAGE);
}
if( n < nRandom )
{
JOptionPane.showMessageDialog(null, "Too Low","Wrong Number", JOptionPane.INFORMATION_MESSAGE);
}
}
} while ( n != nRandom);
JOptionPane.showMessageDialog(null, "Congrats!! Attempts = "+ nAttempts,"Level finished", JOptionPane.WARNING_MESSAGE);
}
}
【问题讨论】:
标签: java swing jtextfield joptionpane