java数组个数无限,Java GPA计算(如何设置无限数组?)

基本上,我的问题是我不确定如何从用户设置无限输入数组..

目前,只有当用户准确输入6个主题时,计算才会顺利进行,但如果输入的主题少于或大于6,则计算不正确.

不知何故,我想让输入无限..

这是我到目前为止:

主要课程

import javax.swing.*;

public class CGPAMain extends JFrame

{

public static void main(String[] args)

{

JFrame frame = new JFrame("GPA Calculation");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MainPage panel = new MainPage();

frame.getContentPane().add(panel);

frame.pack();

frame.setVisible(true);

}

}

其他类

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MainPage extends JPanel

{

//declare GUI elements

private JLabel subjectLabel, cHoursLabel, gradeLabel;

private JTextField subject, cHours;

private JButton addSubjectButton, calcGPAButton, clearAllButton;

private JTextArea tArea;

private JComboBox grade;

//declare array to store and collect user input value

String[] subjectArray = new String[6];

String[] gradeArray = new String[6];

int[] cHoursArray = new int[6];

double[] gradeValue = { 4.00, 3.67, 3.33, 3.00, 2.67, 2.33, 2.00, 1.67, 1.33, 1.00, 0.00 };

String[] gradeLetter= { "A", "A-", "B+", "B", "B-", "C+", "C", "D", "D-", "E", "F"};

public MainPage()

{

setLayout (null);

setPreferredSize (new Dimension(500, 500));

setBackground (Color.orange);

//Properties of GUI elements

subjectLabel = new JLabel ("Subject Name: ");

subject = new JTextField (33);

subject.addActionListener (new TempListener());

gradeLabel = new JLabel ("Grade: ");

grade = new JComboBox (gradeLetter);

grade.addActionListener (new TempListener());

cHoursLabel = new JLabel ("Credit Hours: ");

cHours = new JTextField (1);

cHours.addActionListener (new TempListener());

addSubjectButton = new JButton("Add Another Subject");

addSubjectButton.addActionListener(new TempListener());

calcGPAButton = new JButton("Calculate GPA");

calcGPAButton.addActionListener(new TempListener());

clearAllButton = new JButton("Clear All");

clearAllButton.addActionListener(new TempListener());

tArea = new JTextArea(5, 5);

tArea.setEditable(false);

add (subjectLabel);

add (subject);

add (gradeLabel);

add (grade);

add (cHoursLabel);

add (cHours);

add (addSubjectButton);

add (calcGPAButton);

add (clearAllButton);

add (tArea);

//Position of GUI elements

subjectLabel.setBounds (20, 20, 150, 20);

subject.setBounds (120, 20, 350, 20);

gradeLabel.setBounds (20, 50, 50, 20);

grade.setBounds (120, 50, 50, 20);

cHoursLabel.setBounds (20, 80, 100, 20);

cHours.setBounds (120, 80, 50, 20);

addSubjectButton.setBounds (20, 120, 200, 30);

calcGPAButton.setBounds (300, 440, 175, 30);

clearAllButton.setBounds (20, 440, 120, 30);

tArea.setBounds (20, 170, 450, 250);

}

private class TempListener implements ActionListener

{

//---------------------------------------------------------------------------

// Performs the conversion when the enter key is pressed in the text field.

//---------------------------------------------------------------------------

double tCrPoints = 0.00, tCrHours = 0.00, tGPA = 0.00;

String status;

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == addSubjectButton)

{

for (int i=0; i<6; i++)

{

subjectArray[i] = subject.getText();

gradeArray[i] = (String) grade.getSelectedItem();

cHoursArray[i] = Integer.parseInt(cHours.getText());

}

tArea.append (subject.getText() + "" +

grade.getSelectedItem() + "" +

cHours.getText() + "

");

subject.setText("");

cHours.setText("");

}

if (event.getSource() == calcGPAButton)

{

for (int i=0 ; i

{

for (int j=0; j

{

if(gradeArray[i].equals(gradeLetter[j]))

{

tCrHours += cHoursArray[i];

tCrPoints += gradeValue[j] * cHoursArray[i];

}

}

}

tGPA = tCrPoints/tCrHours;

if (tGPA >= 2)

status = ("Pass");

else

status = ("Fail");

//Output for text area

tArea.setText("Total Credit Points : " + tCrPoints + "

" +

"Total Credit Hours : " + tCrHours + "

" +

"Grade Point Average (GPA) : " + tGPA + "

" +

"Status : " + status);

}

if (event.getSource() == clearAllButton)

{

tArea.setText("");

cHours.setText("");

grade.setSelectedIndex(0);

tCrHours = 0.00;

tCrPoints = 0.00;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值