Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How do I do this program? I'm new to Java programming...

Have Programming Question? Ask it here!
 
 
 


JavaBeginner
How do I do this program? I'm new to Java programming...
2 Answer(s)      3 years and 6 months ago
Posted in : Java Beginners


Suppose you want to print out numbers in brackets, formatted as follows: [1] [2] [3] and so on. Write a method that takes two parameters: howMany and lineLength. The method should printout line numbers from 1 to howMany in the previous format, but it should not output more than lineLength characters on any one line.


Advertisement
View Answers

January 27, 2011 at 11:33 AM


Hi Friend,

Try this:

import java.util.*;
class NumberExample1
{
    public static void main(String[] args) 
    {
        int count=0;
        Scanner input=new Scanner(System.in);
        System.out.print("How many numbers do you want to enter: ");
        int nums=input.nextInt();
        System.out.print("Length of Line: ");
        int len=input.nextInt();
        for(int i=1;i<=nums;i++){
            if(count==len){
                System.out.print("\n");
            }
            System.out.print("["+i+"]"+" ");
       count++;
        }
    }
}

Thanks



January 27, 2011 at 6:40 PM


Thanks so much! I have 1 doubt, the question says it should not output more than lineLength "CHARACTERS" on any one line. That means it counts the entire [1] [2] [3] as a character. Any help with this?



Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com