我想让代码将for循环输出到列中,而不仅仅是在一列中直接向上和向下输出,但是三个for循环中的每一个中的每个int都会使列从左到右。
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 for plant : ");
int min_plant_temp = scan.nextInt();
System.out.print("Enter maximum temperature for plant : ");
int max_plant_temp = scan.nextInt();
System.out.print("Enter minimum rainfall for plant : ");
int min_rainfall_for_plant = scan.nextInt();
System.out.println("Month" + " " + "Temp" + " " + "Rain" + " " + "Growth" + " " + "Plant Height");
for (int j=0; j<number_months; j++) {
System.out.println(j);}
for (int i = 0; i < avgTemp.length; i++) {
System.out.println(avgTemp[i]);
}
for (int k = 0; k < avgRain.length; k++) {
System.out.println(avgRain[k]);
}
{
}
System.out.println();
}
}
我想让上面的代码打印出月份临时降雨等列中的信息....但是它从上到下以直线而不是列的形式输出。
答案 0 :(得分:0)
您应该使用System.out.print()而不是System.out.println(); println()会打印到下一行,而print()会将其打印到右边而不是下一行。 它应该像魔术一样工作。 注意:记得添加空格,以便每个循环不会堆叠在一起。示例System.out.print(j +&#34;&#34;);
注意:我认为你想要实现的是这样的
System.out.println("Month" + " " + "Temp" + " " + "Rain" + " " + "Growth" + " " + "Plant Height");
for (int j=0; j<number_months; j++) {
System.out.print(j+1 +" "); //since there is no 0 month. therefore you add 1 into j.
System.out.print(avgTemp[j] + " ");
System.out.print(avgRain[j] + " ");
//System.out.print(newGrowth + " "); //after you determine what it is, this variable is not set yet.
//System.out.print(plantHeight + " "); //after you determine what it is, this variable is not defined yet.
System.out.println(); //after all rows are print, print this to set to the next line.
}