我需要就如何在Java中如何在Java中使用循環
這是類的分配使用for循環的幫助,所以我寧願只在正確的方向指出,而不是給出一個答案。
「有效七個小矮人的名單:困,害臊,DOC,打噴嚏,快樂,脾氣暴躁,隨機字符,非有效的小矮人糊塗蛋 游泳池:獅子-O,Cheetara,Panthro,TIGRA,SNARF,唐老鴨,米老鼠,米妮,高飛,希刺克厲夫,休伊,杜威,路易,史高治·麥克老鴨,
聲明這些變量:
int counter = 7;
boolean firstSelection = false;
boolean secondSelection = false;
boolean thirdSelection = false;
boolean fourthSelection = false;
boolean fiveSelection = false;
boolean sixSelection = false;
boolean sevenSelection = false;
打印的三種選擇到控制檯一個清單詢問用戶挑選。三種選擇中的正確矮人 三種選擇的列表將包括來自隨機字符列表和七個小矮人中的一個名字。 您將創建一個switch語句來處理選項的選擇範圍 如果選擇了錯誤的話,那麼遞減int變量稱爲櫃檯當選擇了正確的情況下打印到控制檯「錯誤的選擇」 那麼相應的布爾變量改爲true (即.. firstSelection,secondSelection等),並打印到控制檯「何嗨,你選擇了正確的」 默認情況下將打印到控制檯「無效的選擇」的聲明 創建一個循環,將執行此七倍直到你覆蓋了所有的七個小矮人。
- for循環
- 使用一個重新創建循環再次使用do-while循環
- 重新創建再次使用while循環 在結束循環,創建一個if-else語句。該語句將有短路& &的,這將考驗所有的布爾變量。如果屬實,則向控制檯打印聲明「您贏得了一顆金星!」。否則,向控制檯打印一份聲明「您沒有得到正確的答案」。 「
我完成了前面的任務,這正是沒有循環的規範,沒有任何問題,但是我真的不明白教授希望我們如何將循環集成到問題中。可以想到的是,他想起來創建一個循環七次,不知怎的,詢問不同的侏儒每個七倍。是否可能?你可以改變的循環,你通過它來運行?我覺得內容。我只是還沒想到這個正確
下面是從以前的分配我的代碼,SANS循環:
import java.util.Scanner;
public class SevenDwarfs {
public static void main(String[] args) {
int counter = 7;
boolean firstSelection = false;
boolean secondSelection = false;
boolean thirdSelection = false;
boolean fourthSelection = false;
boolean fiveSelection = false;
boolean sixSelection = false;
boolean sevenSelection = false;
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Sleepy");
System.out.println("2 Lion-O");
System.out.println("3 Cheetara");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("Hi Ho, you picked the correct one");
firstSelection = true;
break;
case 2:
System.out.println("Wrong selection");
--counter;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Panthro");
System.out.println("2 Bashful");
System.out.println("3 Tigra");
Scanner input2 = new Scanner(System.in);
int choice2 = input2.nextInt();
switch (choice2) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
secondSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Snaf");
System.out.println("2 Doc");
System.out.println("3 Donald Duck");
Scanner input3 = new Scanner(System.in);
int choice3 = input3.nextInt();
switch (choice3) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
thirdSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Mickie Mouse");
System.out.println("2 Sneezy");
System.out.println("3 Minie Mouse");
Scanner input4 = new Scanner(System.in);
int choice4 = input4.nextInt();
switch (choice4) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
fourthSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Heathcliff");
System.out.println("2 Happy");
System.out.println("3 Goofy");
Scanner input5 = new Scanner(System.in);
int choice5 = input5.nextInt();
switch (choice5) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
fiveSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Huey");
System.out.println("2 Grumpy");
System.out.println("3 Dewey");
Scanner input6 = new Scanner(System.in);
int choice6 = input6.nextInt();
switch (choice6) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
sixSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Scrooge McDuck");
System.out.println("2 Dopey");
System.out.println("3 Louie");
Scanner input7 = new Scanner(System.in);
int choice7 = input7.nextInt();
switch (choice7) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
sevenSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
if (firstSelection == true && secondSelection == true
&& thirdSelection == true && fourthSelection == true
&& fiveSelection == true && sixSelection == true
&& sevenSelection == true) {
System.out.println("You earned a gold star!");
} else {
System.out.println("\nYou did not get all correct.");
}
}
}
他要求你用循環替換switch語句。 –
嗨羅曼,你認爲你可以寫一個如何做到這一點的例子? –
@Roman C:我不認爲是這樣。相反,switch語句應該包含在for循環中。 –