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

 
 


JavaBeginner
How do I do this program? I'm new to Java programming...
2 Answer(s)      2 years and 4 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.

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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.