如何使用if语句仅在输入字符时执行括号内的操作。但是当输入整数/无值时,将它们发送到else语句。
#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <stdbool.h>
int main(void)
{
// use boolean for printing if user fails(true) or passes(false) test
int score = 0; //score starts at 0, and 10 points are added if correct.
char answer1;
char answer2;
char answer3;
char answer4;
char answer5;
char answer6;
char answer7;
char answer8;
char answer9;
char answer10;
system("clear");
printf("\n1. What is the capital of Russia?\n");
printf("a)Washington DC \nb)Moscow\nc)Copenhagen\nd)Stockholm\n");
scanf("%s", &answer1);
system("clear");
if (answer1 == 'b') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer1 != 'b') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
else {
printf("\nPlease enter in a valid response\n");
}
printf("\n2. Who was the first president of the United States?\n");
printf("a)Washington\nb)Lincoln\nc)Obama\nd)Adams\n");
scanf("%s", &answer2 &&);
system("clear");
if (answer2 == 'a') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer2 != 'a') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n3. How many states are there in the United States?\n");
printf("a)25\nb)87\nc)42\nd)50\n");
scanf("%s", &answer3);
system("clear");
if (answer3 == 'd') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer3 != 'd') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n4. What is the square root of 144?\n");
printf("a)12\nb)14\nc)7\nd)24\n");
scanf("%s", &answer4);
system("clear");
if (answer4 == 'a') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer4 != 'a') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n5. What is the most basic unit of human life?\n");
printf("a)Skin\nb)Mitochondra\nc)Cell\nd)ATP\n");
scanf("%s", &answer5);
system("clear");
if (answer5 == 'c') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer5 != 'c') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n6. What programming language is this written in?\n");
printf("a)Objective-C\nb)C++\nc)C#\nd)C\n");
scanf("%s", &answer6);
system("clear");
if (answer6 == 'd') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer6 != 'd') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n7. What is apple's newest programming language?\n");
printf("a)Swift\nb)Java\nc)Python\nd)Objective-C\n");
scanf("%s", &answer7);
system("clear");
if (answer7 == 'a') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer7 != 'a') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n8. What is the most portable way to store data?\n");
printf("a)Ram\nb)Flash Drive\nc)Solid-state drive\nd)Hard Drive\n");
scanf("%s", &answer8);
system("clear");
if (answer8 == 'b') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer8 != 'b') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n9. What is the name of IBM's AI?\n");
printf("a)Arnold\nb)Jonathan\nc)Watson\nd)Pablo\n");
scanf("%s", &answer9);
system("clear");
if (answer9 == 'c') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer9 != 'c') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
printf("\n10. What is the universal sign for peace?\n");
printf("a)Index finger and thumb raised\nb)Index finger raised\nc)Middle finger raised\nd)Index and middle finger raised\n");
scanf("%s", &answer10);
system("clear");
if (answer10 == 'd') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
}
if (answer10 != 'd') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
if (score > 70) {
printf("\nCongratulations! You have passed!\n");
}
else {
printf("\nUnfortunately, you have failed. Please try again\n");
}
}
这是我的代码,我尝试在if语句下面添加一个else语句来表示输入有效的响应,但它不起作用。请帮忙。
编辑:我修好了
q1:
printf("\n1. What is the capital of Russia?\n");
printf("a)Washington DC \nb)Moscow\nc)Copenhagen\nd)Stockholm\n");
scanf(" %c", &answer1);
system("clear");
if (answer1 == 'a' || answer1 == 'b' || answer1 == 'c' || answer1 == 'd') {
if (answer1 == 'b') {
printf("Correct!\n");
(score += 10); //if answer is correct, score adds 10
printf("Your score is: %i\n",score);
goto q2;
}
if (answer1 != 'b') {
printf("Wrong!\n");
printf("Your score is: %i\n",score);
}
}
else {
printf("Please enter a letter a-b!");
goto q1;
}
答案 0 :(得分:1)
首先检查用户是否输入了有效的内容......
SELECT *
FROM customers
WHERE upper(fname) LIKE '_[A-D]%';
答案 1 :(得分:0)
假设:
char answer1;
然后
scanf("%s", &answer1);
尝试读取字符串并将其放在answer1
中。这将始终覆盖answer1
之后的内存,因为即使您只输入一个字母,字符串也将以空值终止。结果,您的程序变得不可靠。相反(见P.P.的评论),使用:
scanf(" %c", &answer1);