[英]Try Again Do-While Loop
這是一個非常簡單的程序。 計算機和用戶在 0-3 之間選擇一個數字。 如果用戶沒有猜出與計算機相同的數字,我希望程序循環返回。
String input; // input is a string variable
int cpucrazy;
int convertstring;
// 第一步 //
input = JOptionPane.showInputDialog("Guess a number between 0-3");
convertstring = Integer.parseInt(input);
// 隨機部分 //
Random ran = new Random ();
cpucrazy = ran.nextInt(3);
// 計算!
if (cpucrazy < convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too high. Guess again?"); }
else if (cpucrazy > convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too low. Guess again?"); }
else JOptionPane.showMessageDialog(null, "Correct!!");
if (cpucrazy < convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too high. Guess again?"); }
else if (cpucrazy > convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too low. Guess again?"); }
else JOptionPane.showMessageDialog(null, "Correct!!");
您需要在上面的代碼周圍放置一個 while 循環(或某種類型的循環構造)並在以下情況下退出構造: cpucrazy == convertstring
或在cpucrazy != convertstring
停留在循環構造中
循環可能類似於以下偽代碼:
b = random();
do
{
input a;
if( a != b )
{
if(a < b )
{
print "Too low";
}
else
{
print "Too high";
}
}
} while( a != b );
print "You got it correct"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.