Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- =====================================================
- chapter 5: Arrays
- Ex12: Star Graph
- =====================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- final int SIZE=4;
- int arr[]=new int[SIZE];
- int max=0; //represent max value in array
- Scanner s=new Scanner(System.in);
- System.out.println("Enter "+SIZE+" numbers: ");
- //user input → init array, init max value
- for(int i=0;i<arr.length;i++){
- arr[i]=s.nextInt();
- if(arr[i]>max)
- max=arr[i];
- }
- //display Star Graph
- for(int row=0;row<max;row++){
- for(int col=0;col<arr.length;col++) {
- if (max - arr[col] > row)
- System.out.print(" ");
- else
- System.out.print(" * ");
- }
- System.out.println();
- }
- //aesthetic gap
- System.out.println("------------");
- //display array
- for(int i=0;i<arr.length;i++){
- System.out.print(" "+arr[i]+" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment