none
[Dev C]猜英文字母 RRS feed

  • 問題

  • 你好各位大大,我是個大學生,程式語言的作業一直找不出問題在哪

    問題:

    請寫一支C語言程式來進行猜字母的遊戲,你的程式先從A到Z機選取一個字母(大小寫不限),然後於螢幕上印出: "I have selected a character between A and Z. Try to guess the character selected by me! What is your guess?" 玩家就開始輸入所猜的字母. 你程式對玩家所猜的字母做出下列三種反應中的一種: 1.Great! Your guess is right! 2.Way ahead! Please try again. 3.Way behind! Please try again. 如果玩家猜錯, 你的程式要重複直到玩家猜對為止。 玩家可透過程式的反應way ahead (更前)或way behind(更後), 來協助玩家找到正確答案。  ------- 隨機參考方式, #include <stdlib.h style="font-family:'Times New Roman';letter-spacing:2px;background-color:#ecf1f7;">#include <time.h>srand(time(NULL)); //以時間種子產生亂數 a=(rand()%100)+1; // 產生1~100之間的亂數 --- ASCII 大寫字母A對應十位數為65 大寫字母Z對應 90 小寫字母a對應十位數為 97 小寫字母z對應122</time.h></stdlib.h>

    這是我的程式碼:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    char guess(char random);
    char guess(char random);

    int main()
    {
    char random;
    printf("I have selected a character between A and Z. Try to guess the character selected by me! What is your guess?\n");
    srand(time(NULL));//設定A~Z的變數 
    random=(rand()%25)+65; 
    guess(random);
    return 0 ;
    }
    char judgment(char guessword,char random)//判定字母是否正確 
    {
    if(guessword==random) 
    {
    printf("Great! Your guess is right!\n");
    }
    else if (guessword<random)
    {
    printf("Way behind! Please try again.\n");
    guess(random);
    }
    else if (guessword>random)
    {
    printf("Way ahead! Please try again.\n");
    guess(random);
    }


    char guess(char random) //猜字母 
    {
    char guessword;
    printf("Please,enter your guess!\n");
    scanf("%c",&guessword);
    judgment(guessword,random);
    }

    以下為執行結果:

    I have selected a character between A and Z. Try to guess the character selected by me! What is your guess?
    The answer is H.
    Please,enter your guess!
    A
    Way behind! Please try again.
    Please,enter your guess!
    Way behind! Please try again.
    Please,enter your guess!
    B
    Way behind! Please try again.
    Please,enter your guess!
    Way behind! Please try again.
    Please,enter your guess!
    Z
    Way ahead! Please try again.
    Please,enter your guess!
    Way behind! Please try again.
    Please,enter your guess!
    W
    Way ahead! Please try again.
    Please,enter your guess!
    Way behind! Please try again.
    Please,enter your guess!
    H
    Great! Your guess is right!

    如果輸入正確的話很正常

    輸入錯誤會跑出兩行,而且有時候還會兩行都不同

    2018年10月13日 下午 03:31

解答

  • 試試看這樣寫:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    
    char guess(char random);
    int judgment(char guessword, char random);//判定字母是否正確 
    
    int main()
    {
    	char random = '\0';
    	char guessword = '\0';
    
    	printf("I have selected a character between A and Z. Try to guess the character selected by me! What is your guess?\n");
    	srand(time(NULL));//設定A~Z的變數 
    	random = (rand() % 26) + 65;
    	
    	do {
    		guessword =guess(random);
    	} while (!judgment(guessword, random));
    	return 0;
    }
    int judgment(char guessword, char random)//判定字母是否正確 
    {
    	int returnvalue = 0;
    
    	if (guessword == random)
    	{
    		printf("Great! Your guess is right!\n");
    		returnvalue = 1;
    	}
    	else if (guessword<random)
    	{
    		printf("Way behind! Please try again.\n");
    	}
    	else if (guessword>random)
    	{
    		printf("Way ahead! Please try again.\n");
    	}
    	return returnvalue;
    }
    
    char guess(char random) //猜字母 
    {
    	char returnvalue = '\0';
    	printf("Please,enter your guess!\n");
    	returnvalue=_getche();
    	printf("\n");
    	return returnvalue;
    }

    • 已標示為解答 林宏勳 2018年10月14日 上午 03:57
    2018年10月13日 下午 11:41