翻译这个等式...请

时间:2015-08-29 02:46:48

标签: java swing math equation

我在这里有我用java编写的期末考试计算器应用程序,但是我无法正确地计算数学。

这是我的代码

import java.awt.EventQueue;
import javax.swing.JOptionPane; 
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyFirstApp {

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;

/**
 * Launch the application.
 */
public static void main(String[] args) {


    try {
        // Set System L&F
    UIManager.setLookAndFeel(
        UIManager.getSystemLookAndFeelClassName());
} 
catch (UnsupportedLookAndFeelException e) {
   // handle exception
}
catch (ClassNotFoundException e) {
   // handle exception
}
catch (InstantiationException e) {
   // handle exception
}
catch (IllegalAccessException e) {
   // handle exception
}

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyFirstApp window = new MyFirstApp();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });
}

/**
 * Create the application.
 */
public MyFirstApp() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 342, 281);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            char c=e.getKeyChar();
            if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE)) {
                e.consume();
            }

        }

    });



    textField.setBounds(88, 37, 150, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            char c=e.getKeyChar();
            if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) 
                    || c==KeyEvent.VK_DELETE)) {

                e.consume();

            }
        }
    });
    textField_1.setColumns(10);
    textField_1.setBounds(88, 84, 150, 20);
    frame.getContentPane().add(textField_1);

    textField_2 = new JTextField();
    textField_2.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            char c=e.getKeyChar();
            if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE)) {
                e.consume();
            }
        }
    });
    textField_2.setColumns(10);
    textField_2.setBounds(127, 129, 71, 20);
    frame.getContentPane().add(textField_2);

    JButton btnNewButton = new JButton("Calculate");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String gradestring = textField.getText();
            String wantstring = textField_1.getText();
            String finalstring = textField_2.getText();

            int gradeparse = Integer.parseInt(gradestring);
            int wantparse = Integer.parseInt(wantstring);
            int finalpercentage = Integer.parseInt(finalstring);



            int findworthpercentage = finalpercentage;
            int showfinal = 

            JOptionPane.showMessageDialog(null, "This is what you need " + showfinal/100);

        }
    });
    btnNewButton.setBounds(116, 181, 101, 39);
    frame.getContentPane().add(btnNewButton);

    JLabel lblGrade = new JLabel("Grade:");
    lblGrade.setBounds(45, 40, 43, 14);
    frame.getContentPane().add(lblGrade);

    JLabel lblWant = new JLabel("Want:");
    lblWant.setBounds(45, 87, 33, 14);
    frame.getContentPane().add(lblWant);

    JLabel lblWorth = new JLabel("Worth:");
    lblWorth.setBounds(85, 132, 43, 14);
    frame.getContentPane().add(lblWorth);

    JLabel label = new JLabel("%");
    label.setBounds(202, 132, 11, 14);
    frame.getContentPane().add(label);

    JLabel label_1 = new JLabel("%");
    label_1.setBounds(242, 87, 11, 14);
    frame.getContentPane().add(label_1);

    JLabel label_2 = new JLabel("%");
    label_2.setBounds(242, 40, 11, 14);
    frame.getContentPane().add(label_2);
}
}

我需要数学运算的地方是int showfinal。这是等式:

  

成绩=考试成绩×考试成绩+(1 - 考试成绩)×当前成绩

我需要帮助将其翻译成我的项目。 谢谢,抱歉英语不好。

2 个答案:

答案 0 :(得分:2)

所以,
findworthpercentage =考试价值?
wantparse =考试成绩?
gradeparse =当前等级?

如果是,则:

showfinal = findworthpercentage * wantparse + ( 1 - findworthpercentage ) * gradeparse;

你的变量用法&命名有点令人困惑

答案 1 :(得分:2)

而不是: int showfinal 对于更正确的答案,它是双倍的: double showfinal

然后,假设当前等级是百分比,例如95(A),这是有效的等式:

double showfinal = (wantparse/100.0 - ((1 - findworthpercentage/100.0) * gradeparse/100.0)) / (findworthpercentage/100.0);
showfinal = Math.round(showfinal*100.0)/100.0;

然后,如果要显示百分比,请更改: JOptionPane.showMessageDialog(null, "This is what you need " + showfinal/100);

乘以100,而不是: JOptionPane.showMessageDialog(null, "This is what you need " + showfinal*100);

如果你想要一个小数,不要乘以100。

如果我明白你的意图,这个等式将会产生一个例子:

gradeparse = 85 //Current Grade percentage
wantparse = 90 //Grade wanted percentage
findworthpercentage = 50 //Final is worth half of overall grade

showfinal = 0.95 //Grade needed in Final. Again, multiply by 100 for the percentage.