-
TwoDarray or loop
Hi ,
any one could help me on this ,i want it start from one until 30 thanks
Code:
import java.util.Scanner;
public class inte
{
public static void main(String[] args)
{
final int MAX_ROWW = 5;
final int MAX_STARR = 20;
int i,k;
for(i=1; i <= 30 ;i++)
{
for(k=1; k <= MAX_ROWW; k++)
{
System.out.print(String.format ("%5d",i++));
}
System.out.println();
}
}
}
the out come is as follow my question where is 6 ,10 ,
why ?
any help please
1 2 3 4 5
7 8 9 10 11
13 14 15 16 17
19 20 21 22 23
25 26 27 28 29
-
Code:
System.out.print(String.format ("%5d",i++));
You're incrementing the loop variable within the (nested) loop. Not a good thing to do as it can make the logic very hard to follow. Even worse in your case, it has led to an unwanted side effect. Every time the inner loop completes you are going to increment the outer loop variable twice - once in the code I have posted, and again in the outer loop control logic.
I think you're going to have to rethink your algorithm for doing this. There's no need to have more than one loop, you can do it with one loop and have a variable that counts and determines when a new line is needed.
-
here new code new problems
Hi, I did as i you give me some hints but still i have some problem , the out come start from 0 even i want to start from 1.
any help now please ?
thanks
Code:
import java.util.Scanner;
public class NestedPanels
{
public static void main(String[] args)
{
int[][] table = new int[20][5];
// Load the table with values
for (int row=0; row < table.length; row++)
for (int col=0; col < table[row].length; col++)
table[row][col] = row * 5 + col;
// Print the table
for (int row=0; row < table.length; row++)
{
for (int col=0; col < table[row].length; col++)
System.out.print (table[row][col] + "\t");
System.out.println();
}
}
}
out come
0 1 2 3 4
5 6 7 8 9
....
Similar Threads
-
By tc_lawabider in forum Java
Replies: 2
Last Post: 03-16-2006, 06:39 PM
-
By naazrael in forum Java
Replies: 2
Last Post: 12-14-2005, 10:22 AM
-
By draven2kg in forum VB Classic
Replies: 2
Last Post: 08-25-2005, 02:00 PM
-
By salvinger in forum VB Classic
Replies: 0
Last Post: 05-07-2005, 01:38 PM
-
By Mark in forum VB Classic
Replies: 1
Last Post: 03-23-2001, 03:41 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks