当我在这段代码中输入字符串而不是y或n时,它将返回大数。
所以我想知道如何修复它。
我的第一个想法是,如果我输入字符串而不是这个,它将返回到第一个段落并再次运行。
但当我尝试使用它时,它会返回并再次运行。
但它将保存字符串并将其更改为ASCII代码。
#include <stdlib.h>;
#include <stdio.h>;
int main() {
int num = 1; //設置迴圈變數
int change; //設置可變動變數
int count=0; // 跑了幾次
char ans1,ans2,ans3,ans4,ans5,ans6,ans7 ; //答案共7個
int final_ans = 0;//最後答案
printf("thinking a number on your mind, and I will guess it!\n\n");
first:
for(num=1; num<=100; num+=2) {
++count;
printf("%-4.2d", num);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
count = 0;//清空變數
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans1);
if((ans1=='Y') || (ans1=='y')) { //若有數字
final_ans += 1;
}
second:
for(num=2; num<=100; num+=2) {
change = num;
++count;
if(count%2==0) {
change--;
}
printf("%-4.2d", change);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans2);
count = 0;//清空變數
change = 0;//清空變數
if((ans2=='Y') || (ans2=='y')) {
final_ans += 2;
}
third:
for(num=4; num<=100; num++) {
change = num;
++count;
if(count%4==1&&num!=4) {
num+=4;
}
printf("%-4.2d", num);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans3);
count = 0;//清空變數
change = 0;//清空變數
if((ans3=='Y') || (ans3=='y')) {
final_ans +=4;
}
fourth:
for(num=8; num<=100; num++) {
change = num;
++count;
if(count%8==1&&num!=8) {
num+=8;
}
if(num>100) {
break;
}
printf("%-4.2d", num);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans4);
count = 0;//清空變數
change = 0;//清空變數
if((ans4=='Y') || (ans4=='y')) {
final_ans += 8;
}
fifth:
for(num=16; num<=100; num++) {
change = num;
++count;
if(count%16==1&&num!=16) {
num+=16;
}
if(num>100) {
break;
}
printf("%-4.2d", num);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans5);
count = 0;//清空變數
change = 0;//清空變數
if((ans5=='Y') || (ans5=='y')) {
final_ans += 16;
}
sixth:
for(num=32; num<=100; num++) {
change = num;
++count;
if(count%32==1&&num!=32) {
num+=32;
}
if(num>100) {
break;
}
printf("%-4.2d", num);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans6);
count = 0;//清空變數
change = 0;//清空變數
if((ans6=='Y') || (ans6=='y')) {
final_ans += 32;
}
seventh:
for(num=64; num<=100; num++) {
++count;
printf("%-4.2d", num);//4格寬 數字顯示2位
if(count%9==0) {
printf("\n");//每執行9次換行
}
}
printf("\n");
printf("Is your number on the table (y/n)? \n");
scanf("%s", &ans7);
if((ans7=='Y') || (ans7=='y')) {
final_ans += 64;
}
else if((ans1=='n') || (ans1=='N')) { //若無數字
final_ans += 0;
goto second;//回到第二個table
} else if((ans2=='n') || (ans2=='N')) { //若無數字
final_ans += 0;
goto third;//回到第三個table
} else if((ans3=='n') || (ans3=='N')) { //若無數字
final_ans += 0;
goto fourth;//回到第四個table
} else if((ans4=='n') || (ans4=='N')) { //若無數字
final_ans += 0;
goto fifth;//回到第五個table
} else if((ans5=='n') || (ans5=='N')) { //若無數字
final_ans += 0;
goto sixth;//回到第六個table
} else if((ans6=='n') || (ans6=='N')) { //若無數字
final_ans += 0;
goto seventh;//回到第七個table
} else if((ans7=='n') || (ans7=='N')) { //若無數字
final_ans += 0;
}
if(final_ans<=100) { //判斷最後答案
printf("your secret number is %d",final_ans);
} else if(final_ans>100) {
printf("WRONG!%d is not a number form 1~100",final_ans);
}
}发布于 2019-12-03 02:23:12
您已经在每个库之后放置了;,请删除它
#include <stdlib.h>; // remove the ;
#include <stdio.h>; // remove the ;正确的代码必须是
#include <stdlib.h>
#include <stdio.h>通过编辑,我得到了正确的答案。这对我很管用。
我已经想到了73
输出-:
thinking a number on your mind, and I will guess it!
01 03 05 07 09 11 13 15 17
19 21 23 25 27 29 31 33 35
37 39 41 43 45 47 49 51 53
55 57 59 61 63 65 67 69 71
73 75 77 79 81 83 85 87 89
91 93 95 97 99
Is your number on the table (y/n)?
y
02 03 06 07 10 11 14 15 18
19 22 23 26 27 30 31 34 35
38 39 42 43 46 47 50 51 54
55 58 59 62 63 66 67 70 71
74 75 78 79 82 83 86 87 90
91 94 95 98 99
Is your number on the table (y/n)?
n
04 05 06 07 12 13 14 15 20
21 22 23 28 29 30 31 36 37
38 39 44 45 46 47 52 53 54
55 60 61 62 63 68 69 70 71
76 77 78 79 84 85 86 87 92
93 94 95 100
Is your number on the table (y/n)?
n
08 09 10 11 12 13 14 15 24
25 26 27 28 29 30 31 40 41
42 43 44 45 46 47 56 57 58
59 60 61 62 63 72 73 74 75
76 77 78 79 88 89 90 91 92
93 94 95
Is your number on the table (y/n)?
y
16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 48 49
50 51 52 53 54 55 56 57 58
59 60 61 62 63 80 81 82 83
84 85 86 87 88 89 90 91 92
93 94 95
Is your number on the table (y/n)?
n
32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58
59 60 61 62 63 96 97 98 99
100
Is your number on the table (y/n)?
n
64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81
82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99
100
Is your number on the table (y/n)?
n
64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81
82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99
100
Is your number on the table (y/n)?
y
your secret number is 73 https://stackoverflow.com/questions/59141129
复制相似问题