# 找到最高质数的Java程序

##### 问题描述投票：0回答：1

[尝试编写一个接受2个正整数并返回它们之间的最高质数的javascript函数，否则，返回适当的错误消息。如果输入数字是50和75，则输出应该是73；如果输入数字是-50，-75，则输出应该是错误消息。

``````function highestPrimeInRange(int1, int2) {
if (int1 <= 0 || int2 <= 0) {
alert("Both numbers should be positive and non 0");
}
let highestPrime;

for (let i = int1; i <= int2; i++) {
let primeFlag = true;
for (let j = 2; j <= i / 2; j++) {
if (i % j == 0) {
primeFlag = false;
break;
}
if (primeFlag) {
highestPrime = i;
}
}
}
}
highestPrimeInRange(50, 75);``````

javascript
##### 1个回答
1

``````function highestPrimeInRange(int1, int2) {
if (int1 <= 0 || int2 <= 0) {
alert("Both numbers should be positive and non 0");
return;
}
if (int1 >= int2) {
alert("First number should be less than second");
return;
}
let highestPrime;

for (let i = int1; i <= int2; i++) {
let primeFlag = true;
for (let j = 2; j <= i / 2; j++) {
if (i % j == 0) {
primeFlag = false;
break;
}
}
if (primeFlag) {
highestPrime = i;
}
}
}
highestPrimeInRange(50, 75);
highestPrimeInRange(-50, -75);
highestPrimeInRange(10, 100);``````

``````function highestPrimeInRange(int1, int2) {
if (int1 <= 0 || int2 <= 0) {
alert("Both numbers should be positive and non 0");
return;
}
if (int1 >= int2) {
alert("First number should be less than second");
return;
}

for (let i = int2; i >= int1; i--) {
let primeFlag = true;
for (let j = 2; j <= i / 2; j++) {
if (i % j == 0) {
primeFlag = false;
break;
}
}
if (primeFlag) {