0

嗨,我正在尝试获取文本字体大小和颜色的数组的频率,当我有字符串并且我计算频率时,我想根据它们的总体值更改单词和颜色的大小,我有试图划分frequency/average *10 但给我null,但在我的计算器上它给了我即80/100 * 10 = 8,这将采用8的索引并具有180字体大小

谁能帮我这个?

private static int[] fontWeight = { 40, 60, 80, 100, 120, 140, 160,180 };
private static Color[] Colors = { Color.blue, Color.cyan, Color.yellow, Color.green, Color.red,
        Color.orange, Color.pink,Color.pink  };

//主要的

  for (String str: wordList) {


            int wordFreq = randWord.getFrequency();//assume 80 comes here
            int fontSize = getFontSize(wordFreq);
            Font font = new Font(Font.SANS_SERIF, Font.ITALIC, fontWeight [fontSize ]);
            graphics.setFont(font);
            graphics.setColor(Colors[fontSize]);
            FontMetrics fm = graphics.getFontMetrics();

            graphics.drawString(randWord.getWord() + "", x, y);
            }

//方法

  private static int getFontSize(int freq) {

    int newFont = (int) (freq/100)*10;

    if (newFont >= 5) newFont = 6;
    // if(newFont<2)
    // newFont = 1;
    System.out.println("freq " + freq + " font index" + newFont + "  font size "
            + fontWeight[newFont]);

    return newFont;
}

谢谢你的帮助

4

1 回答 1

1

整数除法的行为不同,如果分子小于分母,则结果为 0。例如 80/100 返回 0。您要做的是将分子乘以该 10,然后除以它。

int newFont = (int) (freq/100)*10;

应该改为

int newFont = (int) (freq*10 / 100);
于 2013-01-09T18:47:17.473 回答