为什么在输入大于 9 x 的值后我不能输入更多的输入?
Why can't I enter more inputs after entering bigger than 9 x values?
#include <stdio.h>
main()
{
int n;
int x;
int h=24;
int s=9;
scanf("%d",&n);
while (n>0)
{
n=n-1;
scanf("%d",&x);
while (x<=9)
{
if (x==1)
printf("2\n");
if (x==2)
printf ("3\n");
if (x==3)
printf ("5\n");
if (x==4)
printf("7\n");
if (x==5)
printf("11\n");
if (x==6)
printf("13\n");
if (x==7)
printf("17\n");
if (x==8)
printf("19\n");
if (x==9)
printf("23\n");
break;
}
while (23<h<542 && x>9)
{
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 )
{
s=s+1;
if (x==s)
printf("%d\n",h);
}
}
}
}
代码的问题是输入n,这将是以下输入的数量。每个输入必须给出第 x 个素数。
示例:
输入:
3个
,4
,20
,50
输出:
7
,71
,229.
x 可以在 1 到 100 之间。(第 100 个素数)
现在我的问题是 x>9.after 为它输入一个值,它将不再接受 x.
的值
我想知道为什么会发生这种情况以及如何解决它。
我对编程也很陌生,还没有学过数组。(我知道 scanfs 不是最好的东西,但这就是我到目前为止所学的全部)
这一行:
while (23<h<542 && x > 9)
当 x
大于 9
时, 正在创建一个无限循环。 23 < h < 542
不测试 h
是否在 23
和 542
之间。该表达式等效于 (23 < h) < 542
。如果 23
小于 h
,(23 < h)
就是 1
,这种情况总是如此,因为 h
从 24
开始并且循环增加它。 1
总是小于 542
.
你想要的是:
if (x > 9) {
while (h < 542)
...
}
}
不需要每次循环都测试x
,因为它在循环中永远不会改变。并且不需要测试 23 < h
,因为那总是正确的。
当你确实需要检查一个变量是否在两个数字之间时,方法是使用 23 < h && h < 542
.
另一个问题是,当您输入大于 9 的多个数字时,您并没有将 h
和 s
重置回它们在 while
循环之前的初始值更高的素数。您应该在循环之前初始化这些值,而不仅仅是在程序的顶部。
您还可以使用级联 if/else if/else,这样您就不需要在最后测试 x
(或者您可以使用 switch/case
)。
这里是完整的重写程序:
#include <stdio.h>
main() {
int n;
int x;
scanf("%d",&n);
while (n>0) {
n=n-1;
scanf("%d",&x);
if (x==1) {
printf("2\n");
} else if (x==2) {
printf ("3\n");
} else if (x==3) {
printf ("5\n");
} else if (x==4) {
printf("7\n");
} else if (x==5) {
printf("11\n");
} else if (x==6) {
printf("13\n");
} else if (x==7) {
printf("17\n");
} else if (x==8) {
printf("19\n");
} else if (x==9) {
printf("23\n");
} else {
int h = 24;
int s = 9;
while (h<542) {
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 ) {
s=s+1;
if (x==s) {
printf("%d\n",h);
}
}
}
}
}
}
#include <stdio.h>
main()
{
int n;
int x;
int h=24;
int s=9;
scanf("%d",&n);
while (n>0)
{
n=n-1;
scanf("%d",&x);
while (x<=9)
{
if (x==1)
printf("2\n");
if (x==2)
printf ("3\n");
if (x==3)
printf ("5\n");
if (x==4)
printf("7\n");
if (x==5)
printf("11\n");
if (x==6)
printf("13\n");
if (x==7)
printf("17\n");
if (x==8)
printf("19\n");
if (x==9)
printf("23\n");
break;
}
while (23<h<542 && x>9)
{
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 )
{
s=s+1;
if (x==s)
printf("%d\n",h);
}
}
}
}
代码的问题是输入n,这将是以下输入的数量。每个输入必须给出第 x 个素数。 示例:
输入: 3个 ,4 ,20 ,50 输出: 7 ,71 ,229.
x 可以在 1 到 100 之间。(第 100 个素数) 现在我的问题是 x>9.after 为它输入一个值,它将不再接受 x.
的值我想知道为什么会发生这种情况以及如何解决它。
我对编程也很陌生,还没有学过数组。(我知道 scanfs 不是最好的东西,但这就是我到目前为止所学的全部)
这一行:
while (23<h<542 && x > 9)
当 x
大于 9
时, 正在创建一个无限循环。 23 < h < 542
不测试 h
是否在 23
和 542
之间。该表达式等效于 (23 < h) < 542
。如果 23
小于 h
,(23 < h)
就是 1
,这种情况总是如此,因为 h
从 24
开始并且循环增加它。 1
总是小于 542
.
你想要的是:
if (x > 9) {
while (h < 542)
...
}
}
不需要每次循环都测试x
,因为它在循环中永远不会改变。并且不需要测试 23 < h
,因为那总是正确的。
当你确实需要检查一个变量是否在两个数字之间时,方法是使用 23 < h && h < 542
.
另一个问题是,当您输入大于 9 的多个数字时,您并没有将 h
和 s
重置回它们在 while
循环之前的初始值更高的素数。您应该在循环之前初始化这些值,而不仅仅是在程序的顶部。
您还可以使用级联 if/else if/else,这样您就不需要在最后测试 x
(或者您可以使用 switch/case
)。
这里是完整的重写程序:
#include <stdio.h>
main() {
int n;
int x;
scanf("%d",&n);
while (n>0) {
n=n-1;
scanf("%d",&x);
if (x==1) {
printf("2\n");
} else if (x==2) {
printf ("3\n");
} else if (x==3) {
printf ("5\n");
} else if (x==4) {
printf("7\n");
} else if (x==5) {
printf("11\n");
} else if (x==6) {
printf("13\n");
} else if (x==7) {
printf("17\n");
} else if (x==8) {
printf("19\n");
} else if (x==9) {
printf("23\n");
} else {
int h = 24;
int s = 9;
while (h<542) {
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 ) {
s=s+1;
if (x==s) {
printf("%d\n",h);
}
}
}
}
}
}