Can someone help me please.. just starting java..:( How can I display all possible values based on the given minimum input value, maximum input value and the incrementing value?
for example: min value: 1 max value: 10 increment value: 2
the result will be: 1, 3, 5, 7, 9
this is what i got so far..
public class DisplayIncrement {
public static void main(String []args){
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
in.nextInt();
System.out.println("Enter max value: ");
in.nextInt();
System.out.println("Enter increment value: ");
in.nextInt();
int i;
for(i=0; i<=10; i+=2){
System.out.println(i);
}
}
}
Some Notes:
1- in.nextInt();
reads an integer from the user, blocks until the user enters an integer into the console and presses ENTER. The result integer has to be saved in order to use it later on, and to do so save it into a variable, something like this:
int value = in.nextInt();
In your code, you need to assign the 3 integers that the user enters to the corresponding variables:
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
2- You are implementing the loop very well, but you just need to use the user's inputs rather than using explicit integers:
for(int i = min; i <= max; i += increment)
{
System.out.println(i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With