用JAVA编写一个实现加法运算的程序
刚学JAVA老师就布置我们写程序,说要实现加法的运算,还要可以实现虚数的运算,我实在不懂啊,请有心人帮帮我吧,小女子感激不尽了...
刚学JAVA老师就布置我们写程序,说要实现加法的运算,还要可以实现虚数的运算,我实在不懂啊,请有心人帮帮我吧,小女子感激不尽了
展开
4个回答
展开全部
public class Plus {
public static void main(String[] args) {
String fristParam = args[0];
String secondParam = args[1];
String result = plus(fristParam, secondParam);
System.out.println(result);
}
private static String plus(String _sFristParam, String _sSecondParam) {
String sResult = null;
if (isVirtual(_sFristParam) || isVirtual(_sSecondParam)) {
sResult = virualPlus(_sFristParam, _sSecondParam);
}
else {
sResult = normalPlus(_sFristParam, _sSecondParam);
}
return sResult;
}
private static String normalPlus(String _sFristParam, String _sSecondParam) {
if (_sFristParam == null || _sSecondParam == null) {
return "对不起输入有错,请重新输入";
}
int nFristParam = Integer.parseInt(_sFristParam);
int nSecondParam = Integer.parseInt(_sSecondParam);
int nResult = nFristParam + nSecondParam;
String sResult = String.valueOf(nResult);
return sResult;
}
private static String virualPlus(String _sFristParam, String _sSecondParam) {
String sFirstActual = getActual(_sFristParam);
String sFirstVirtual = getVirtual(_sFristParam);
String sSecondActual = getActual(_sSecondParam);
String sSecondVirtual = getVirtual(_sSecondParam);
String sResult = null;
int nFirstActual = 0;
int nFirstVirtual = 0;
int nSecondActual = 0;
int nSecondVirtual = 0;
int nVirtual = 0;
int nActual = 0;
if (sFirstActual == null || sFirstVirtual == null || sSecondActual == null ||
sSecondVirtual == null) {
return "对不起输入的虚数有错,请重新输入";
}
nFirstActual = Integer.parseInt(sFirstActual);
nFirstVirtual = Integer.parseInt(sFirstVirtual);
nSecondActual = Integer.parseInt(sSecondActual);
nSecondVirtual = Integer.parseInt(sSecondVirtual);
nVirtual = nFirstVirtual + nSecondVirtual;
nActual = nFirstActual + nSecondActual;
String sVirtual = String.valueOf(nVirtual);
String sActual = String.valueOf(nActual);
sResult = sActual + "+" + sVirtual + "i";
return sResult;
}
private static String getVirtual(String _sParam) {
String[] members = _sParam.split("\\+");
String sResult = (members[1] != null) ? members[1].substring(0, 1) : null;
return sResult;
}
private static String getActual(String _sParam) {
String[] members = _sParam.split("\\+");
String sResult = (members[0] != null) ? members[0] : null;
return sResult;
}
private static boolean isVirtual(String _sParam) {
return (_sParam.indexOf("i") != -1) ? true : false;
}
}
自己改一下吧,基本上没问题
public static void main(String[] args) {
String fristParam = args[0];
String secondParam = args[1];
String result = plus(fristParam, secondParam);
System.out.println(result);
}
private static String plus(String _sFristParam, String _sSecondParam) {
String sResult = null;
if (isVirtual(_sFristParam) || isVirtual(_sSecondParam)) {
sResult = virualPlus(_sFristParam, _sSecondParam);
}
else {
sResult = normalPlus(_sFristParam, _sSecondParam);
}
return sResult;
}
private static String normalPlus(String _sFristParam, String _sSecondParam) {
if (_sFristParam == null || _sSecondParam == null) {
return "对不起输入有错,请重新输入";
}
int nFristParam = Integer.parseInt(_sFristParam);
int nSecondParam = Integer.parseInt(_sSecondParam);
int nResult = nFristParam + nSecondParam;
String sResult = String.valueOf(nResult);
return sResult;
}
private static String virualPlus(String _sFristParam, String _sSecondParam) {
String sFirstActual = getActual(_sFristParam);
String sFirstVirtual = getVirtual(_sFristParam);
String sSecondActual = getActual(_sSecondParam);
String sSecondVirtual = getVirtual(_sSecondParam);
String sResult = null;
int nFirstActual = 0;
int nFirstVirtual = 0;
int nSecondActual = 0;
int nSecondVirtual = 0;
int nVirtual = 0;
int nActual = 0;
if (sFirstActual == null || sFirstVirtual == null || sSecondActual == null ||
sSecondVirtual == null) {
return "对不起输入的虚数有错,请重新输入";
}
nFirstActual = Integer.parseInt(sFirstActual);
nFirstVirtual = Integer.parseInt(sFirstVirtual);
nSecondActual = Integer.parseInt(sSecondActual);
nSecondVirtual = Integer.parseInt(sSecondVirtual);
nVirtual = nFirstVirtual + nSecondVirtual;
nActual = nFirstActual + nSecondActual;
String sVirtual = String.valueOf(nVirtual);
String sActual = String.valueOf(nActual);
sResult = sActual + "+" + sVirtual + "i";
return sResult;
}
private static String getVirtual(String _sParam) {
String[] members = _sParam.split("\\+");
String sResult = (members[1] != null) ? members[1].substring(0, 1) : null;
return sResult;
}
private static String getActual(String _sParam) {
String[] members = _sParam.split("\\+");
String sResult = (members[0] != null) ? members[0] : null;
return sResult;
}
private static boolean isVirtual(String _sParam) {
return (_sParam.indexOf("i") != -1) ? true : false;
}
}
自己改一下吧,基本上没问题
展开全部
以下程序仅实现2个整数相加,虚数的太长了,我没写.其实差不多,只是多设几个文本框和标签,弄成类似(___+___i)+(___+___i)=_____的形式
我也是在学着写,你可以参考下.可以运行:)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class sumup extends JApplet implements ActionListener
{
Container cp=getContentPane();
JLabel lb1=new JLabel("两整数相加:"),
lb2=new JLabel("+"),
lb3=new JLabel("=");
JTextField tf1=new JTextField(3),
tf2=new JTextField(3);
JTextArea tfs=new JTextArea("和是",1,4);
JButton bt=new JButton("计算");
public void init()
{
cp.setLayout(new FlowLayout());
cp.add(lb1);
cp.add(tf1);
cp.add(lb2);
cp.add(tf2);
cp.add(lb3);
cp.add(tfs);
cp.add(bt);
bt.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int i1,i2,i3;
String str;
i1=Integer.parseInt(tf1.getText());
i2=Integer.parseInt(tf2.getText());
i3=i1+i2;
str=Integer.toString(i3);
if(e.getSource()==bt)
tfs.setText(str);
}
}
我也是在学着写,你可以参考下.可以运行:)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class sumup extends JApplet implements ActionListener
{
Container cp=getContentPane();
JLabel lb1=new JLabel("两整数相加:"),
lb2=new JLabel("+"),
lb3=new JLabel("=");
JTextField tf1=new JTextField(3),
tf2=new JTextField(3);
JTextArea tfs=new JTextArea("和是",1,4);
JButton bt=new JButton("计算");
public void init()
{
cp.setLayout(new FlowLayout());
cp.add(lb1);
cp.add(tf1);
cp.add(lb2);
cp.add(tf2);
cp.add(lb3);
cp.add(tfs);
cp.add(bt);
bt.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int i1,i2,i3;
String str;
i1=Integer.parseInt(tf1.getText());
i2=Integer.parseInt(tf2.getText());
i3=i1+i2;
str=Integer.toString(i3);
if(e.getSource()==bt)
tfs.setText(str);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void mian(String[] args){
float a,b,c;
a=3;
b=5;
c=a+b;
System.out.println("a=3,b=5,a+b="+c);
}
a,b改成你想计算的数就好了
float a,b,c;
a=3;
b=5;
c=a+b;
System.out.println("a=3,b=5,a+b="+c);
}
a,b改成你想计算的数就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
各位,人家说还要实现虚数呢!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询