我有用于完成Project Euler任务之一的当前代码:
public static void mod(int value){
int[] modValues = {11,12,13,14,15,16,17,18,19,20};
int y = 0;
for(int x = 0; x < 10; x++){
int originalValue = value;
int modSum = value % modValues[x];
if(modSum == 0){
y += 1;
if(y == 10){
System.out.println(originalValue);
break;
}
}
}
}
public static void main(String[] args) {
final long startTime = System.nanoTime(); //<<<ignore//
int x;
for(x = 0; x < Integer.MAX_VALUE; x++){
mod(x);
}
}
现在我的问题是,一旦从方法中找到第一个值(232792560),我怎么能让整个程序中断。我目前在if(y == 10){ break;}
的休息时间没有这样做。
答案 0 :(得分:0)
public static int mod(int value){
int[] modValues = {11,12,13,14,15,16,17,18,19,20};
int y = 0;
for(int x = 0; x < 10; x++){
int originalValue = value;
int modSum = value % modValues[x];
if(modSum == 0){
y += 1;
if(y == 10){
System.out.println(originalValue);
return originalValue;
}
}
}
}
public static void main(String[] args) {
final long startTime = System.nanoTime(); //<<<ignore//
int x;
for(x = 0; x < Integer.MAX_VALUE; x++){
if(mod(x)==232792560){
// now it will break so just break it
break;
}
}
}
}
答案 1 :(得分:-1)
public static void mod(int value)
{
int[] modValues = {11,12,13,14,15,16,17,18,19,20};
int y = 0;
for(int x = 0; x < 10 && y != 10; x++)
{
int originalValue = value;
int modSum = value % modValues[x];
if(modSum == 0)
{
y += 1;
if(y == 10)
{
System.out.println(originalValue);
break;
}
}
}
}