0
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.applet.Applet;
import java.util.Scanner;

public class Histogram extends Applet{
static int [] scores= {13,30,23,8};
static int [] minInterval = {0,25,50,75};
static int [] maxInterval = {25,50,75,100};
public void paint (Graphics g){
    int max = 0; 
    for (int i = 0; i < scores.length; i++) {
        if (max < scores[i]) {
            max = scores[i];
        }
    }

    Graphics2D g2 = (Graphics2D)g;
    Point2D.Double Yi = new Point2D.Double(50,50);
    Point2D.Double Yf = new Point2D.Double(50,30*scores.length);
    Line2D.Double Y = new Line2D.Double (Yi,Yf);
    Point2D.Double Xi = new Point2D.Double(50,50);
    Point2D.Double Xf = new Point2D.Double(50+(8*max),50);
    Line2D.Double X = new Line2D.Double (Xi,Xf);
    int x = 8*max;
    //Draw the "Score"
    int headerX = 50+(x/(max/5))*((max/5)-1);
    g2.drawString("Histogram of Student Scores",(headerX),30);
    for(int i=0;i<=max/5;i++){
        int j = (i)*5;
        if(i<max/5)
            g2.drawString(String.format("%d",j),50+(x/(max/5))*i,50);
        else
            g2.drawString(String.format("Number of Students"),50+(x/(max/5))*i,50);

    }
    for(int i=0;i<=maxInterval.length;i++){
        if(i != maxInterval.length-1 )
            g2.drawString(String.format("[%d,%d)",minInterval,maxInterval),20,60+(30)*i);
        else if(i == maxInterval.length-1)
            g2.drawString(String.format("[%d,%d]",minInterval,maxInterval),20,60+(30)*i);
        else
            g2.drawString("Score Ranges",20,60+(30)*i);
    }
    g2.draw(X);
    g2.draw(Y);
}

}

我的问题是代码没有进入第三个循环。但是当我在另一个循环中测试时

方法。它有点工作。所以我不知道下一步该做什么。我想知道为什么它不

执行该代码块。

预先感谢。

4

1 回答 1

0

您提供了一个数组作为参数String.format("...", , )(而不是 an int,您可以通过从数组中查找一个元素来获得它,大概是使用 index i)。

由于它是 String.format 的参数,因此您很可能没有收到编译器或 IDE 警告。

所以如果你解决了这个问题,代码看起来像这样,你可以从那里得到它。

    for (int i = 0; i <= maxInterval.length; i++) {
        if (i != maxInterval.length - 1)
            g2.drawString(String.format("[%d,%d]", minInterval[i], maxInterval[i]), 20, 60 + (30) * i);
        else if (i == maxInterval.length - 1)
            g2.drawString(String.format("[%d,%d]", minInterval[i], maxInterval[i]), 20, 60 + (30) * i);
        else
            g2.drawString("Score Ranges", 20, 60 + (30) * i);
    }
于 2013-11-02T17:58:31.503 回答