Results 1 to 3 of 3
Thread: Nested Loops Help!
- 03-15-2012, 10:49 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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();
}
}
}
- 03-16-2012, 12:07 AM #2
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
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:
My version: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(); } } }
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.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(); } } }Last edited by javanubby; 03-16-2012 at 12:09 AM.
- 03-16-2012, 04:20 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
Nested Loops
By joemama in forum New To JavaReplies: 1Last Post: 01-01-2011, 09:17 PM -
Nested Loops
By candygirl198827 in forum New To JavaReplies: 38Last Post: 12-01-2010, 06:03 AM -
Nested for loops
By luke in forum New To JavaReplies: 23Last Post: 10-21-2010, 02:49 AM -
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02-18-2009, 02:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks