Java For Loop with Array in columns -
this question has answer here:
- how print columns in java txt file 4 answers
i want make code output loops columns not straight , down in 1 column, each int in each of 3 loops make column starting left right.
import java.util.scanner; public class project4 { public static void main(string [] args) { final int number_months = 12; int [] avgtemp = {46, 48, 49, 50, 51, 53, 54, 55, 56, 55, 51, 47}; int [] avgrain = {5, 3, 3, 1, 1, 0, 0, 0, 0, 1, 3, 4}; int [] newgrowth; newgrowth = new int[number_months]; scanner scan = new scanner(system.in); system.out.print("enter minimum temperature plant : "); int min_plant_temp = scan.nextint(); system.out.print("enter maximum temperature plant : "); int max_plant_temp = scan.nextint(); system.out.print("enter minimum rainfall plant : "); int min_rainfall_for_plant = scan.nextint(); system.out.println("month" + " " + "temp" + " " + "rain" + " " + "growth" + " " + "plant height"); (int j=0; j<number_months; j++) { system.out.println(j);} (int = 0; < avgtemp.length; i++) { system.out.println(avgtemp[i]); } (int k = 0; k < avgrain.length; k++) { system.out.println(avgrain[k]); } { } system.out.println(); } }
i want make above code print out info in columns under month temp rain etc....but outputs top bottom in straight line instead of in columns.
you should use system.out.print() instead of system.out.println(); println() print next line while print() print right instead of next line. , should works magic. note: remember add spaces each loops doesn't stack together.example system.out.print(j+" ");
note: think trying achieve this
system.out.println("month" + " " + "temp" + " " + "rain" + " " + "growth" + " " + "plant height"); (int j=0; j<number_months; j++) { system.out.print(j+1 +" "); //since there no 0 month. therefore add 1 j. system.out.print(avgtemp[j] + " "); system.out.print(avgrain[j] + " "); //system.out.print(newgrowth + " "); //after determine is, variable not set yet. //system.out.print(plantheight + " "); //after determine is, variable not defined yet. system.out.println(); //after rows print, print set next line. }
Comments
Post a Comment