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

}

3

8 Answers

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); } 
 System.out.println("Enter min value: "); int minVal = in.nextInt(); System.out.println("Enter max value: "); int maxVal = in.nextInt(); System.out.println("Enter increment value: "); int increment = in.nextInt(); for(i=minVal; i<=maxVal; i+=incremement){ System.out.println(i); } 

Well first of all, you declared the integers needed, however, you did not actually use them.

So, when you are calling the method in.nextInt();, you are actually "getting" an integer. So you can set the integers min, max, and increment to that method respectably. like this,

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

So for the complete answer, something like this should work.

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: "); min = in.nextInt(); System.out.println("Enter max value: "); max = in.nextInt(); System.out.println("Enter increment value: "); increment = in.nextInt(); for(int i=min; i<=max; i+=increment){ System.out.println(i); } } 
 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: "); int min=in.nextInt(); System.out.println("Enter max value: "); int max=in.nextInt(); System.out.println("Enter increment value: "); int inc=in.nextInt(); int i; for(i=min; i<max; i+=inc){ System.out.println(i); } } } 

The flaws in your code are obvious but...

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: "); min = in.nextInt(); System.out.println("Enter max value: "); max = in.nextInt(); System.out.println("Enter increment value: "); increment = in.nextInt(); for(int i = min; i <= max; i += increment) System.out.println(i); } } 
 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: "); min = in.nextInt(); System.out.println("Enter max value: "); max = in.nextInt(); System.out.println("Enter increment value: "); increment = in.nextInt(); for(; min<=max; min+=increment ){ System.out.println(min); } } } 
public static void main(String []args){ int min, max, increment; Scanner in = new Scanner(System.in); System.out.println("Enter min value: "); int minval=in.nextInt(); System.out.println("Enter max value: "); int maxval=in.nextInt(); System.out.println("Enter increment value: "); int incr=in.nextInt(); int i; for(i=minval; i<=maxval;i+=incr){ System.out.println(i); } 

}

Dont forget to close the Scanner at the end.

import java.util.Scanner; 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: "); min = in.nextInt(); System.out.println("Enter max value: "); max = in.nextInt(); System.out.println("Enter increment value: "); increment = in.nextInt(); //for(initial value; condition; increment/decrement) for(int i=min; i <=max; i+= increment){ System.out.println(i); } in.close(); } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy