-2

I have the current code used to complete one of the Project Euler tasks:

 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);
   }
}

Now my question is how can I get the whole program to break once the first value (232792560) is found from the method. My current break at the if(y == 10){ break;} does not do this.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
  • does the `print` happen, does your code even reach the `break` statement? – luk2302 May 16 '17 at 15:13
  • 1
    You cannot break the outer loop if it is in another method. Given that you have 3 lines long main method, just inline mod method. Then you can label outer loop `outer: for(x = 0; x < Integer.MAX_VALUE; x++){` and change your `break;` to `break outer;`. – Jaroslaw Pawlak May 16 '17 at 15:14
  • Possible duplicate of [Breaking out of nested loops in Java](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java) – Jaroslaw Pawlak May 16 '17 at 15:16
  • what is the for loop which is inside main function – Basil Battikhi May 16 '17 at 15:17
  • you can use `System.exit(0);` if you want to terminate your entire program – Bill F May 16 '17 at 15:29
  • @luk2302 yes, the value is printed however the break is skipped and other values past the first continue to print. – Harry Orson May 16 '17 at 17:45
  • @BillF I did try this, however, I use a few other lines of code after to tell me how long the program takes to execute. Using system.exit closes the program without those lines of code running. – Harry Orson May 16 '17 at 17:58
  • @HarryOrson if you have code to execute after your loops, please see my answer. You would have to have your method return a boolean. – Bill F May 16 '17 at 18:40

2 Answers2

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;
}

}
     }
}
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
  • This would be useful, however, the value of 232792560 is an unknown. The code is there to find that value therefore that value wouldn't exist when writing the code. My fault, the further context was needed. – Harry Orson May 16 '17 at 17:43
  • Accept the answer if this is help you to help others – Basil Battikhi May 17 '17 at 03:12
-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;
       }
     }
   }
 }
storm87
  • 49
  • 1
  • 5
  • I'm sorry, i did not understand correctly your first question. I think that @BillF approach of returning a boolean is the correct one – storm87 May 17 '17 at 08:02