2011-03-26 5 views
0

2 개 코드를 하나로 결합하려고하는데 많은 문제가 있습니다. 코드는 그룹 번호와 기부 금액을 물어보고 0을 누를 때까지 되돌아 가야합니다. 0을 누르면 모든 그룹의 총계가 표시됩니다. 여기C# 스위치 루프, 각 경우에 대해 합계가 있습니다.

코드 1 개

using System; 
public class TotalPurchase 
{ 
    public static void Main() 
    { 
     double donation; 
     double total = 0; 
     string inputString; 
     const double QUIT = 0; 
     Console.WriteLine("Please enter the amount of the contribution: "); 
     inputString = Console.ReadLine(); 
     donation = Convert.ToDouble(inputString); 
     while(donation != QUIT) 
     { 
      total += donation; 
      Console.WriteLine("Enter next donation amount, or " + 
       QUIT + " to quit "); 
      inputString = Console.ReadLine(); 
      donation = Convert.ToDouble(inputString); 
     } 
     Console.WriteLine("Your total is {0}", total.ToString("C")); 
    } 
} 

코드 2

using System; 
namespace donate 
{ 

class donate 
{ 
    public static void Main() 
    { 


     begin: 
     string group; 
     int myint; 
     Console.WriteLine("Please enter group number (4, 5, or 6)"); 
     Console.WriteLine("(0 to quit): "); 
     group = Console.ReadLine(); 
     myint = Int32.Parse(group); 

      switch (myint) 
      { 
       case 0: 
        Console.WriteLine("Bye."); 
        break; 
       case 4: 
       case 5: 
       case 6: 
        double donation; 

        string inputString; 

        Console.WriteLine("Please enter the amount of the contribution: "); 
        inputString = Console.ReadLine(); 
        donation = Convert.ToDouble(inputString); 
        goto begin; 
       default: 
        Console.WriteLine("Incorrect grade number.", myint); 
        goto begin; 
       } 




     }  
    } 
} 

그래서 기본적으로 내가 2 코드를 사용하여 각 그룹에 대한 총을 찾으려는 2 개 개의 다른 코드입니다.

도움을 주시면 감사하겠습니다.

+0

설명이 포함 된 학교 프로젝트입니까? –

답변

0

그런 식으로 사용하지 마십시오. 첫 번째 코드 세그먼트에서했던 것처럼 다른 while 루프를 사용하십시오.

이 말을 훨씬 더 간결 것 :

if (myInt > 3 && myInt < 7) { ... } 

대신의 스위치 문을 사용하여.

기본적으로 기부액을 요약하는 코드는 속임수를 사용하므로 그룹 번호가 무엇인지 처리하는 비슷한 루프 안에 그 코드를 붙이면됩니다. 이렇게하면 기부금의 끝과 입력의 끝을 함께 나타내는 다른 입력을 사용하려고합니다. 응용 프로그램에서 "입력을 종료하려면 0을 입력하십시오", "기부 입력을 종료하려면 0을 입력하십시오"라는 순서로 혼란을 겪을 수 있습니다.

0

코드 2에 매우 가깝습니다. 코드 2를 루프 중에 넣으면 작동합니다!

여기서 작성한 유일한 코드는 while 루프 외부에서 myint로 선언되어 0이 아닌 값으로 초기화되었습니다.

double total = 0; 
    int myint = -1; 

    while (myint != 0) 
    { 
     string group; 

     Console.WriteLine("Please enter group number (4, 5, or 6)"); 
     Console.WriteLine("(0 to quit): "); 
     group = Console.ReadLine(); 
     myint = Int32.Parse(group); 

     switch (myint) 
     { 
      case 0: 
       Console.WriteLine("Bye."); 
       break; 
      case 4: 
      case 5: 
      case 6: 
       double donation; 
       string inputString; 
       Console.WriteLine("Please enter the amount of the contribution: "); 
       inputString = Console.ReadLine(); 
       donation = Convert.ToDouble(inputString); 
       total += donation; 
       break; 
      default: 
       Console.WriteLine("Incorrect grade number.", myint); 
       break; 
     } 
    } 

    Console.WriteLine("Your total is {0}", total.ToString("C"));