Results 1 to 3 of 3
  1. #1
    spendogw is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Nested Loops Help!

    Hello, this is my first time posting so sorry if I forget to include something. I am having trouble figuring out how to print the numbers 1-100 on ten separate lines using nested for loops. Also, when printing the same numbers using a different method I cant figure out why the last number on each line prints twice. These are exercises for a test in my comp sci class so thank you for any help!

    Here is what I have so far.

    import java.util.Scanner;
    public class Numbers
    {
    private static void method1()
    {
    int number, counter;
    number=1;counter=1;
    while(counter<=10&&number<100){
    System.out.print(number+". ");
    number++;counter++;
    if(counter==10){
    counter=1;
    System.out.println(number+". ");
    }
    }
    }
    private static void method2()
    {
    int i;
    for(i=1;i<=10;i++)
    {
    System.out.print(i+". ");
    //I can print 1-10, or even 1-100, but I can't figure out how to break the numbers up into 10 separate lines of 10 numbers.
    }
    }
    public static void main(String args[])
    {
    Scanner scanner = new Scanner(System.in);
    String methodpick;
    System.out.println("Method 1 or Method 2?");
    methodpick = scanner.nextLine();
    if(methodpick.equalsIgnoreCase("1")){
    Numbers.method1();
    }
    if(methodpick.equalsIgnoreCase("2")){
    Numbers.method2();
    }
    }
    }

  2. #2
    javanubby is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Nested Loops Help!

    Use [CODE] next time, code was an eyesore. anyways I put up my edited version as well as comments regarding them.
    Your code:
    Java Code:
    import java.util.Scanner;
    public class Numbers
    {
        private static void method1()
        {          
            int number, counter;
            number=1;counter=1;
            while(counter<=10&&number<100){
                System.out.print(number+". ");
                number++;counter++;
                if(counter==10){
                    counter=1;
                    System.out.println(number+". ");
                }
            }
        }
        private static void method2()
        {
            int i;
            for(i=1;i<=10;i++)
            {
                System.out.print(i+". ");
                //I can print 1-10, or even 1-100, but I can't figure out how to break the numbers up into 10 separate lines of 10 numbers.
            }
        }
        public static void main(String args[])
        {
            Scanner scanner = new Scanner(System.in);
            String methodpick;
            System.out.println("Method 1 or Method 2?");
            methodpick = scanner.nextLine();
            if(methodpick.equalsIgnoreCase("1")){
                Numbers.method1();
            }
            if(methodpick.equalsIgnoreCase("2")){
                Numbers.method2();
            }
        }
    }
    My version:
    Java Code:
    import java.util.Scanner;
    public class Numbers
    {
        private static void method1()
        { 
            int number = 1;
            int counter = 1; //more reader-friendly
            while(counter <= 10 && number <= 100){ //you had <100 but since you wanted to include 100, you need to have <=
                System.out.print(number+". ");
                number++;
                counter++;
                if(counter==11){ //following your prev logic, counter would enter the WHILE loop as 9, be bumped up by the
                    counter=1; // counter ++ in the upper line to 10, and be resetted to 1 w/o printing 10. simply change if
                    System.out.print("\n"); // loop to counter == 11 would allow a full numbering of 1-10.
                } //"\n" simply means enter in java, meaning after 10 is printed, and your counter is resetted to 1, 
            } //11 will start on a new line.
        }
        private static void method2()
        {
            for(int a = 1; a <= 10; a++) //as for your nested for loops, after few trial and error, i have no idea how that is going to work.
                System.out.print(a+". "); //not that I know off. My only suggestion for your method2 is to make 10 separate for loops
            System.out.print("\n");
            for(int a = 11; a <= 20; a++)
                System.out.print(a+". ");
            System.out.print("\n");
            for(int a = 21; a <= 30; a++)
                System.out.print(a+". ");
            //etcetera, etcetera ...
        }
        public static void main(String args[])
            {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Method 1 or Method 2?");
            String methodpick = scanner.nextLine(); //just simply combined your extraneous string methodpick; into this line 
            if(methodpick.equalsIgnoreCase("1")){
                Numbers.method1();
            }
            if(methodpick.equalsIgnoreCase("2")){
                Numbers.method2();
            }
        }
    }
    java can be frustrating, but try to work the logic out yourself. i hated it when i first took it and still get confused by things.
    Last edited by javanubby; 03-16-2012 at 12:09 AM.

  3. #3
    spendogw is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Nested Loops Help!

    Thank you so much, it makes sooo much more sense now hahaha.

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. Nested Loops
    By joemama in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 09:17 PM
  3. Nested Loops
    By candygirl198827 in forum New To Java
    Replies: 38
    Last Post: 12-01-2010, 06:03 AM
  4. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 02:49 AM
  5. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •