我有一個簡單的c程序,需要兩個整數,給出平均值,並檢查它們是否爲素數。當我運行它並提供用戶輸入時,程序會停止運行,直到遇到esc時它纔會執行任何操作。一旦我打開esc,它會給我正確的結果。這是服務器還是我的代碼的問題?程序陷入癱瘓
這裏是我的代碼:
#include <stdio.h>
2 #include "prime.h"
3
4 int main()
5 {
6 int x, y;
7 float avg;
8
9 /* Get integers x and y from user */
10 printf("Please enter two positive integers\n");
11 scanf("%d %d", &x, &y);
12 scanf("%d", &x, &y);
13 printf("You typed in %d and %d\n", x, y);
14 avg = (x+y)/2;
15 printf("The average is: %f\n", avg);
16
17 /* Test if x and y are prime */
18 if(prime(x) == 1)
19 printf("%d is prime\n", x);
20 else
21 printf("%d is not prime\n", x);
22 if(prime(y) == 1)
23 printf("%d is prime\n", y);
24 else
25 printf("%d is not prime\n", y);
26
27 return 0;
28 }
prime.c:
1 #include "prime.h"
2
3 int prime(int x)
4 {
5 int isprime;
6 int i;
7
8 for(i = sqrt(x); i > 1; i--)
9 {
10 if(x % i == 0)
11
12 /* isprime is false*/
13 isprime = 0;
14 else{
15
16 /* isprime is true*/
17 isprime = 1;
18 }
19 }
20 return isprime;
21 }
〜
謝謝!!
線12條,'的scanf( 「%d」,&X,&y);'是不必要的和不正確的。 –
謝謝!問題解決了。 – user3000731