0

学生が試験で達成した成績を表す Java コードを書いています。グレード A の # とラベル付けされたバーに数字の 10 を入力し、グレード Bs の # とラベル付けされたバーに数字 20 を入力し、グレード E の # に達するまでそれを行い、ラベル「表示バー」をクリックします。 chart」の場合、Grade As : 10 ....Grade Bs : 20..........Grade E : something のチャートが得られます。あなたへの私の質問はこれです。出力 Grade A : 10 の代わりに、出力 AAAAAAAAAA (つまり、文字 A を 10 回書き出す) が必要な場合、どうすればよいですか? 一日中考えましたが、まだ答えが思いつきません。

2 番目の (関連する) 質問: 生徒が特定の成績を達成していない場合 (誰も A を採点しなかったとしましょう)、そのバー (成績 A のバー) に成績文字は表示されません。これを考慮してコードを変更する方法を教えてください。これが私のコードです:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class GradeChart extends JFrame
         implements ActionListener {


    private JTextField gradeAField = new JTextField( 5 );
    private JTextField gradeBField = new JTextField( 5 );
    private JTextField gradeCField = new JTextField( 5 );
    private JTextField gradeDField = new JTextField( 5 );
    private JTextField gradeEField = new JTextField( 5 );


    private int gradeAs;
    private int gradeBs;
    private int gradeCs; 
    private int gradeDs;
    private int gradeEs;


    private JButton displayChartButton = new JButton( "Display bar chart" );
    private JButton displayDataFieldsButton = new JButton( "Clear data fields" );


    private JPanel chartPanel = new JPanel();


    private Color chartPanelColor = Color.cyan;


    private final Font labelFont = new Font( "Serif", Font.BOLD, 16 );

    // Various fixed coordinates and dimensions


    private final int chartLeftXA = 30;
    private final int chartLeftXB = 30;
    private final int chartLeftXC = 30;
    private final int chartLeftXD = 30;
    private final int chartLeftXE = 30;


    private final int chartTopYA = 50;
    private final int chartTopYB = 102;
    private final int chartTopYC = 154;
    private final int chartTopYD = 206;
    private final int chartTopYE = 258;


    public static void main( String[] args ) {

        GradeChart chart = new GradeChart();
        chart.setSize( 550, 550 );
        chart.createGUI();
        chart.setVisible( true );

    }  

    private void createGUI() {

        // Set up main window characteristics
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        Container window = getContentPane();
        window.setLayout( new FlowLayout() );


        JLabel gradeAFieldLabel = new JLabel( "# of grade As:" );
        gradeAFieldLabel.setFont( labelFont );
        window.add( gradeAFieldLabel );
        window.add( gradeAField );

        JLabel gradeBFieldLabel = new JLabel( "# of grade Bs:" );
        gradeBFieldLabel.setFont( labelFont );
        window.add( gradeBFieldLabel );
        window.add( gradeBField );

        JLabel gradeCFieldLabel = new JLabel( "# of grade Cs:" );
        gradeCFieldLabel.setFont( labelFont );
        window.add( gradeCFieldLabel );
        window.add( gradeCField );

        JLabel gradeDFieldLabel = new JLabel( "# of grade Ds:" );
        gradeDFieldLabel.setFont( labelFont );
        window.add( gradeDFieldLabel );
        window.add( gradeDField );

        JLabel gradeEFieldLabel = new JLabel( "# of grade Es:" );
        gradeEFieldLabel.setFont( labelFont );
        window.add( gradeEFieldLabel );
        window.add( gradeEField );


        window.add( displayChartButton );
        displayChartButton.addActionListener( this );

        window.add( displayDataFieldsButton );
        displayDataFieldsButton.addActionListener( this );


        chartPanel.setPreferredSize( new Dimension( 450, 300 ) );
        chartPanel.setBackground( chartPanelColor );
        window.add( chartPanel );


    }


    public void actionPerformed( ActionEvent e ) {

        if ( e.getSource() == displayChartButton ) {

            checkAndRecordData();

            Graphics g = chartPanel.getGraphics();



            g.setColor(Color.white);
            g.fillRect(20,20,410,52);
            g.setColor(Color.black);
            g.drawRect(20,20,410,52);
            g.setColor( Color.black );
            g.drawString( "Grade As: " + gradeAs, chartLeftXA, chartTopYA );

            g.setColor(Color.white);
            g.fillRect(20,72,410,52);
            g.setColor(Color.black);
            g.drawRect(20,72,410,52);
            g.setColor( Color.black );
            g.drawString( "Grade Bs: " + gradeBs, chartLeftXB, chartTopYB );

            g.setColor(Color.white);
            g.fillRect(20,124,410,52);
            g.setColor(Color.black);
            g.drawRect(20,124,410,52);
            g.setColor( Color.black );
            g.setColor( Color.black );
            g.drawString( "Grade Cs: " + gradeCs, chartLeftXC, chartTopYC );

            g.setColor(Color.white);
            g.fillRect(20,176,410,52);
            g.setColor(Color.black);
            g.drawRect(20,176,410,52);
            g.setColor( Color.black );
            g.setColor( Color.black );
            g.drawString( "Grade Ds: " + gradeDs, chartLeftXD, chartTopYD );

            g.setColor(Color.white);
            g.fillRect(20,228,410,52);
            g.setColor(Color.black);
            g.drawRect(20,228,410,52);
            g.setColor( Color.black );
            g.setColor( Color.black );
            g.drawString( "Grade Es: " + gradeEs, chartLeftXE, chartTopYE );

        }

        if ( e.getSource() == displayDataFieldsButton ) {

            gradeAField.setText("");
            gradeBField.setText("");
            gradeCField.setText("");
            gradeDField.setText("");
            gradeEField.setText("");

        }
    }  // End of actionPerformed


    private void checkAndRecordData() {

        int tempAs = 0; 
        int tempBs = 0;
        int tempCs = 0;
        int tempDs = 0;
        int tempEs = 0;


        tempAs = Integer.parseInt( gradeAField.getText() );
        tempBs = Integer.parseInt( gradeBField.getText() );
        tempCs = Integer.parseInt( gradeCField.getText() );
        tempDs = Integer.parseInt( gradeDField.getText() );
        tempEs = Integer.parseInt( gradeEField.getText() );


        gradeAs = tempAs;
        gradeBs = tempBs;
        gradeCs = tempCs;
        gradeDs = tempDs;
        gradeEs = tempEs;

    }
4

2 に答える 2

1

いくつかだけ必要な場合:

String someAs = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".substring(0, length);
于 2013-10-23T18:04:26.447 に答える