import java.util.Scanner;
public class Assignment5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a;
System.out.println("Please enter a number between 1 and 9:");
int[] graph = new int[10];
for(int i = 0; i < 10; ++i){
a = scan.nextInt();
graph[i] = a;
}
}
// this is the method I'm required to use for
// this array assignment
public static void printVertical(int[] graph){
for(int i = 0; i < graph.length; i++){
for(int j = 0; j < graph[i]; j++){
System.out.print(" " + graph[i]);
}
System.out.println();
}
}
// this is what I've got for the horizontal method
// I took the for loop and nested for loop from the vertical.
// I'm stuck on changing the variable to flip the graph -90 degrees
public static void printHorizontal(int[] graph){
for(int i = 0; i < graph.length; i++){
for(int h = 0; h < graph[i]; h++){
System.out.print(" " + graph[i]);
}
System.out.println();
}
}
public static int calculateTotal(int[] graph){
int sum = 0;
for( int num : graph ){
sum = sum + num;
}
System.out.print("The total is: " + sum);
return sum;
}
}
我在方法中的 for 循环printVertical
没有正确返回图表。
我究竟做错了什么?
输出需要如下所示:
Sample output:
2 4 6 8 9 8 6 4 3 2
---- Vertical Graph ----
2 2
4 4 4 4
6 6 6 6 6 6
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
6 6 6 6 6 6
4 4 4 4
3 3 3
2 2
---- Horizontal Graph ----
9
8 9 8
8 9 8
6 8 9 8 6
6 8 9 8 6
4 6 8 9 8 6 4
4 6 8 9 8 6 4
2 4 6 8 9 8 6 4 2
The total is: 52
2 4 6 8 9 8 6 4 2