C++ 什么';是什么让我的素数都消失了?[C+;+;]
我已经做了一个算法,理论上,应该显示一个范围内的每个数字,可以由人工输入。它的大脑是这样编程的C++ 什么';是什么让我的素数都消失了?[C+;+;],c++,C++,我已经做了一个算法,理论上,应该显示一个范围内的每个数字,可以由人工输入。它的大脑是这样编程的 //Prime Number Displayer #include <iostream> using namespace std; int main(){ int Low, High, check; cout<<"Enter positive integer."<<endl; cin>>Low; cout<<"Enter a seco
//Prime Number Displayer
#include <iostream>
using namespace std;
int main(){
int Low, High, check;
cout<<"Enter positive integer."<<endl;
cin>>Low;
cout<<"Enter a second positive integer."<<endl;
cin>>High;
cout<<"You will get a set of all prime numbers between "<<Low<<" and "<<High<<"."<<endl;
check=(Low%2);
while (Low <=High){
if(check==1){
cout<<""<<Low<<""<<endl;
check++;
}
}
return 0;
}
//素数显示器
#包括
使用名称空间std;
int main(){
int低,高,检查;
cout这样的代码将在用户给定的范围内找到素数:
bool isPrime(int n)
{
if (n <= 1)
return false;
if (n%2 == 0 && n != 2)
return false;
for (int i = 3; i < n; i+=2)
if (n % i == 0)
return false;
return true;
}
int main()
{
int Low, High;
bool isPrimeNum;
cout<<"Enter positive integer."<<endl;
cin>>Low;
cout<<"Enter a second positive integer."<<endl;
cin>>High;
cout<<"You will get a set of all prime numbers between "<<Low<<" and "<<High<<"."<<endl;
while (Low <= High)
{
isPrimeNum = isPrime(Low);
if (isPrimeNum)
cout<<endl<<Low<<" is prime number.";
Low++;
}
}
bool isPrime(int n)
{
if(n1)您只检查Low
是否除以2
,而不检查范围内的任何数字是否为素数。2)如果Low
不除以2
,则输出它,并不断增加check
,这意味着除了无限循环外,它将永远不会再次等于1
。3)因此您与king为你编写整个程序?自从“修复”4)考虑从C++中学习C++,而不是随机编码。你应该学习如何格式化代码,我认为这是循环的,因为循环总是低的那么高,因为低值在while循环中没有改变/增加。这看起来是另一个家庭作业/教程问题。我建议以下是在S.O.上询问类似的基本问题之前,请先阅读教程。在发布代码之前,您是否测试了代码?循环
变量将包含2
、4
、6
、8
、10
,…因此,您的代码将声明9
是一个素数。是的,我注意到了。我会更正它还有一段时间,现在不在系统上