好的,所以我已经在保留功能的同时简化/压缩了它,但我只做了一个半月的 C++。有 100 行代码。是否可以在函数争论中声明变量,然后在没有的情况下调用它们将值传递给arguemnets?
#include <iostream>
#include <windows.h>
using namespace std;
int primeCheck10 (int j)
{
int count=0;
cout<<"Enter a number between 1 and 10___";
cin>>j;
if(j<1 ||j>10)
{
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int primeCheck100(int j)
{
int count=0;
cout<<"Enter a number between 1 and 100___";
cin>>j;
if(j<1 || j>100){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int checkPrime1000(int j)
{
int count=0;
cout<<"Enter a number between 1 and 1000___";
cin>>j;
if(j<1 || j>1000){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int main ()
{
system("pause");
return 0;
}