java:二次方程工具的输出

时间:2016-02-26 04:31:08

标签: java output equation

我正在开发一个java应用程序,允许用户输入数据(a,b和c为ax ^ 2 + bx + c = 0),这样就可以进行计算并显示解决方案对于屏幕上的用户(具体来说,它将解决x-plus和x-minus)。

我认为我已经使用下面的代码走上正轨(当然,这意味着我会发现我的错误,哈哈),但我似乎无法弄清楚如何在计算完成后正确显示x-plus和x-minus。

我认为我不确定一件事的代码位置(例如,在放置计算之前或之后;我尝试了两种方式,每次都错了)。我也想知道导入JOptionPane是否真的是完成这项任务的方法。

这是我到目前为止所拥有的。有什么建议吗?

package a3main;
import javax.swing.JOptionPane;
public class A3main {

public static void main(String[] args) {
    int a;
    int b;
    int c;
    String input;

    input = JOptionPane.showInputDialog("Please enter your value for a:  ");
    a = (int) Double.parseDouble(input);

    input = JOptionPane.showInputDialog("Please enter your value for b:  ");
    b = (int) Double.parseDouble(input);

    input = JOptionPane.showInputDialog("Please enter your value for c:  ");
    c = (int) Double.parseDouble(input);

}

private double XPlus (double a, double b, double c){
    return ((-b) + Math.sqrt(( b * b) - (4 * a * c))) / (2 * a);
}

private double XMinus (double a, double b, double c) {
    return ((-b) - Math.sqrt(( b * b) - (4 * a * c))) / ( 2 * a);
}
}

3 个答案:

答案 0 :(得分:0)

您好,您可以使用它来显示您的价值:

JOptionPane.showMessageDialog(null, "Your text: " + your value);

您也可以添加此内容以获取新行:

   JOptionPane.showMessageDialog(null, "Your text: " + yourValue + "\n" + "Anoter text:" + anotherValue);

在代码中,如果要查看输入的所有结果,则必须将其添加到输入的末尾,否则,必须在每次输入后添加它。

这是最后的一个例子:

package a3main;
import javax.swing.JOptionPane;
public class A3main {

public static void main(String[] args) {
int a;
int b;
int c;
String input;

input = JOptionPane.showInputDialog("Please enter your value for a:  ");
a = (int) Double.parseDouble(input);

input = JOptionPane.showInputDialog("Please enter your value for b:  ");
b = (int) Double.parseDouble(input);

input = JOptionPane.showInputDialog("Please enter your value for c:  ");
c = (int) Double.parseDouble(input);

JOptionPane.showMessageDialog(null, "A value: " + a + "\nB value:" + b + "\nC value:" + c);

}

private double XPlus (double a, double b, double c){
    return ((-b) + Math.sqrt(( b * b) - (4 * a * c))) / (2 * a);
}

private double XMinus (double a, double b, double c) {
    return ((-b) - Math.sqrt(( b * b) - (4 * a * c))) / ( 2 * a);
}
}

答案 1 :(得分:0)

您也可以通过System.out.println(xPlus(a, b, c));提供信息,然后再拨打xMinus。这会打印出您想要的信息System.out

另一个答案涉及使用GUI执行此操作。

答案 2 :(得分:0)

此代码将创建一个单一窗格程序,完全满足您的需要:

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class A3main {
private static Component frame;
public static void main(String[] args) throws Exception{
  JTextField a = new JTextField(5);
  JTextField b = new JTextField(5);
  JTextField c = new JTextField(5);
  JLabel aLabel = new JLabel("a value:");
  JLabel bLabel = new JLabel("b value:");
  JLabel cLabel = new JLabel("c value:");
  JLabel calculatedLabel = new JLabel("Calculated:");    
  JLabel calculatedNum = new JLabel("0.0");    
  JPanel myPanel = new JPanel();

  myPanel.add(aLabel);
  myPanel.add(a);
  myPanel.add(bLabel);
  myPanel.add(b);
  myPanel.add(cLabel);
  myPanel.add(c);
  myPanel.add(calculatedLabel);
  myPanel.add(calculatedNum);
  myPanel.setLayout(new GridLayout(4,2));


//creating icon
final ImageIcon icon = new ImageIcon(new URL("http://iconizer.net/files/Devine_icons/thumb/128/Calculater.png"));

//variable declarations    
double aNum = 0;
double bNum = 0;
double cNum = 0;
boolean invalidInput = false;
String Answer = "";
boolean restart = true;
Object[] mainOptions = {"Exit","Calculate","Copy answer"};


while(restart){
    //user input
    int mainPanel = JOptionPane.showOptionDialog(null, myPanel, 
       "Quadratic Equation Calculator", JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,
        icon, mainOptions, mainOptions[1]);

    if(mainPanel==JOptionPane.CANCEL_OPTION){
        StringSelection selection = new StringSelection(Answer);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);               
    }else if(mainPanel==JOptionPane.NO_OPTION){
            invalidInput = false;
        try{
            aNum = Double.parseDouble(a.getText());
        }catch(NumberFormatException nfe){
            invalidInput = true;
            calculatedLabel.setText(""); 
            calculatedNum.setText("Invalid a value");                    
        }try{
            bNum = Double.parseDouble(b.getText())/100;
        }catch(NumberFormatException nfe){
            invalidInput = true;
            calculatedLabel.setText(""); 
            calculatedNum.setText("Invalid b value");                    
        }try{
            cNum = Double.parseDouble(c.getText());
        }catch(NumberFormatException nfe){
            invalidInput = true;
            calculatedLabel.setText(""); 
            calculatedNum.setText("Invalid c value");                    
        }

        //calculating answer
        double b2 = bNum*bNum;
        double inSqrt = b2 - (4 * aNum * cNum);
        double ans1 = (((bNum*(-1)) - java.lang.Math.sqrt(java.lang.Math.abs(inSqrt))) / ( 2 * aNum));
        double ans2 = (((bNum*(-1)) - java.lang.Math.sqrt(java.lang.Math.abs(inSqrt))) / ( 2 * aNum))*-1;

        //converting answer to string format
        Answer = "+: "+ans1+", -: "+ans2;

        if(!invalidInput){
            calculatedLabel.setText("Calculated:"); 
            calculatedNum.setText(Answer);
        }
    }else{
        System.exit(0);               
    }           
}
} // main string
} // public class
相关问题