Friday 15 March 2013

Issue with Endless nested for-loops in do-while loop in java -



Issue with Endless nested for-loops in do-while loop in java -

i coding lab high school comp. sci. class. objective of programme read info file, 1 integer @ time , print inverted triangle.

the info txt file with: 3 3 7 4 (in file info arranged vertically 3 @ top, can't format here reason)

i changed .txt extension .dat

it should print inverted triangle top row beingness 3 , each successive row having 1 less asterisk (again, cant figure out how format this. tried , didnt work). if int 3 top row 3, if int 7 top row 7 etc.

i getting stuck in nested for-loop somehow though have decrement operator should loop terminate, not. see total weirdness.

public class triangle2_pr31 { public static void main(string[] args) throws ioexception { scanner triscan = new scanner(new file("pr31.dat"));//reads file int = triscan.nextint(); system.out.println(a);//prints number verification int b;//instantiate { //a = triscan.nextint(); for(int x = 1; x<=a; x--)//main for-loop { for(b = a; b>=0; b--)//nested for-loop print asterisks { system.out.print("*"); system.out.println(b);//print see happened b } system.out.println(b);//print see if loops gets out here a--;//decrease loop terminates //doesnt out here } } while(triscan.hasnext()); triscan.close(); } }

when compiler gets thesop(b); line in nested for-loop, thing goes way negative 50,000 before manually terminate program, allow go -200,000 1 time fun. not understand why not terminate. stared @ eclipse 10 minutes trying sorts of different stuff cant figure out wrong.

your main for-loop decreasing x @ same rate decreased, x never grows larger causing endless for-loop.

java for-loop logic do-while

No comments:

Post a Comment