簡體   English   中英

我需要幫助修復我的直方圖

[英]I need help repairing my histogram

我正在嘗試制作一個生成隨機數並保存每個數字滾動次數的數組。 然后我試圖創建結果的直方圖,但控制台變為空白。 我想不出巧妙地使用循環的方法,所以我不得不使用一個非常粗略的解決方案。

import java.util.Random;
import java.util.Arrays;

public class DiceRolls {
    public static void main(String[] args){
        int[] diceRolls = new int [9];

        Random rand = new Random();

        for(int numberOfRolls = 0; numberOfRolls <= 20; numberOfRolls++){
            int individualRolls = rand.nextInt(9);
            diceRolls[individualRolls] ++;
        }
        for(int y = 0; y >= diceRolls[0]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[1]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[2]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[3]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[4]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[5]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[6]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[7]; y++){
            System.out.print("*");
        }

        System.out.println("         ");
        for(int y = 0; y >= diceRolls[8]; y++){
            System.out.print("*");
        }
    }
}
public static void main(String[] args){

    int[] diceRolls = new int [9];

    Random rand = new Random();

    for(int numberOfRolls = 0; numberOfRolls <= 20; numberOfRolls++){
        int individualRolls = rand.nextInt(9);
        diceRolls[individualRolls] ++;
        }
    for(int j : diceRolls) {
        for(int i = 0; i<j; i++) {
            System.out.print("*");
        }
    System.out.println();
    }
    }

任何問題?

你反轉了循環條件。 他們應該是:

y < diceRolls[?]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM