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