2014-09-15 2 views
-1

do-while 루프를 while 루프로 변환하려면 어떻게해야합니까?do while 루프를 while 루프로 변환하는 방법

int numAttempts = 0; 

do 
{ 
    System.out.println("Do you want to convert to Peso or Yen?"); 
    pesoOrYen = readKeyboard.nextLine(); 

    if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen")) 
    { 
     notPesoOrYen = false; 
    } 
    else if (numAttempts < 2) 
    { 
     System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:"); 
     notPesoOrYen = true; 
    } 

    numAttempts++; 

} while(notPesoOrYen==true && numAttempts < 3); 

나는 그 문을 while(notPesoOrYen==true && numAttempts < 3)을 시도했지만 작동하지 않았다.

내 전체 코드

패키지 currencyconverter;

import java.util.Scanner; 가져 오기 java.text.NumberFormat;

공용 클래스 CurrencyConverter {

public static void main(String[] args) 
{ 
    Scanner readKeyboard = new Scanner(System.in); 

    double doubleUsersCaptial; 
    boolean notPesoOrYen=true; 
    String pesoOrYen; 
    double usersConvertedCapital; 
    boolean userInputToRunProgramAgain=true; 

    final double US_DOLLAR_TO_PESO = 13.14; 
    final double US_DOLLAR_TO_YEN = 106.02; 


    do 
    { 

     System.out.println ("How much money in US dollars do you have?"); 
     String usersCaptial = readKeyboard.nextLine(); 
     doubleUsersCaptial = Double.parseDouble(usersCaptial); 

     int numAttempts = 0; 

     do 
     { 
      System.out.println ("Do you want to convert to Peso or Yen?"); 
      pesoOrYen  = readKeyboard.nextLine(); 



      if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen")) 
      { 
       notPesoOrYen = false; 
      } 
      else if (numAttempts < 2) 
      { 
       System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:"); 
       notPesoOrYen = true; 

      } 
     numAttempts++; 
     }while(notPesoOrYen==true && numAttempts < 3); 

     if(numAttempts==3) 
     { 
      System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type."); 
      System.out.println("You entered the wrong currency type too many times\nGood Bye"); 
      System.exit(0); 
     } 

     if (pesoOrYen.equalsIgnoreCase("Peso")) 
     { 
      usersConvertedCapital = doubleUsersCaptial*US_DOLLAR_TO_PESO; 
     } 
     else 
     { 
      usersConvertedCapital = doubleUsersCaptial*US_DOLLAR_TO_YEN; 
     } 


     NumberFormat formatter  = NumberFormat.getCurrencyInstance(); 
     String formatUsersCaptial = formatter.format(doubleUsersCaptial); 
     String formatUsersConvertedCapital = formatter.format(usersConvertedCapital); 


     System.out.println(formatUsersCaptial+"US Dollars = " 
          +formatUsersConvertedCapital+" "+pesoOrYen); 
     System.out.println("Would you like to run the Program Again?(enter 'yes' or 'no')"); 
     String runProgramAgain = readKeyboard.nextLine(); 


     if (runProgramAgain.equalsIgnoreCase("yes")) 
     { 
      userInputToRunProgramAgain = true; 
     } 
     else if (runProgramAgain.equalsIgnoreCase("no")) 
     { 
      System.out.println("Goood Bye"); 
      System.exit(0);  
     } 

     else 
     { 
      System.out.println ("You entered something other than 'yes' or 'no'\n" 
           +"Good Bye"); 
      System.exit(0); 
     } 
    }while (userInputToRunProgramAgain==true); 
} 

}

+0

다른 방법이 루프를 다시 작성하려고하는 이유는 어떤 특별한 이유? – Bruno

+2

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html 여기에서 시작하십시오. – maress

+0

@Herman - while 루프와 do/while 루프의 차이점을 알고 있습니까? – jww

답변

0

whiledo... while 거의 동일 while도 그것을 평가하는 반면, do... while은 단순히 처음으로 종료 조건을 평가하기 전에 반복을 수행 첫 번째 반복 (결국 while 루프의 본문은 결코 다시 연결할 수 없지만 do... while 본문은 항상 한 번 이상 실행됩니다).

코드 스 니펫이 완전하지 않지만 루프 이전에 notPesoOrYentrue으로 초기화하지 않았으므로 그것이 작동하지 않는 것입니다. 마지막으로 while(notPesoOrYen==true && numAttempts < 3)을 쓰지 않고 while(notPesoOrYen && numAttempts < 3)이라고 쓰면 == true 비교가 필요하지 않습니다.

+0

내 전체 코드를 넣으려면 편집했습니다 –

+0

한 번 프로그램을 실행 한 후 사용자가 다른 통화 유형을 입력하게하지 않았습니다. –

+0

'while'과 'do while'의 유일한 차이점은 첫 번째 반복이므로 conitnuation 조건의 시작은 사실입니다 (귀하의 경우), 그들은 엄격하게 동일합니다. – Dici

0

루프 동안 밖에서 부울 변수를 초기화한다 :

int numAttempts = 0; 
boolean notPesoOrYen=true; 
while (notPesoOrYen && numAttempts < 3) { 
    System.out.println("Do you want to convert to Peso or Yen?"); 
    pesoOrYen = readKeyboard.nextLine(); 

    if (pesoOrYen.equalsIgnoreCase("Peso") || pesoOrYen.equalsIgnoreCase("Yen")) { 
     notPesoOrYen = false; 
    } else if (numAttempts < 2) { 
     System.out.println("Sorry, but '" + pesoOrYen + "' is not a valid currency type. Try again:"); 
     notPesoOrYen = true; 
    } 
    ++numAttempts; 
}; 
관련 문제