當用戶按Y或Y時應該停止該程序,但是測試測試條件對代碼沒有影響,程序不斷詢問用戶下一個數字。爲什麼測試條件沒有影響
#include<iostream.h>
#include<conio.h>
#include<math.h>
int prime(int);
void main(void)
{
int n;
char ch='i';
mylabel:
cout << "\nEnter num: ";
cin >> n;
cout << prime(n);
cout << "\nPress N or n to exit : ";
if (getche() != ('N' || 'n')) {
goto mylabel;
}
cout << ",,,";
}
int prime(int p)
{
int test = 1;
if((p>2 && p%2==0) || p==0 || p==1 || p<0)
return 0;
for (int i=3 ; i<=(int(sqrt(p)))+1 ;i+=2)
{
if(p%i==0)
test=0;
break;
}
return test;
}
爲什麼downvote? – Illaz 2015-03-13 13:46:30
因爲這是一個愚蠢的問題,未來不會幫助任何人。你在哪裏讀到'if(getche()!=('N'||'n'))'是合併條件的正確方法?當然不是你的C++書。 – 2015-03-13 13:47:49
@LightnessRacesinOrbit什麼是正確的方法? – Illaz 2015-03-13 13:49:32