導航:首頁 > 源碼編譯 > java編寫一個除法演算法的程序

java編寫一個除法演算法的程序

發布時間:2024-06-24 21:18:24

Ⅰ 編寫程序來執行兩個數字12和5的算術加法,乘法和除法(java程序)求源代碼

double a = 12.0;
double b = 5.0;
int addtion_result = 0;//加法計算結果
int substraction_result = 0;//減法計算結果毀悔
int multipliction_result = 0;//乘法計算結果
double divide_result = 0.0;//除法計算結果

//加法
addtion_result = (int)(a + b);

/羨余伍兄或/減法
substraction_result = (int)(a - b);

//乘法
multipliction_result = (int)(a * b) ;

//除法
divide_result = a / b;

Ⅱ java緙栫▼錛氱紪鍐欏簲鐢ㄧ▼搴忥紝浠庡懡浠よ岃緭鍏ヤ袱涓灝忔暟鍙傛暟錛屾眰瀹冧滑鐨勫晢銆傝佹眰紼嬪簭涓鎹曟崏NumberFo

鍒嗘瀽棰樼洰

NumberFormatException錛屾暟瀛楁牸寮忓紓甯革紝甯歌佷簬鐢卞瓧絎︿覆杞鎹㈠埌鏁板瓧鐨勬椂鍊欎細鍑虹幇

姣斿俛bc2.0--杞鎹㈡垚鏁板瓧鏃訛紝緋葷粺灝變細鎶涘嚭榪欎釜寮傚父銆

ArithmeticException 錛岄櫎0寮傚父銆傝繖涓寮傚父甯歌佷簬鏁存暟闄ゆ暣鏁幫紝濡傛灉琚闄ゆ暟鏄0錛岄偅涔堜細鎶涘嚭榪欎釜寮傚父銆備絾鏄鎻愮ず瑕佹眰鏄灝忔暟闄ゅ皬鏁般俤ouble灝忔暟灝辯畻鏄0錛岄偅涔堝湪瀛樺偍鐨勬椂鍊欎篃涓嶆槸綆鍗曠殑0錛岃屽彲鑳芥槸0.00000000001絳夋儏鍐碉紝閭d箞闄ら浂寮傚父灝變笉浼氳鎶涘嚭銆傛垜浠鍙浠ヨ嚜宸辨墜鍔ㄥ垽鏂琚闄ゆ暟鏄鍚︾瓑浜0錛屽傛灉絳変簬0錛岄偅涔堟垜浠鎵嬪姩鎶涘嚭璇ュ紓甯革紝鐒跺悗榪涜屾崟鑾

鍙傝冧唬鐮佷竴錛氱畝媧佺増

importjava.util.Scanner;

publicclassDivDemoSp{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
while(true){
try{
System.out.println("杈撳叆闄ゆ暟");
doubled1=Double.parseDouble(sc.nextLine());
System.out.println("杈撳叆琚闄ゆ暟");
doubled2=Double.parseDouble(sc.nextLine());
if(d2==0){
thrownewArithmeticException("鎹曡幏鍒伴櫎0寮傚父~~錛侊紒");//鎵嬪姩鎶涘嚭闄ら浂寮傚父
}
doubled3=d1/d2;
System.out.println("璁$畻緇撴灉錛"+d1+"/"+d2+"="+d3);
break;//濡傛灉鑳芥e父榪愯屽埌榪欓噷錛岃存槑鍓嶉潰鐨勮緭鍏ラ兘鏄姝g『鐨勶紝鎵浠ュ彲浠ヨ煩鍑哄驚鐜浜
}catch(NumberFormatExceptione1){//鎹曡幏鏁板瓧鏍煎紡寮傚父
System.out.println("鎹曡幏鍒版暟瀛楁牸寮忓紓甯");
continue;
}catch(ArithmeticExceptione2){//鎹曡幏鍒伴櫎闆跺紓甯
System.out.println(e2.getMessage());
continue;
}

}
}
}

嫻嬭瘯浠g爜

杈撳叆闄ゆ暟
12
杈撳叆琚闄ゆ暟
0
鎹曡幏鍒伴櫎0寮傚父~~錛侊紒
杈撳叆闄ゆ暟
12
杈撳叆琚闄ゆ暟
agc
鎹曡幏鍒版暟瀛楁牸寮忓紓甯
杈撳叆闄ゆ暟
12
杈撳叆琚闄ゆ暟
0.5
璁$畻緇撴灉錛12.0/0.5=24.0

鍙傝冧唬鐮佷簩錛

publicclassDivDemo{
staticScannersc=newScanner(System.in);//瀹氫箟涓涓鎵鎻忓櫒錛岀敤浜庤誨彇杈撳叆淇℃伅

publicstaticvoidmain(String[]args){//main鏂規硶
div();//璋冪敤闄ゆ硶鏂規硶
}

//div鏂規硶錛岀敤浜庡緱鍒2涓灝忔暟錛屽苟涓旇繘琛岄櫎娉曡繍綆楋紝濡傛灉琚闄ゆ暟涓0.鎶涘嚭闄ら浂寮傚父
staticvoiddiv(){
doubled1=getNum("杈撳叆闄ゆ暟");//璋冪敤杈撳叆鏁板瓧鐨勬柟娉
doubled2=getNum("杈撳叆琚闄ゆ暟");
try{
if(d2==0){//濡傛灉琚闄ゆ暟絳変簬0錛岄偅涔堟姏鍑洪櫎闆跺紓甯
thrownewArithmeticException("鎹曡幏鍒伴櫎闆跺紓甯!!!");
}
doubled3=d1/d2;
System.out.println("璁$畻緇撴灉錛"+d1+"/"+d2+"="+d3);
//div();//鍙浠ラ噸鏂拌皟鐢╠iv鏂規硶錛屽啀嬈¤繘琛岃緭鍏
}catch(ArithmeticExceptionexception){//鎹曡幏鍒伴櫎闆跺紓甯
System.out.println(exception.getMessage());//鎵撳嵃鍑哄紓甯鎬俊鎮
//div();//褰撴崟鑾峰紓甯稿悗錛屼篃鍙浠ラ噸鏂拌皟鐢╠iv鏂規硶錛屽啀嬈¤繘琛岃緭鍏
}
}
//getNum鏂規硶錛岀敤浜庡緱鍒頒竴涓鏁板瓧
staticdoublegetNum(Stringtips){
doublednum;//瀹氫箟涓涓灝忔暟鍙橀噺
while(true){
try{
System.out.println(tips);//鏄劇ず鎻愮ず鏂囧瓧
Strings1=sc.nextLine();//璇誨彇杈撳叆鐨勪竴琛屼俊鎮
dnum=Double.parseDouble(s1);//濡傛灉鍙浠ユ妸杈撳叆鐨勪俊鎮杞鎹㈡垚鏁板瓧灝辮煩鍑哄驚鐜錛
//濡傛灉鏃犳硶鎶婁俊鎮杞鎴愭暟瀛楀氨浼氭姤鏁板瓧鏍煎紡寮傚父
break;//璺沖嚭寰鐜
}catch(){//鎹曡幏鏁板瓧鏍煎紡寮傚父
System.out.println("鎹曡幏鍒頒簡鏁板瓧鏍煎紡閿欒寮傚父!!!");//鎻愮ず鏂囧瓧錛屽苟涓旂戶緇寰鐜
}
}
returndnum;//榪斿洖鏁板瓧
}
}

榪愯屾祴璇

杈撳叆闄ゆ暟
1.0
杈撳叆琚闄ゆ暟
0
鎹曡幏鍒伴櫎闆跺紓甯!!!

杈撳叆闄ゆ暟
1.0abc
鎹曡幏鍒頒簡鏁板瓧鏍煎紡閿欒寮傚父!!!
杈撳叆闄ゆ暟
1.0
杈撳叆琚闄ゆ暟
0.5
璁$畻緇撴灉錛1.0/0.5=2.0

Ⅲ java:編寫一個計算器小程序,要求可以做加減乘除運算

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //當前運算的運算符
private boolean firstDigit = true; // 標志用戶按的是否是整個表達式的第一個數字,或者是運算符後的第一個數字
private double resultNum = 0.0; // 計算的中間結果
private boolean operateValidFlag = true; //判斷操作是否合法
public Calculator()
{
super("Calculator");
this.setBounds(300, 300, 300, 300);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//設置布局
text_double=new JTextField("0",20);//設置文本區
text_double.setHorizontalAlignment(JTextField.RIGHT);//設置水平對齊方式未右對齊
this.getContentPane().add(text_double,BorderLayout.NORTH);//將文本區添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在內容窗口添加一個網格布局
this.getContentPane().add(panel);//添加panel面板
for(int i=0;i<button.length;i++)//在面板上添加按鈕
panel.add(button[i]);

for(int i=0;i<button.length;i++)
button[i].addActionListener(this);//為按鈕注冊
text_double.setEditable(false);//文本框不可編輯
text_double.addActionListener(this);//

this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand();//返回與此動作相關的命令字元串。
System.out.println("##########command is "+c);
if(c.equals("C")){
handleC(); //用戶按了「C」鍵
}
else if (c.equals("CE")) // 用戶按了"CE"鍵
{
text_double.setText("0");
}
else if ("0123456789.".indexOf(c) >= 0) // 用戶按了數字鍵或者小數點鍵
{
handleNumber(c); // handlezero(zero);
} else //用戶按了運算符鍵
{
handleOperator(c);
}
}
private void handleC() // 初始化計算器的各種值
{
text_double.setText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//輸入的第一個數字
{
text_double.setText(button);
} else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))//輸入的是小數點,並且之前沒有小數點,則將小數點附在結果文本框的後面
//如果字元串參數作為一個子字元串在此對象中出現,則返回第一個這種子字元串的第一個字元的索引;如果它不作為一個子字元串出現,則返回 -1
{
text_double.setText(text_double.getText() + ".");
} else if (!button.equals("."))// 如果輸入的不是小數點,則將數字附在結果文本框的後面
{
text_double.setText(text_double.getText() + button);
}
// 以後輸入的肯定不是第一個數字了
firstDigit = false;
}
private void handleOperator(String button) {

if (operator.equals("/")) {
// 除法運算
// 如果當前結果文本框中的值等於0
if (getNumberFromText() == 0.0){
// 操作不合法
operateValidFlag = false;
text_double.setText("除數不能為零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")){
// 加法運算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
// 減法運算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
// 乘法運算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// 平方根運算
if(getNumberFromText()<0){
operateValidFlag = false;
text_double.setText("被開方數不能為負數");}
else
resultNum = Math.sqrt(resultNum);
}
else if (operator.equals("+/-")){
// 正數負數運算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
// 賦值運算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 雙精度浮點數的運算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_double.setText(String.valueOf(t1));
} else {
text_double.setText(String.valueOf(resultNum));
}
}
operator = button; //運算符等於用戶按的按鈕
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //從結果的文本框獲取數字
{
double result = 0;
try {
result = Double.valueOf(text_double.getText()).doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 實例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}

Ⅳ java:輸入兩個正整數m和n,求其最大公約數和最小公倍數。 程序分析:利用輾除法。

程序運行截圖:

publicclassTest{

publicstaticfinalvoidmain(String[]args){
System.out.println("請輸入兩個正整數");
System.out.print("第一個正整數:");
Scannerscanner=newScanner(System.in);
Stringone=scanner.nextLine();
inta=Integer.valueOf(one);
System.out.print("第二個正整數:");
Stringtwo=scanner.nextLine();
intb=Integer.valueOf(two);
System.out.println("a="+a+",b="+b);
//求最大公約數
//1.a÷b,令temp為所得余數(0≤r<b)
//若temp=0,演算法結束;b即為答案。
//2.互換:置a←b,b←temp,並返回第一步。
inttemp,num1=a,num2=b;
if(a<b){
//互換位置
temp=b;
b=a;
a=temp;
}
System.out.println("a="+a+",b="+b);
while(b!=0){/*利用輾除法,直到b為0為止*/
temp=a%b;
a=b;
b=temp;
}
System.out.println("最大公約數:"+a);
//求最小公倍數
System.out.println("最小公倍數:"+num1*num2/a);
}
}

Ⅳ 用java編寫程序,輸入兩個正整數,利用輾轉相除法求兩個整數的最大公約數和最小公倍數

自然語言描述

計算兩個非負整數p 和q 的最大公約數:若

q 是0,則最大公約數為p。否則,將p 除以

q 得到余數r,p 和q 的最大公約數即為q 和

r 的最大公約數。

Java code 求公約數

publicstaticintgcd(intp,intq)
{
if(q==0)returnp;
intr=p%q;
returngcd(q,r);
}


公倍數就是兩個數的積除以最大公約數。

publicstaticintg(intp,intq)
{
returnp*q/gcd(q,r);
}
閱讀全文

與java編寫一個除法演算法的程序相關的資料

熱點內容
明朝那些事pdf 瀏覽:727
mysql命令查看錶 瀏覽:531
阿里雲伺服器登錄密碼忘了怎麼辦 瀏覽:495
linuxwwwdata 瀏覽:992
用蘋果終端java編譯 瀏覽:364
安卓怎麼拼接截屏 瀏覽:17
如何從安卓手機上修改wifi密碼 瀏覽:226
安卓12一加如何升級 瀏覽:86
冒險小虎隊pdf下載 瀏覽:542
程序員黑話今晚加班 瀏覽:930
androidgetmethod 瀏覽:798
編程輸出1112 瀏覽:792
加密貨幣礦怎麼樣 瀏覽:212
信息流優化師和程序員 瀏覽:101
pythonkivy框架 瀏覽:734
xshell怎樣返回上一條命令 瀏覽:490
python函數內存機制 瀏覽:829
手機國際網路伺服器地址 瀏覽:398
安卓系統投影儀怎麼連接外接設備 瀏覽:816
陰線向下接進20日均線選股源碼 瀏覽:433