【问题标题】:Histogram without arrays没有数组的直方图
【发布时间】:2019-10-10 20:50:22
【问题描述】:

我必须写从用户那里读取三个非负整数,然后 使用符号“*”打印由数字表示的数据的“直方图”,即, 三个垂直条,在底部对齐,高度等于 三个数字。 例如,对于数字 3、1 和 8,结果应如下所示:

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

实际上我编写了以下程序,但它会打印一个星号金字塔,并以输入的数字为底:

import java.util.Scanner;

    public class Task1 {
public static void main (String[] args) {
     Scanner scan = new Scanner(System.in);
     System.out.print("Enter a positive odd number: ");
    int n = scan.nextInt();
     scan.close();

     for (int len=1, sp=n/2; len <= n; len+=2, --sp) {
         for (int i = 0; i < sp; ++i)
             System.out.print(" ");
         for (int i = 0; i < len; ++i)
             System.out.print("*");
         System.out.println();
         }

提前谢谢你

【问题讨论】:

  • 从阅读三个数字开始!然后尝试使用所有这些。 (你会想从他们的最大值倒数)

标签: java histogram


【解决方案1】:

导入 java.util.Scanner; 公共类主 {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Type in three numbers");
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();

       int max = 0;

    if (a>b && a>c){
        max = a;
    } else if (b>a && b>c){
        max = b;
    } else if (c>a && c>b){
        max = c;
    }

        for (int i = 8; i>=1; --i) {
            if (i>a) {
                System.out.print(" ");
            }else {
                System.out.print("*");
            }
            if (i>b){
                System.out.print(" ");
            }else {
                  System.out.print("*");
             }
            if (i>c){
                System.out.print(" ");
            }else {
                System.out.print("*");
            }
            System.out.println();
        }
}

}

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2021-07-02
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多