我必須使它成為一個無限回圈,并在輸入錯誤字符時讓它回到開始。該怎么辦?
到目前為止,這是我的代碼:
#include <stdio.h>
#include <cs50.h>
int main ()
{
// Prompt user for input last choice contender
char c;
printf("Last choice: ");
c = getchar();
// If last choice is R, loser should play S
if (c == 'R'|| c == 'r')
{
printf("Your next move: S");
}
// Else if last choice is P, loser should play R
else if (c == 'P' || c == 'p')
{
printf("Your next move: R");
}
// Else if last choice is S, loser should play P
else if (c == 'S' || c == 's')
{
printf("Your next move: P");
}
// TO DO: If another character start over
else if (c != 'S'|| c != 's' || c != 'P' || c != 'p' || c != 'R'|| c != 'r')
}
// TODO: make an infinite loop / start over
uj5u.com熱心網友回復:
我有多種方法可以做到這一點,我建議使用 while 回圈。
添加一段時間(){
圍繞你想要重申的所有代碼,在 while() 引數中,你可以有一個像這樣的布林值 boolean won = false, while(!won) 并在玩家獲勝或不想要的時候添加 won = true玩家繼續你的游戲!但我也建議閱讀有關 while 回圈的內容,google while loop in google
uj5u.com熱心網友回復:
如果我確實正確理解了您的需求,您可以這樣做:
#include <stdio.h>
#include <cs50.h>
int main ()
{
while(true) // or while(1) if you don't want to include stddef or any other lib containing true and false declaration
// Prompt user for input last choice contender
{
char c;
printf("Last choice: ");
c = getchar();
// If last choice is R, loser should play S
if (c == 'R'|| c == 'r')
{
printf("Your next move: S");
}
// Else if last choice is P, loser should play R
else if (c == 'P' || c == 'p')
{
printf("Your next move: R");
}
// Else if last choice is S, loser should play P
else if (c == 'S' || c == 's')
{
printf("Your next move: P");
}
// TO DO: If another character start over
else if (c != 'S'|| c != 's' || c != 'P' || c != 'p' || c != 'R'|| c != 'r')
}
}
此外,如果沒有遇到授權字符,您無需執行任何其他操作即可讓您的代碼在回圈開始時重新啟動。您甚至不需要使用最新的 else if 子句來捕獲它們,因為無論如何到達回圈中的代碼末尾,一旦到達那里就不會再做任何事情了。(否則 if (c != 'S'|| c != 's' || c != 'P' || c != 'p' || c != 'R'|| c != 'r' )) 但是,如果你希望保留你的空(沒有子 {})else if 子句,你應該添加一個 ; 在子句的末尾,否則我不知道您當前的代碼將如何編譯。
我歡迎你來堆疊溢位,但我建議你遵循一些關于 c 語言的基本指南,看起來你缺少一些嚴肅的基礎,這會一直阻礙你目前的學習道路。總的來說,我鼓勵你繼續學習 :) 祝你好運!
uj5u.com熱心網友回復:
您可以使用無限回圈(例如for(;;)or while(1)),它將永遠運行,直到break遇到明確的陳述句。您可以break在確定輸入有效后使用該陳述句。在那之前,回圈將永遠運行。
在這種情況下,您可以簡單地使用陳述句,而不是使用長鏈if... ,然后使用 function 將字母變為大寫。else ifswitchtoupper
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
for (;;) //infinite loop
{
//NOTE: The following variable must be an "int" in order to
//be able to distinguish EOF from an actual character.
int c;
printf("Last choice: ");
c = getchar();
switch ( toupper( c ) )
{
case 'R':
printf( "Your next move: S" );
break;
case 'P':
printf( "Your next move: R" );
break;
case 'S':
printf( "Your next move: P" );
break;
case EOF:
fprintf( stderr, "unexpected error!\n" );
exit( EXIT_FAILURE );
default:
printf( "Input was not ok, please try again.\n" );
continue;
}
//input was ok, so we can break out of the infinite loop
break;
}
}
但是,這個程式還不能正常作業:
Last choice: q
Input was not ok, please try again.
Last choice: Input was not ok, please try again.
Last choice:
Last choice: This is a test.
Input was not ok, please try again.
Last choice: Input was not ok, please try again.
Last choice: Input was not ok, please try again.
Last choice: Your next move: P
As you can see, the program prints the error message several times for the same line of input. This is because getchar will return every character on the line, including the newline character at the end of the line, and every character will be processed individually, until it finds one of the desired characters. This is not what you want. Since user input is line-based, it would make more sense to read a whole line of input at once, and reject the whole line of input if it contains more than one character (in addition to the newline character). In order to read a whole line of input, I recommend using the function fgets:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main ()
{
for (;;) //infinite loop
{
//need space for 1 character, the newline character and
//the terminating null character
char line[3];
printf("Last choice: ");
//attempt to read one line of input
if ( fgets( line, sizeof line, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//make sure that line was not too long
if ( strchr( line, '\n' ) == NULL )
{
int c;
printf( "Please only enter a single character!\n" );
//discard remainder of line
do
{
c = getchar();
if ( c == EOF )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
} while ( c != '\n' );
continue;
}
switch ( toupper( (unsigned char)line[0] ) )
{
case 'R':
printf( "Your next move: S" );
break;
case 'P':
printf( "Your next move: R" );
break;
case 'S':
printf( "Your next move: P" );
break;
default:
printf( "Input was not ok, please try again.\n" );
continue;
}
//input was ok, so we can break out of the infinite loop
break;
}
}
Now the program works properly:
Last choice: This is a test.
Please only enter a single character!
Last choice: q
Input was not ok, please try again.
Last choice: s
Your next move: P
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453935.html
