2017-05-17 223 views
-1

我想教自己C.爲了娛樂和我自己的發展,我創建了一個代碼,提示用戶輸入字母等級,然後輸出該字母等級的範圍。以下是我迄今爲止:確定正確的循環使用

//Ted C. Lim 

#include "stdio.h" 


int main() 
{ 
char grade; 

    printf("Enter a single character grade: "); 
    scanf("%c", &grade); 
    printf("You entered %c as the grade. ", grade); 

    switch(grade) 
    { 
     case 'A': 
      printf("The grade range for A and A- is 100%% - 90%%."); 
      break; 
     case 'B': 
      printf("The grade range for B and B- is 80%% - 89%%."); 
      break; 
     case 'C': 
      printf("The grade range for C and C- is 70%% - 79%%."); 
      break; 
     case 'D': 
      printf("The grade range for D and D- is 60%% - 69%%."); 
     case 'F': 
      printf("The grade range for F is 0%% - 59%%."); 
     default: 
      printf("That grade does not exist."); 
      break; 
    } 


} 

如果你運行程序,你會看到,它要求用戶只有一次,返回正確的輸出,然後停止。我會要做的是無限期地重複提示,直到用戶輸入類似'Q'的東西退出。我知道我應該在這裏使用某種循環,但我不太清楚如何應用它。

回答

0

有幾個不同的選項,你可以在這裏選擇,while和while while循環都可以工作。就我個人而言,我會說while while循環更適合這種情況,主要是因爲您確定知道您希望程序至少提示用戶一次。爲了使用它,您需要將print放在printf語句之前,然後運行,同時末尾的一些scanf輸入是!=「Q」

0

您可以使用while循環以及另一個字符大小寫退出跳出循環。

char grade; 

while (1) 
{ 
    printf("Enter a single character grade (or 'X' to exit): "); 
    scanf(" %c", &grade); 
    printf("You entered %c as the grade. ", grade); 

    if (grade == 'X') // Or another letter, make it clear what you're using 
    { 
     break; 
    } 

    // Output code here... 
} 

我也建議你檢查小寫字母和大寫字母。在switch聲明:

case 'A': 
case 'a': 
    printf("The grade range for A and A- is 100%% - 90%%."); 
    break; 

if聲明:

if (grade == 'X' || grade == 'x') 
{ 
    break; 
} 
0

無限期地重複一個動作,你可以把它包裝一個while循環使用的條件,始終是真正的內部(例如,while (1) { ... })如下:

#include <stdio.h> 
#include <ctype.h> 

int main() 
{ 
    char grade; 

    while (1) 
    { 
     printf("Enter a single character grade (or Q to quit):\n"); 
     scanf(" %c", &grade); 
     grade = toupper(grade); 
     if (grade == 'Q') break; 

     printf("You entered %c as the grade.\n", grade); 

     switch(grade) 
     { 
      case 'A': 
       printf("The grade range for A and A- is 100%% - 90%%.\n"); 
       break; 
      case 'B': 
       printf("The grade range for B and B- is 80%% - 89%%.\n"); 
       break; 
      case 'C': 
       printf("The grade range for C and C- is 70%% - 79%%.\n"); 
       break; 
      case 'D': 
       printf("The grade range for D and D- is 60%% - 69%%.\n"); 
       break; 
      case 'F': 
       printf("The grade range for F is 0%% - 59%%.\n"); 
       break; 
      default: 
       printf("That grade does not exist.\n"); 
       break; 
     } 
    } 
    return 0; 
} 

你會注意到我做了一些其他的修改,我將貫穿這裏:

  1. include "stdio.h"應該真的是#include <stdio.h>。尖括號指示編譯器在標準目錄中查找系統頭文件。

  2. 我還加了#include <ctype.h>因爲我使用toupper()函數將輸入字符轉換爲大寫。這使您的代碼更易於使用,因爲它現在可以接受大寫和小寫字母。

  3. scanf()格式字符串在%c之前包含一個空格。這將跳過包括換行符在內的任何空白字符。沒有它,程序會將這些字符視爲實際輸入,並告訴您\n等級不存在。

  4. 當用戶輸入Q時,可以使用break語句退出循環。switch塊中還有幾個break缺失。

  5. main()函數聲明爲int main() { ... },所以它應該返回一個整數值。如果沒有發生錯誤,則該值應爲零。

0

將您的開關盒置於while循環中,這是真的,它將無限期地運行。您也可以使用scanf來檢查輸入的特定鍵以便將其停止。