Home > front end >  How to create Java histograms without arrays, strings and any other kind of collections
How to create Java histograms without arrays, strings and any other kind of collections

Time:03-31

How I can write a program which reads from the user three non-negative integers and then prints, using symbols ’*’, a ‘histogram’ of the data represented by the numbers, i.e., three vertical bars, aligned at the bottom, with heights equal to the values of the three numbers. It has to be without arrays, strings and any other kind of collections.

== So thanks for everyone and now I`m introducing you the final result, which is works correctly

For example, for numbers 3, 1 and 8 the result should look like this:

  *
  *
  *
  *
  *
* *
* *
***

Everything what I have now:

But it shows everywhere 1 more "*" and I wanna know how to improve this code

package com.company;

import java.util.Scanner;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("input first number");
        int a = sc.nextInt();
        System.out.println("input second number");
        int b = sc.nextInt();
        System.out.println("input third number");
        int c = sc.nextInt();
        int max = Math.max(Math.max(a, b), c);
        for(int i=max; i>=0 ; i--) {
            System.out.print((i<a) ? "* " : "  ");
            System.out.print((i<b) ? "* " : "  ");
            System.out.println((i<c) ? "* " : "  ");



}

CodePudding user response:

Your solution was already very close. Just change:

for(int i=max; i>=0 ; i--)

To:

for (int i=max; i > 0; i--)

CodePudding user response:

Try the below code.

    public static void printHistogram(int a, int b, int c) {
        int arr[] = {a, b, c}, max = Math.max(arr[0], Math.max(arr[1], arr[2]));
        for (int i = 1; i <= max; i  ) {
            for (int num : arr) {
                System.out.print(max - i < num ? 'x' : ' ');
            }
            System.out.println();
        }
    }

Now if you call the above method like so

        printHistogram(8, 1, 3);
        System.out.println("--------------------");
        printHistogram(1, 3, 8);
        System.out.println("--------------------");
        printHistogram(8, 3, 1);
        System.out.println("--------------------");

You will get the below result

x  
x  
x  
x  
x  
x x
x x
xxx
--------------------
  x
  x
  x
  x
  x
 xx
 xx
xxx
--------------------
x  
x  
x  
x  
x  
xx 
xx 
xxx
--------------------

Note: If you pass 8,1,3 to this method, then 8 will be the first column and 3 will be the last column. If you want it reversed, just reverse the inner for loop.

Update: If you don't want any usage of arrays, try this code below

    public static void printHistogram(int a, int b, int c) {
        int max = Math.max(a, Math.max(b, c));
        for (int i = 1; i <= max; i  ) {
            System.out.print(max - i < a ? "x " : "  ");
            System.out.print(max - i < b ? "x " : "  ");
            System.out.print(max - i < c ? "x " : "  ");
            System.out.println();
        }
    }

CodePudding user response:

Try this. As simple and as clean as possible.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("input first number");
    int a = sc.nextInt();
    System.out.println("input second number");
    int b = sc.nextInt();
    System.out.println("input third number");
    int c = sc.nextInt();
    int max = Math.max(Math.max(a, b), c);
    for (int i=max; i>0 ; i--){
        System.out.print((i<a) ? "* " : "  ");
        System.out.print((i<b) ? "* " : "  ");
        System.out.println((i<c) ? "* " : "  ");
    }
  }
}
  •  Tags:  
  • java
  • Related