如何在GUI中编写代码,以便在calculation、Java、BlueJ中使用for assign变量接收多个输入

如何在GUI中编写代码,以便在calculation、Java、BlueJ中使用for assign变量接收多个输入,java,Java,我正在上基础Java编程课,我的教授用Java完成了一个项目作业。这个主题非常开放,所以我提出了“预期成绩计算程序”,这是我的计算代码 import java.util.Scanner; import java.io.*; public class ProAnalyziz { public ProAnalyziz() { //declare variable double g,c,sum=0,mean,C=0,GPA,x=0,y,z,result=0; int amou

我正在上基础Java编程课,我的教授用Java完成了一个项目作业。这个主题非常开放,所以我提出了“预期成绩计算程序”,这是我的计算代码

import java.util.Scanner;
import java.io.*;

public class ProAnalyziz
{
public ProAnalyziz()
{
    //declare variable
    double g,c,sum=0,mean,C=0,GPA,x=0,y,z,result=0;
    int amount;
   Scanner subjects = new Scanner(System.in);
   Scanner grade = new Scanner(System.in);
   Scanner credits = new Scanner(System.in);
   Scanner gpa = new Scanner(System.in);

   //all grade will be input in fixed amount ex: 4,3.5,3,...,0 
   
   //create program parameter
   System.out.println("How many subject you have leaned this semester?");
   amount = subjects.nextInt();
   System.out.println("Please enter your expected GPA in this semester:");
   GPA = gpa.nextDouble();
   System.out.println("Please enter your grade and credits of each subjects in order");
   System.out.println("If you don't know your grade, please type 0 ");
   System.out.println();
   
   //calculation part
   for(int order = 1; order<=amount; order++){
       //loop for storing input data
       System.out.printf("Subject No.%d : Your grade is: ",order);
       g = grade.nextDouble(); //input grade
       System.out.printf("Subject No.%d : Your credit is: ",order);
       c = credits.nextDouble(); //input credit
       if(g==0){
           //create coefficient of x
           x += c;
        }
       C += c; //total credits
       sum += g*c;
       System.out.println();
    }
    //mean for current known grade
       mean = sum/C;
    //create options
    if(mean>GPA){
        System.out.println("Your grade is " + mean);
        System.out.println("You already pass dude!!!");
    }
    else if(mean<=GPA){
        y = ((GPA*C)-sum)/x;
        //System.out.println(+y); (for checking error)
        if(y<=4){
            if(y<=1){//convert output answer to integer
                result = 1;
            }
            else if (1 < y&&y <= 1.5){
                result = 1.5;
            }
            else if (1.5 < y&&y <= 2){
                result = 2;
            }
            else if (2 < y&&y <= 2.5){
                result = 2.5;
            }
            else if (2.5 < y&&y <= 3){
                result = 3;
            }
            else if (3 < y&&y <= 3.5){
                result = 3.5;
            }
            else if (3.5 < y&&y <= 4){
                result = 4;
            }
            
            System.out.println("Your missing grade for each subject is " +result);
            System.out.println("End of program...");
        }
        else{
            System.out.println("It's impossible for you to get out of this hell, sorry...");
            System.out.println("End of program...");
        }
}     
}
}

乍一看,你真的应该用一台扫描仪来输入。其次,为了获得对UI元素的输入,您可以通过传入对UI元素的引用或使其可访问来调用该UI元素上的更新方法。我会发代码,但在手机上回复

“我不知道如何编写一个文本字段代码来接收多个输入”,只需创建多个文本框…不要添加IDE标记,您的问题实际上与IDE无关。您可以在任何IDE中编写相同的代码。您应该避免将GUI和控制台代码组合在一起。使用GUI,通过GUI(JTextFields,JComboBox,…)获取用户输入,并使用GUI显示结果。感谢您的建议!但是我还是不明白。我上个月刚开始上课,我真的知道一些Java编程的知识(我的英语技能让它变得更难,哈哈)。
/**
* Write a description of class ProAnalyziz here.
*
* @author Bhuvanut Duangsasidhorn & 
* @version 1.00
*/

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

public class GUI_ProAnalyziz extends JFrame implements ActionListener{
    //Create the GUI elemts to use
    private JTextField tbxAmount;
    private JTextField tbxExpectedGPA;
    private JTextField tbxGrade;
    private JTextField tbxCredit;
    private JTextArea tbxOutput;
    private JButton btSubmit;
    private double totalCredits;
    private double coeficientX;
    private double sum;
    private double result;
    private double y;
    private double ExGPA;
    private int index;
    //Constructor of  the class
    public GUI_ProAnalyziz(){
       index = 0;
       sum = 0;
       totalCredits = 0;
       coeficientX = 0;
       ExGPA = 0;
       y = 0;
       totalCredits = 0;
       result = 0;
       coeficientX = 0;
       
       this.setTitle("ProAnalyziz Alpha v.1"); //Set title of the program
       //
       JPanel panelLeft = new JPanel(); //Create a new panel to put in the textBox and labels
       panelLeft.setLayout(new GridLayout(2,2)); //Set the style of the layout
       panelLeft.add(new JLabel("Class you have taked this semester?")); //Attach a label in the panel
       tbxAmount = new JTextField(10);
       panelLeft.add(tbxAmount); //And inmediately attach the text box
       //the same as I did previously
       panelLeft.add(new JLabel("Expected GPA in this semester: "));
       tbxExpectedGPA = new JTextField(10);
       panelLeft.add(tbxExpectedGPA);
       Container panel = this.getContentPane(); //Create the container of the panels
       panel.setLayout(new FlowLayout());
       panel.add(panelLeft);
       //
       JPanel panelCenter = new JPanel();
       panelCenter.setLayout(new GridLayout(2, 2));
       //Create the right panel
       btSubmit = new JButton("Next");
       btSubmit.addActionListener(this); //Activate the action listener
       panelCenter.add(new JLabel("Grade : "));
       tbxGrade = new JTextField(10);
       tbxGrade.setEnabled(false);
       panelCenter.add(tbxGrade);
       panelCenter.add(new JLabel("Credit : "));
       tbxCredit = new JTextField(10);
       tbxCredit.setEnabled(false);
       panelCenter.add(tbxCredit);       
       panel.add(panelCenter);
       JPanel panelRight = new JPanel(); //Create a new panel to put in the textBox and labels
       panelRight.setLayout(new GridLayout(1,1)); //Set the style of the layout
       panelRight.add(btSubmit);
       panel.add(panelRight);
       JPanel panelRightRight = new JPanel(); //Create a new panel to put in the textBox and labels
       panelRight.setLayout(new GridLayout(1,1)); //Set the style of the layout
       tbxOutput = new JTextArea(20, 50);
       tbxOutput.setEnabled(false);
       panelRightRight.add(tbxOutput);
       panel.add(panelRightRight);
    }
    
    public void actionPerformed(ActionEvent e)
    {
       
            if(e.getSource() == btSubmit)
        {
            if(tbxAmount.isEnabled() && tbxExpectedGPA.isEnabled()){
                tbxGrade.setEnabled(true);
                tbxAmount.setEnabled(false);
                tbxCredit.setEnabled(true);
                tbxExpectedGPA.setEnabled(false);
                //btSubmit.setText("Attach");
                return;
            }
            double amount = Double.parseDouble(tbxAmount.getText());
            double credit = Double.parseDouble(tbxCredit.getText());
            double grade = Double.parseDouble(tbxGrade.getText());
            double y = Double.parseDouble(tbxExpectedGPA.getText());
            
           
            //__________________________________Output message___________________________________________
            String output = "Subject No. " + Integer.toString(index + 1) + ": Your grade is : " + String.valueOf(grade) + "\r\n";
            output += "Subject No. " + Integer.toString(index + 1) + ": Your grade is : " + String.valueOf(credit) + "\r\n\r\n";
            tbxOutput.append(output);
            //__________________________________Increase items____________________________________________
            
            index++;
            
            //__________________________________Calcuation _______________________________________________
            
            if(grade == 0){
                coeficientX += credit;
            }
           
            
            totalCredits += credit;
            
            sum += grade*credit;
            
                
            result = ((y*totalCredits)-sum)/coeficientX;
            
            //_________________________________Go in this if when the program finish______________________
            
            if(index == Integer.parseInt(tbxAmount.getText())){
                tbxGrade.setEnabled(false);
                tbxCredit.setEnabled(false);
                btSubmit.setEnabled(false);
                //____________________________________Finsih calculate__________________________________
                String out = "Your missing grade for each subject is : "+ String.valueOf(result);
                
                tbxOutput.append(out);
            }
        }
        
    }

    public static void main(String[] args) {
        GUI_ProAnalyziz frame = new GUI_ProAnalyziz();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}