java - method that accepts two integers, and prints the sequence of numbers between them -


i beginner of java , appreciate if can me program. working on now, , believe need use scanner, int, loop, , if/else statement. description of program below:

write method called printrange accepts 2 integers arguments , prints sequence of numbers between 2 arguments, enclosed in square brackets. print increasing sequence if first argument smaller second; otherwise, print decreasing sequence. if 2 numbers same, number should printed between square brackets. here sample calls printrange:

printrange(2, 7);

printrange(19, 11);

printrange(5, 5);

the output produced should following:

[2, 3, 4, 5, 6, 7]

[19, 18, 17, 16, 15, 14, 13, 12, 11]

[5]

i have written of it, , don't know in printrange method. believe need int can make loops work. don't know how make numbers in [] , , space. helpful if code work...

import java.util.*; // scanner

public class printrange

{ //begin class printrange

public static void main(string[] args)    { //begin main method      scanner console = new scanner(system.in);      system.out.println("this program prints sequence of numbers between 2 numbers give");      //obtain values     system.out.println("enter 2 numbers (x,y)");     system.out.print("number x: ");     int x = console.nextint();     system.out.print("number y: ");     int y = console.nextint();            int sequence;     sequence = printrange(x,y);      system.out.println("the sequence of number " + sequence);    } //end main method  public static int printrange(int x, int y)   { //begin printrange method        //this method accepts 2 parameters , return integer int      if (x > y)     { //begin if statement       //x larger y         (int = x; <= y; i++)         {//begin loop         system.out.print("[" + + "]");         range = range + i;         }//end loop      } //end if method      else if (x < y)     { //begin else if method       //x smaller y         (int j = x; j >= y; j--)         {//begin loop j         system.out.print("[" + j + "]");         range = range + j;         }//end loop j     } //end else if method      else if (x == y)     { //begin else if method       //x equal y         system.out.print(x);         range = range + x;     } //end else if method return range;   } //end printrange method 

} //end class printrange

thanks in advance!

since you're learning this, won't code answer you. i'll give hints.

1.import java.util.scanner if haven't already.

2.for loop is: for(int i= 0;i<parameter.length();i++)

3.look .substring() here: http://www.tutorialspoint.com/java/java_string_substring.htm

4.scanner works this:

system.out.println("here put ask user input"); int input = input.nextint(); //this stores user's input in var input 

5.good luck! suggest run for loop , use substring on counter i.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -