编写一个程序,用户输入两个数,求其加减乘除,并用消息框显示计算结果。

代码:

//an complex program

import javax.swing.JOptionPane;//import class JOptionPane

public class Complex {

public static void main(String[] args) {

// TODO 自动生成的方法存根

String firstNumber,     //first string entered by user

secondNumber;    //second string entered by user

int number1,            //first number to add

number2,            //second number to add

sum,                //sum of number1 and number2

difference,         //difference of number1 and number2

product,            //product of number1 and number2

quotient;           //quotient of number1 and number2

// read in first number from user as a string

firstNumber = JOptionPane.showInputDialog("Enter first integer ");

//read in second number from user as a string

secondNumber = JOptionPane.showInputDialog("Enter second integer ");

//convert number from type string to type int

number1 = Integer.parseInt(firstNumber);

number2 = Integer.parseInt(secondNumber);

//add the numbers

sum = number1 + number2;

//difference the numbers

difference = number1-number2;

//product the numbers

product = number1 * number2;

//quotient the numbers

quotient = number1/number2;

//display the results

JOptionPane.showMessageDialog(

null,"The Sum is " + sum,"Results",JOptionPane.PLAIN_MESSAGE);

JOptionPane.showMessageDialog(

null,"The difference is " + difference,"Results",JOptionPane.PLAIN_MESSAGE);

JOptionPane.showMessageDialog(

null,"The product is " + product,"Results",JOptionPane.PLAIN_MESSAGE);

JOptionPane.showMessageDialog(

null,"The quotient is " + quotient,"Results",JOptionPane.PLAIN_MESSAGE);

System.exit(0);//terminate the program

}

}

      

时间: 2024-08-08 17:56:18

编写一个程序,用户输入两个数,求其加减乘除,并用消息框显示计算结果。的相关文章

编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果

编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果 import javax.swing.JOptionPane; public class Test{ public static void main(String[] args) { int n1=Integer.parseInt(JOptionPane.showInputDialog("Input number 1: ")); int n2=Integer.parseInt(JOptionPane.showInpu

编程:用户输入两个数,求出其加减乘除,并用消息框显示计算结果

import java.util.Scanner;import javax.swing.JOptionPane;public class FourOperationsOfTwoIntegers {public static void main(String[] args){ String firstNumber, // first string entered by usersecondNumber; // second string entered by userDouble number1,

c语言:编写一个程序,输入a,b,c三个值,输出其中最大者

程序: //编写一个程序,输入a,b,c三个值,输出其中最大者 #include<stdio.h> int main() { int a,b,c,max; printf("请输入三个数:"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max<b) { max=b; } if (max<c) { max=c; } printf("%d\n",max); retur

案例四:银行提供了整存整取定期储蓄业务,其存期分为一年、两年、三年、五年,到期凭存单支取本息。年利率如下表。试编写一个程序,输入存入的本金数目,计算假设存一年、两年、三年、五年,到期取款时,银行应支付的本息分别是多少。

年利率 存期 年利率 一年 2.25% 两年 2.7% 三年 3.24% 五年 3.6% package project_04; import java.util.Scanner; /** * 2018-9-7 21:42:25 * @author Suaron XiMen * */ public class Interest { public static void main(String[] args) { //计算银行存款本息 Scanner input=new Scanner(Syste

编写一个程序,输入a、b、c三个值,输出其中最大值。

题目描述 输入 一行数组,分别为a b c 输出 a b c其中最大的数 样例输入 10 20 30 样例输出 30 程序:#include<stdio.h> int main(){    int arr[3],i,max=0;     for(i=0;i<3;i++)       scanf("%d",&arr[i]);    for(i=0;i<3;i++){        if(arr[i]>max)        max=arr[i];  

编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数

#include <stdio.h> int main() {      char a=0;     int num_count=0;     int space_count=0;     int other_count=0;                                                  //注意此处,不能写成a=getchar(),然后while(a!='\n'),这样做只能输入一行,然后进行死循环      while((a=getchar())!='\

【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。

#include <stdio.h> int main() { char s[20]; char num=0; int num_count=0; int space_count=0; int other_count=0; while((num=getchar())!='\n') {  if(num>='0'&&num<='9')           {              num_count++;           }           else if(n

编写一个程序统计输入字符串中:各个数字,空白字符,以及其他所有字符常出现的次数。

java 优化版 用接口实现(输入两个数选择实现加减乘除运算)

//利用java接口实现计算器,实现加减乘除的功能 import java.util.Scanner; class Test { public static void main(String[] args) { fun i = new fun(); jiafa s1 = new jiafa(); jianfa s2 = new jianfa(); chengfa s3 = new chengfa(); chufa s4 = new chufa(); System.out.println("请输入