2013-01-22 90 views
-2

我需要就如何在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."); 
     } 
    } 
} 
+0

他要求你用循環替換switch語句。 –

+0

嗨羅曼,你認爲你可以寫一個如何做到這一點的例子? –

+2

@Roman C:我不認爲是這樣。相反,switch語句應該包含在for循環中。 –

回答

4

,你意識到你可能不正確思考的概念,並要求提供幫助的事實是一件好事。

閱讀以下內容,熟悉Java中循環。

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

要回答你的問題,是的,你可以運行它改變一個循環的內容。這就是變量的用途。您可以在程序運行時修改它們的值。看看這個例子。隨着循環的每次迭代,變量i都會增加。循環內部的變量outsideLoop也會發生變化。玩這個,你會開始明白。

class ForDemo 
{ 
    public static void main(String[] args) 
    { 
     int outsideLoop = 0; 
     for (int i = 1; i < 11; i++) 
     { 
      outsideLoop += i; 
      System.out.println("Count is: " + i); 
     } 
     System.out.println("Outside loop is: " + outsideLoop); 
    } 
} 

對於打印選擇,獲取用戶輸入以及驗證用戶輸入的過程,您已經有了一個很好的起點。重複該塊(循環)7次。