求JAVA高手帮忙修改一下!~
今天老师布置了JAVA作业给我们,我做了如下的JAVA程序给他,结果他要求我把这个程序改成运行的时候会有窗口弹出,可惜是JAVA盲,求JAVA高手帮忙修改一下,QQ邮箱:...
今天老师布置了JAVA作业给我们,我做了如下的JAVA程序给他,结果他要求我把这个程序改成运行的时候会有窗口弹出,可惜是JAVA盲,求JAVA高手帮忙修改一下,QQ邮箱:305295794@qq.com
public static void main(String[] args)
{
System.out.print("=========================");
System.out.println("=========================");
System.out.print("1为加法------2为减法------ "); System.out.println("3为加法------4为减法 ");
System.out.print("=========================");
System.out.println("=========================");
System.out.print("请输入操作符:");
Scanner in = new Scanner(System.in);
String op = in.nextLine();
int opp=0;
double num1,num2;
double sum=0;
if(op.equals("1"))
{System.out.println("您选择的是加法运算");
opp=1;}
else if(op.equals("2"))
{System.out.println("您选择的是减法运算");
opp=2;}
else if(op.equals("3"))
{System.out.println("您选择的是乘法运算");
opp=3;}
else if(op.equals("4"))
{System.out.println("您选择的是除法运算");
opp=4;}
else
{System.out.println("输入错误");
return;}
System.out.print("请输入第一个操作数:");
num1 = in.nextDouble();
System.out.print("请输入第二个操作数:");
num2 = in.nextDouble();
switch(opp)
{
case 1:
sum=num1+num2;
break;
case 2:
sum=num1-num2;
break;
case 3:
sum=num1*num2;
break;
case 4:
sum=num1/num2;
break;
default:
}
System.out.println("计算结果为:"+sum);
}
} 展开
public static void main(String[] args)
{
System.out.print("=========================");
System.out.println("=========================");
System.out.print("1为加法------2为减法------ "); System.out.println("3为加法------4为减法 ");
System.out.print("=========================");
System.out.println("=========================");
System.out.print("请输入操作符:");
Scanner in = new Scanner(System.in);
String op = in.nextLine();
int opp=0;
double num1,num2;
double sum=0;
if(op.equals("1"))
{System.out.println("您选择的是加法运算");
opp=1;}
else if(op.equals("2"))
{System.out.println("您选择的是减法运算");
opp=2;}
else if(op.equals("3"))
{System.out.println("您选择的是乘法运算");
opp=3;}
else if(op.equals("4"))
{System.out.println("您选择的是除法运算");
opp=4;}
else
{System.out.println("输入错误");
return;}
System.out.print("请输入第一个操作数:");
num1 = in.nextDouble();
System.out.print("请输入第二个操作数:");
num2 = in.nextDouble();
switch(opp)
{
case 1:
sum=num1+num2;
break;
case 2:
sum=num1-num2;
break;
case 3:
sum=num1*num2;
break;
case 4:
sum=num1/num2;
break;
default:
}
System.out.println("计算结果为:"+sum);
}
} 展开
2个回答
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Jsq {
public static void main(String args[]) {
new JsqFrm();
}
}
class JsqFrm extends JFrame implements ActionListener {
JButton plu=new JButton("加法");
JButton sub=new JButton("减法");
JButton mul=new JButton("乘法");
JButton div=new JButton("除法");
public JsqFrm() {
super("计算器");
setBounds(250,180,360,280);
setVisible(true);
setLayout(null);
plu.setBounds(40,80,60,35);
add(plu);
sub.setBounds(110,80,60,35);
add(sub);
mul.setBounds(180,80,60,35);
add(mul);
div.setBounds(250,80,60,35);
add(div);
plu.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==plu) {
this.dispose();
new MainFrm("加法界面","加数1","加数2","算法1");
}
if (e.getSource()==sub) {
this.dispose();
new MainFrm("减法界面","减数1","减数2","算法2");
}
if (e.getSource()==mul) {
this.dispose();
new MainFrm("乘法界面","乘数1","乘数2","算法3");
}
if (e.getSource()==div) {
this.dispose();
new MainFrm("除法界面","除数1","除数2","算法4");
}
}
}
class MainFrm extends JFrame implements ActionListener { //ActionListener为实现动作监听的接口
JTextField textone=new JTextField(10);
JTextField texttwo=new JTextField(10);
JLabel numone;
JLabel numtwo;
JButton Sure=new JButton("确定");
JButton Cancel=new JButton("取消");
String num;
public MainFrm(String s,String one,String two,String i) {
super(s);
num=i;
setBounds(250,180,320,280);
setLayout(null);
numone=new JLabel(one);
numtwo=new JLabel(two);
numone.setBounds(60,40,60,40);
add(numone);
textone.setBounds(100,40,130,40);
add(textone);
numtwo.setBounds(60,80,60,40);
add(numtwo);
texttwo.setBounds(100,80,130,40);
add(texttwo);
Sure.setBounds(50,150,70,45);
add(Sure);
Cancel.setBounds(180,150,70,45);
add(Cancel);
setVisible(true);
Sure.addActionListener(this);
Cancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) { //actionPerformed为ActionListener接口的一个方法,用于接受监听事件
if (e.getSource()==Sure) {
this.dispose();
if (textone.getText().trim().equals("")||texttwo.getText().trim().equals("")) {
new ErrorWindow("错误");
}
else
if (num=="算法1") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法1");
}
if (num=="算法2") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法2");
}
if (num=="算法3") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法3");
}
if (num=="算法4") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法4");
}
}
if (e.getSource()==Cancel) {
System.exit(1);
}
}
}
class JsWindow extends JFrame implements ActionListener {
JLabel jieguo;
JButton conti=new JButton("继续运算");
JButton exit=new JButton("退出");
public JsWindow(String s,String one,String two,String i) {
super(s);
setBounds(250,180,320,280);
setLayout(null);
conti.setBounds(100,100,90,30);
add(conti);
exit.setBounds(100,150,90,30);
add(exit);
double in=Double.parseDouble(one);
double jn=Double.parseDouble(two);
if (i=="算法1") {
jieguo=new JLabel("计算结果为:"+plus(in,jn));
}
if (i=="算法2") {
jieguo=new JLabel("计算结果为:"+sub(in,jn));
}
if (i=="算法3") {
jieguo=new JLabel("计算结果为:"+mul(in,jn));
}
if (i=="算法4") {
jieguo=new JLabel("计算结果为:"+div(in,jn));
}
jieguo.setBounds(90,15,200,80);
add(jieguo);
conti.addActionListener(this);
exit.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==conti) {
this.dispose();
new JsqFrm();
}
if (e.getSource()==exit) {
System.exit(1);
}
}
public double plus(double x,double y) {
return x+y;
}
public double sub(double x,double y) {
return x-y;
}
public double mul(double x,double y) {
return x*y;
}
public double div(double x,double y) {
return x/y;
}
}
class ErrorWindow extends JFrame implements ActionListener {
JLabel errorinfo=new JLabel("输入的内容不能为空!");
JButton jreturn=new JButton("返回");
public ErrorWindow (String s) {
super(s);
setBounds(400,200,300,200);
setVisible(true);
setLayout(null);
errorinfo.setBounds(80,30,150,60);
add(errorinfo);
jreturn.setBounds(100,100,90,30);
add(jreturn);
jreturn.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jreturn) {
this.dispose();
new JsqFrm();
}
}
}
自己写的,你看看把!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Jsq {
public static void main(String args[]) {
new JsqFrm();
}
}
class JsqFrm extends JFrame implements ActionListener {
JButton plu=new JButton("加法");
JButton sub=new JButton("减法");
JButton mul=new JButton("乘法");
JButton div=new JButton("除法");
public JsqFrm() {
super("计算器");
setBounds(250,180,360,280);
setVisible(true);
setLayout(null);
plu.setBounds(40,80,60,35);
add(plu);
sub.setBounds(110,80,60,35);
add(sub);
mul.setBounds(180,80,60,35);
add(mul);
div.setBounds(250,80,60,35);
add(div);
plu.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==plu) {
this.dispose();
new MainFrm("加法界面","加数1","加数2","算法1");
}
if (e.getSource()==sub) {
this.dispose();
new MainFrm("减法界面","减数1","减数2","算法2");
}
if (e.getSource()==mul) {
this.dispose();
new MainFrm("乘法界面","乘数1","乘数2","算法3");
}
if (e.getSource()==div) {
this.dispose();
new MainFrm("除法界面","除数1","除数2","算法4");
}
}
}
class MainFrm extends JFrame implements ActionListener { //ActionListener为实现动作监听的接口
JTextField textone=new JTextField(10);
JTextField texttwo=new JTextField(10);
JLabel numone;
JLabel numtwo;
JButton Sure=new JButton("确定");
JButton Cancel=new JButton("取消");
String num;
public MainFrm(String s,String one,String two,String i) {
super(s);
num=i;
setBounds(250,180,320,280);
setLayout(null);
numone=new JLabel(one);
numtwo=new JLabel(two);
numone.setBounds(60,40,60,40);
add(numone);
textone.setBounds(100,40,130,40);
add(textone);
numtwo.setBounds(60,80,60,40);
add(numtwo);
texttwo.setBounds(100,80,130,40);
add(texttwo);
Sure.setBounds(50,150,70,45);
add(Sure);
Cancel.setBounds(180,150,70,45);
add(Cancel);
setVisible(true);
Sure.addActionListener(this);
Cancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) { //actionPerformed为ActionListener接口的一个方法,用于接受监听事件
if (e.getSource()==Sure) {
this.dispose();
if (textone.getText().trim().equals("")||texttwo.getText().trim().equals("")) {
new ErrorWindow("错误");
}
else
if (num=="算法1") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法1");
}
if (num=="算法2") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法2");
}
if (num=="算法3") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法3");
}
if (num=="算法4") {
new JsWindow("结果",textone.getText().trim(),texttwo.getText().trim(),"算法4");
}
}
if (e.getSource()==Cancel) {
System.exit(1);
}
}
}
class JsWindow extends JFrame implements ActionListener {
JLabel jieguo;
JButton conti=new JButton("继续运算");
JButton exit=new JButton("退出");
public JsWindow(String s,String one,String two,String i) {
super(s);
setBounds(250,180,320,280);
setLayout(null);
conti.setBounds(100,100,90,30);
add(conti);
exit.setBounds(100,150,90,30);
add(exit);
double in=Double.parseDouble(one);
double jn=Double.parseDouble(two);
if (i=="算法1") {
jieguo=new JLabel("计算结果为:"+plus(in,jn));
}
if (i=="算法2") {
jieguo=new JLabel("计算结果为:"+sub(in,jn));
}
if (i=="算法3") {
jieguo=new JLabel("计算结果为:"+mul(in,jn));
}
if (i=="算法4") {
jieguo=new JLabel("计算结果为:"+div(in,jn));
}
jieguo.setBounds(90,15,200,80);
add(jieguo);
conti.addActionListener(this);
exit.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==conti) {
this.dispose();
new JsqFrm();
}
if (e.getSource()==exit) {
System.exit(1);
}
}
public double plus(double x,double y) {
return x+y;
}
public double sub(double x,double y) {
return x-y;
}
public double mul(double x,double y) {
return x*y;
}
public double div(double x,double y) {
return x/y;
}
}
class ErrorWindow extends JFrame implements ActionListener {
JLabel errorinfo=new JLabel("输入的内容不能为空!");
JButton jreturn=new JButton("返回");
public ErrorWindow (String s) {
super(s);
setBounds(400,200,300,200);
setVisible(true);
setLayout(null);
errorinfo.setBounds(80,30,150,60);
add(errorinfo);
jreturn.setBounds(100,100,90,30);
add(jreturn);
jreturn.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jreturn) {
this.dispose();
new JsqFrm();
}
}
}
自己写的,你看看把!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询