2017-12-06 2 views
-2

내 if 문에서 사용자에게 점수를 묻기를 원하는 경우를 제외하면 내 코드가 거의 완료되었으며 정상적으로 작동합니다. 지금 당장은 사용자에게 점수를 묻습니다. 잘못된 항목을 입력하면 다시 시도하십시오.라는 메시지가 표시되지만 코드가 중지됩니다. 사용자가 잘못된 번호를 입력 할 때까지 계속 묻는 것이 좋습니다. 값이 (당신의 if 조건에 따라) 그 숫자되지 입력하면사용자에게 점수를 묻는 메시지가 계속 표시되지 않음

//Variables 
      double grade; 
      string studentName; 
      //Prompt the user for the student's name 
      Console.WriteLine("Please enter the student's name:"); 
      studentName = Console.ReadLine(); 
      //Prompt the user for the student's score 
      Console.WriteLine("Please enter the student's score between 0 and 100:"); 
      if (!(double.TryParse(Console.ReadLine(), out grade))) 
      { 
       Console.WriteLine("Invalid entry, scores entered must be numeric. Please try again"); 
      } 
      else if (grade >=90) 
      { 
       Console.WriteLine("{0} has a score of {1} which is an A.", studentName, grade); 
      } 
      else if (grade < 90 && grade >= 80) 
      { 
       Console.WriteLine("{0} has a score of {1} which is a B.", studentName, grade); 
      } 
      else if (grade < 80 && grade >=70) 
      { 
       Console.WriteLine("{0} has a score of {1} which is a C.", studentName, grade); 
      } 
      else if (grade < 70 && grade >= 60) 
      { 
       Console.WriteLine("{0} has a score of {1} which is a D.", studentName, grade); 
      } 
      else 
      { 
       Console.WriteLine("{0} has a score of {1} which is an F.", studentName, grade); 
      } 
      Console.ReadLine(); 
+2

루프가 없으므로 사용하지 마십시오. –

+0

당신은'do/while'을 사용할 수 있습니다. – Nkosi

+1

* 유효한 * 번호를 입력 할 때까지 그 뜻을 추측합니다 - 그 시점에서 결과를 출력하고 멈추거나 프롬프트를 표시해야합니까? 또한 내가이 운동을 표시했다면 불필요하게 반복되는 코드의 양을 너무 친절하게 보지 않을 것입니다. –

답변

2

당신은 무한 while 루프 인

while (true) 
{ 
    Console.WriteLine("Please enter the student's name:"); 
    studentName = Console.ReadLine(); 
    //Prompt the user for the student's score 
    Console.WriteLine("Please enter the student's score between 0 and 100:"); 
    if (!(double.TryParse(Console.ReadLine(), out grade))) 
    { 
     Console.WriteLine("Invalid entry, scores entered must be numeric. Please try again"); 
     break; 
    } 
    else if (grade >= 90) 
    { 
     Console.WriteLine("{0} has a score of {1} which is an A.", studentName, grade); 
    } 
    else if (grade < 90 && grade >= 80) 
    { 
     Console.WriteLine("{0} has a score of {1} which is a B.", studentName, grade); 
    } 
    else if (grade < 80 && grade >= 70) 
    { 
     Console.WriteLine("{0} has a score of {1} which is a C.", studentName, grade); 
    } 
    else if (grade < 70 && grade >= 60) 
    { 
     Console.WriteLine("{0} has a score of {1} which is a D.", studentName, grade); 
    } 
    else 
    { 
     Console.WriteLine("{0} has a score of {1} which is an F.", studentName, grade); 
    } 
} 

내가 while-loop을 깰 수있는 break 문을 포함 할 수 있습니다.