gpt4 book ai didi

java - 如何在Java中将3A表示为 "AAA"或将3B表示为BBB

转载 作者:行者123 更新时间:2023-12-01 23:29:31 27 4
gpt4 key购买 nike

我正在编写一段 Java 代码来表示学生在考试中取得的成绩。当您在标有# of Grade A的栏中输入数字10,并在标有# of Grade Bs的栏中输入数字20,直到达到# of Grade E,然后单击标签“显示栏”图表”,您会得到一个图表,其输出为 Grade As : 10 ....Grade Bs : 20......Grade E : 某事。我向你提出的问题是这样的。如果我想要输出 AAAAAAAAAA(即字母 A 写出 10 次),而不是输出 Grade A : 10,我该怎么做?我想了一天,还是想不出答案。

第二个(相关)问题:如果没有学生取得任何特定成绩(假设没有人获得 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;

}

最佳答案

使用循环:

String longString = "";
for (int i=0; i<numOfLetter; i++) {
longString += gradeLetter;
}

或者,如果您有 Apache Commons 可用:

String longString = StringUtils.repeat(gradeLetter, numOfLetter);

关于java - 如何在Java中将3A表示为 "AAA"或将3B表示为BBB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com