2015-01-07 4 views
-5

나는 루프를 전환하는 새로운 오전 만들고이 통화 변환기 프로그램에 여러 가지 문제가 있습니다. 먼저, 사용자가 -1을 입력 할 때까지 계속 값을 입력하는 사례 1을 반복하여 루프를 중지하고 계속 이동하려고합니다. 현재로서는 그렇게하지 않습니다. 스위치 1에 GPR 값을 입력하고 원래 GPR 값을 저장 한 상태로 메뉴로 되돌아갑니다.입니까? while 루프? 환율 계산기 프로그램 java

코드는 여기에 있습니다 :

import java.util.Scanner; 
public class Conversion { 

public static void main(String[] args) { 
    double pound; 
    double euro; 
    double dollars; 
    double yen; 
    double rupees; 
    double poundEuro; 
    double poundDollars; 
    double poundYen; 
    double poundRupees; 
    int Choice; 


    Scanner input = new Scanner(System.in); 
    Scanner exchange = new Scanner(System.in); 

    System.out.println("Please choose an option:"); 
    System.out.println("1. Enter values and type -1 to stop"); 
    System.out.println("2. Euros (1GBP = 1.28EUR)"); 
    System.out.println("3. Dollars (1GBP = 1.51USD)"); 
    System.out.println("4. Yen (1GBP = 179.80JPY)"); 
    System.out.println("5. Rupees (1GBP = 95.60INR)"); 
    System.out.println("6. Exit"); 

    Choice = input.nextInt(); 


    switch (Choice) { 

    case 1: 

     while (!exchange.equals("-1"));{ 
      pound = exchange.nextDouble(); 
      System.out.print("Please enter your values you would like to exchange (Type '-1' to stop) "); 
     } 
    case 2: 
     pound = exchange.nextDouble(); 
     dollars = 1.51; 
     poundDollars = pound * dollars; 
     System.out.println("Your amounts in euros are" + poundDollars); 

    case 3: 
     pound = exchange.nextDouble(); 
     yen = 1.28; 
     poundYen = pound * yen; 
     System.out.println("Your amounts in euros are" + poundYen); 

    case 4: 
     pound = exchange.nextDouble(); 
     rupees = 1.28; 
     poundRupees = pound * rupees; 
     System.out.println("Your amounts in euros are" + poundRupees); 

    case 5: 
     pound = exchange.nextDouble(); 
     euro = 1.28; 
     poundEuro = pound * euro; 
     System.out.println("Your amounts in euros are" + poundEuro); 

    case 6: 
     break; 
    } 

    input.close(); 
    exchange.close(); 


} 

}

+4

'switch' 문은 루프 수 없습니다. – immibis

+5

''나는 루프를 바꾸는 것이 새롭다. '- 나는 그렇다. 사실 나는 그런 짐승에 대해 들어 본 적이 없다. while 루프와 같은 실제 루프 구조를 사용하는 것이 좋습니다. 튜토리얼이 도움이 될 것입니다. –

+0

아니요, 아니요, 'swich'루프, 'switch'루프가 아닙니다. –

답변

1

switch 루프 아니다. if과 같은 분기 구문입니다. break이 있는데, 그 이유는 다음 case 문으로 넘어갈 수 있기 때문입니다. 현재 대부분의 경우에 빠지기 시작합니다. 이는 사례 사이에 break 문을 입력하는 것을 잊어 버렸기 때문입니다.

0

switch 문은 루프가 아닙니다. 이 함수를 일련의 else-if 문 대신 사용하는 것으로 간주하십시오. 코드는 실제로 더 같이 읽

if(Choice == 1) 
{ 

} 
else if(Choice == 2) 
{ 

} 
else if(Choice == 3) 
{ 

} 
else if(Choice == 4) 
{ 

} 
else if(Choice == 5) 
{ 

} 
else if(Choice == 5) 
{ 

} 

은 그러나, 스위치의 경우는 break 문을 종료해야합니다. 위의 else-ifs와 달리 그렇지 않은 경우 실행은 다음 사례로 넘어가 결국 코드가 break에 도달하거나 모든 사례를 통과 할 때까지 해당 사례에서 코드를 실행합니다.

while 루프가 switch 문을 감싸고 싶다면 while 루프를 먼저 수정해야합니다. 세미콜론으로 종료 했으므로 무한대로 끝내야합니다. while 루프는 몸체가 없다면 세미콜론을 필요로하지 않지만 조건부 검사에서 부작용이 발생하면 결국 종료됩니다.

while(blah) 
{ 
    switch(blah) 
    { 
     case 1: 
      // Do stuff for this case 
      break; 
     case 2: 
      // Do stuff for this case 
      break; 
     default: 
      // Do stuff when no case is matched 
      break; 
    } 
} 
0

나는 약간 혼란 스럽다고 생각합니다. 그의 의견에서 언급 한 바와 같이, switch 문은 루프가 아니므로 switch안에 루프를 넣어야합니다.

그런 다음 case의 끝에 break 문이 누락되었습니다. the Java tutorials 인용 :

break 문을 둘러싸는 switch 문을 종료합니다. 제어 흐름은 switch 블록 다음의 첫 번째 명령문으로 계속됩니다. break 문, 때문에 그들없이 통해 switch 블록 가을에 문 필요한 같습니다 break 문을 만날 때까지 관계없이 다음 case 라벨의 표현의 일치하는 case 레이블이 순서대로 실행 한 후 모든 문. 각 case 블록의 끝에서 그 break 문을 넣지 않으면

public static void main(String[] args) { 
    // Variable definitions 
    while(true) { // This is a little trick to force the application to repeat 
        // a task until you explicitly break the loop 
     // Code to print your menu 
     choice = input.nextInt(); 
     if(choice == -1 || choice == 6) 
      break; // Break the while loop if choice is -1 or 6 
     switch(choice){ 
      case 1: 
       // Your code for option 1 
       break; 
      case 2: 
       // Your code for option 2 
       break; 
      // More cases 
      default: 
       System.out.println("You must enter an option between 1 and 6!"); 
       break; // Not strictly needed 
     } 
    } 
} 

이 프로그램은 모든 옵션을 통해 떨어질 것 :

여러분의 프로그램은 다음과 같이해야한다.

읽어 보시기 바랍니다 :


또 다른 옵션이 같은 것입니다 :

public static void main(String[] args) { 
    // Variable definitions 
    /* 
     Now let's use a labeled loop 
    */ 
    menu: while(true) { 
     // Code to print your menu 
     choice = input.nextInt(); 
     switch(choice){ 
      case -1: // Nothing goes here, so the execution will fall through 
         // to the next case 
      case 6: 
       break menu; // This will break the menu loop. 
          // Since the previous case simply falls through to 
          // this one, this will happen if choice is 
          // either -1 or 6 
      case 1: 
       // Your code for option 1 
       break; 
      case 2: 
       // Your code for option 2 
       break; 
      // More cases 
      default: 
       System.out.println("You must enter an option between 1 and 6!"); 
       break; // Not strictly needed 
     } 
    } 
} 

더 읽기 :