Answer to Question #23992 in Java | JSP | JSF for Myounghun Kang

Question #23992
Assume there is a triangle shape as..

(0,0)
(1,0) (1,1)
(2,0) (2,1) (2,2)
(3,0) (3,1) (3,2) (3,3)
(4,0) (4,1) (4,2) (4,3) (4,4)


So If I receive integers like
1 2 3

I want to store as 1 as

row[0] = 0;
col[0] = 0;

and 2 as


row[1] = 1;
col[1] = 0;

3 as follow.
row[2] = 1;
col[2] = 1;

public class Test {
public static void main(String[] args) {
int i, j;
for(i = 0; i < 5; i++) {
for(j = 0; j <= i; j++)
System.out.printf("(%d,%d)", i, j);
System.out.println();
}
}
}
1
Expert's answer
2013-02-11T10:38:34-0500
import java.util.Arrays;
import java.util.Scanner; public class Test {
/**
* @param args
*/
staticString[][] arr=new String[5][5];

publicstatic void main(String[] args)
{

for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr.length; j++)
{
arr[i][j]="";
}
}

Scanners=new Scanner(System.in);

while(s.hasNextInt())
{
intd=s.nextInt();
intii=0;
intjj=0;

while(--d>0)
{
if(ii==jj)
{
jj=0;
ii++;
}
else
{
jj++;
}

}

arr[ii][jj]="("+ii+","+jj+")";
}

for(int i = 0; i < arr.length; i++) {
System.out.println(Arrays.toString(arr[i]));
}

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS