Reputation: 23
i'm new in coding and tried to do some open university tasks. Maybe you guys can give some helping hand. I really even don't know how to start this task, witch is divide three Integers from main to method. Example 2, 10 where I should print 3, 6, 9 or 2, 6 where I should print 3, 6.
import java.util.Scanner;
public class divideByThree {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
divideByThree(2, 10);
}
public static void divideByThree(int start, int end) {
while(true) {
if (start % 3 == 0) {
start++;
}
if (end % 3 == 0) {
System.out.println(end);
}
System.out.println(start);
}
}
}
Upvotes: 0
Views: 858
Reputation: 5
I will make some small changes in your code only so that you will be able to understand quickly.
public static void divideByThree(int start, int end) {
while(start! =end) {
if (start % 3 == 0) {
System.out.println(start);
start++;
}
}
}
Upvotes: 0
Reputation: 79095
while(true)
because it creates an infinite loop which will unnecessarily make your program complex.start
by 1
in each iteration and terminate the loop when the value of start
value goes beyond end
. start % 3 == 0
becomes true, print start
. Given below is a sample code which you can use to understand the points mentioned above.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the start number: ");
int start = reader.nextInt();
System.out.print("Enter the end number: ");
int end = reader.nextInt();
divideByThree(start, end);
}
public static void divideByThree(int start, int end) {
while (start <= end) {
if (start % 3 == 0) {
System.out.println(start);
}
start++;
}
}
}
A sample run:
Enter the start number: 3
Enter the end number: 9
3
6
9
Upvotes: 0
Reputation: 233
The while(true)
is an endless loop (something you would want to avoid). It will only stop if you add a break
statement somewhere inside it, which you do not have.
I assume this is the issue you need to solve. The key to solving it is simply making sure that your loop stops at some point. How do you do that?
True
is a boolean value. a while
loop runs again and again for as long as the specified condition remains true, and in your case - it is always true. What you need to do is replace while(true)
with while(some condition that will be false at some point)
. In your specific case, while(start <= end)
seems appropriate - just make sure to increment start
at every iteration.
Another way of doing it would be using a for
loop, i.e:
for (int i = start; i <= end; i++) {
// your code
}
This type of loop takes care of stating the conditions for which the loop should keep on running, and also takes care of incrementing the index i
to make sure the loop stops running at some point.
Upvotes: 0
Reputation: 684
Recursive Approach
import java.util.Scanner;
public class DivideByThree {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int start = reader.nextInt();
int end = reader.nextInt();
divideByThree(start, end);
reader.close();
}
public static void divideByThree(int start, int end) {
if(start % 3 == 0) {
System.out.println(start);
}
if( start <= end) {
divideByThree(++start, end);
}
}
}
Upvotes: 0
Reputation: 132
How about the following implementation about divideByThree method?
public static void divideByThree(int start, int end) {
for(int i = start; i <= end; ++i){
if(i%3==0){
System.out.print(i+" ");
}
}
}
Upvotes: 1