導航:首頁 > 編程語言 > java乘法

java乘法

發布時間:2022-01-19 13:41:42

java乘法運算

//早前寫的,簡易的!
importjava.util.*;
publicclassTestSum{
staticScannersc=newScanner(System.in);

publicstaticvoidmain(String[]args){
while(true){
init();
}
}
privatestaticvoidinit(){
System.out.println("請輸入算數表達式如:1+1回車即可:");
Stringstr=sc.nextLine();
if(!str.matches("[^a-zA-Z()&^#$@!~]+")){
System.out.println("輸入錯誤重輸:請輸入算數表達式如:1+1回車即可:");
str=sc.nextLine();
}
intq=0,h=0,sum=0;
charchs='';
for(inti=0;i<str.length();i++){
chs=str.charAt(i);
if(chs=='+'||chs=='-'||chs=='*'||chs=='/'){
q=Integer.valueOf(str.substring(0,i));
h=Integer.valueOf(str.substring(i+1,str.length()));
switch(chs){
case'+':
sum=q+h;
break;
case'-':
sum=q-h;
break;
case'*':
sum=q*h;
break;
default:
sum=q/h;
break;
}
break;
}
}
System.out.println("結果="+sum+" 繼續下一輪:");
}
}

Ⅱ java的加減乘除運算

使用BigDecimal並且一定要用String來夠造。
實現方法如下:

import java.math.BigDecimal;

/**

* 由於Java的簡單類型不能夠精確的對浮點數進行運算,這個工具類提供精

* 確的浮點數運算,包括加減乘除和四捨五入。

*/

public class Arith{

//默認除法運算精度

private static final int DEF_DIV_SCALE = 10;

//這個類不能實例化

private Arith(){

}

/**

* 提供精確的加法運算。

* @param v1 被加數

* @param v2 加數

* @return 兩個參數的和

*/

public static double add(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.add(b2).doubleValue();

}

/**

* 提供精確的減法運算。

* @param v1 被減數

* @param v2 減數

* @return 兩個參數的差

*/

public static double sub(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.subtract(b2).doubleValue();

}

/**

* 提供精確的乘法運算。

* @param v1 被乘數

* @param v2 乘數

* @return 兩個參數的積

*/

public static double mul(double v1,double v2){

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.multiply(b2).doubleValue();

}

/**

* 提供(相對)精確的除法運算,當發生除不盡的情況時,精確到

* 小數點以後10位,以後的數字四捨五入。

* @param v1 被除數

* @param v2 除數

* @return 兩個參數的商

*/

public static double div(double v1,double v2){

return div(v1,v2,DEF_DIV_SCALE);

}

/**

* 提供(相對)精確的除法運算。當發生除不盡的情況時,由scale參數指

* 定精度,以後的數字四捨五入。

* @param v1 被除數

* @param v2 除數

* @param scale 表示表示需要精確到小數點以後幾位。

* @return 兩個參數的商

*/

public static double div(double v1,double v2,int scale){

if(scale<0){

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供精確的小數位四捨五入處理。

* @param v 需要四捨五入的數字

* @param scale 小數點後保留幾位

* @return 四捨五入後的結果

*/

public static double round(double v,int scale){

if(scale<0){

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(Double.toString(v));

BigDecimal one = new BigDecimal("1");

return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

};

Ⅲ java編寫中文乘法表

兄弟,搞定了,復制直接用,放心,沒病毒。
package importcsv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.HashMap;
import java.util.Stack;

public class DoSomeThing {
public static void main(String[] args) {
DoSomeThing re = new DoSomeThing();

int a = 0, b = 0, c = 0;
for (int i = 0; i < 9; i++) {
a = i + 1;
for (int j = 0; j < i + 1; j++) {
b = j + 1;
c = a * b;
System.out.print(re.rework(a) + "" + re.rework(b) + "得" + re.rework(c) + " ");
}
System.out.println(" ");
}
}
public Stack<Integer> transfer(int n) {
Stack<Integer> st = new Stack<Integer>();
int division = 0; // 余數
while (n >= 10) {
division = n % 10;
st.push(division);
n = n / 10;
}

st.push(n); // 將最高位壓棧

return st;
}

public String rework(Integer num) {
DoSomeThing tf = new DoSomeThing();
Stack<Integer> s = tf.transfer(num);
/*
* while(!s.empty()){ System.out.print(s.pop()); //測試語句 }
*/

HashMap<Integer, String> hp1 = new HashMap<Integer, String>(); // 第一個映射表
hp1.put(0, "零"); // 根據所在位的數值與中文對應
hp1.put(1, "一");
hp1.put(2, "二");
hp1.put(3, "三");
hp1.put(4, "四");
hp1.put(5, "五");
hp1.put(6, "六");
hp1.put(7, "七");
hp1.put(8, "八");
hp1.put(9, "九");

HashMap<Integer, String> hp2 = new HashMap<Integer, String>(); // 第二個映射表
hp2.put(2, "十"); // 根據所在位數,與中文對應
hp2.put(3, "百");
hp2.put(4, "千");
hp2.put(5, "萬");
hp2.put(6, "十萬");
hp2.put(7, "百萬");
hp2.put(8, "千萬");
hp2.put(9, "億");

String out = "";
while (!s.isEmpty()) {
int temp = s.pop();

if (s.size() == 0) {
if (temp != 0) {
out = out + hp1.get(temp);
}
} else {
if (temp == 0) {
out = out + hp1.get(temp);
} else {
out = out + hp1.get(temp) + hp2.get(s.size() + 1);
}
}
}
return out;
}
}

Ⅳ java中如何算double類型的乘法

java中dobule類型的運算方法與int和float的一樣。
代碼示例:
double s1=0.23;
dobule s2=5.2;
dobule s3=s1*s2;
相關知識:java的運算方法和運算符。
加+,減-,乘以*,除以/,取余%。java的運算方式也是遵守先乘除後加減,有括弧先算括弧里的運算方式。

Ⅳ java九九乘法表

println 改成 print
System.out.print(i + "*" + j + "=" + i*j + "\t");

Ⅵ JAVA 乘法口訣表

//反這來輸出

public static void main(String[] args) {
for(int i=9;i>0;i--) {
for (int j = 1; j <= i; j++) { //j<=i是確保列小於或等於行數
System.out.print(i + "x" + j + "=" + i * j+' '); //' '的意思是強制水平製表
}
System.out.println(); //這一行的意思循環結束就換行
}
}

Ⅶ JAVA 兩個數相乘怎麼寫

public class Day25B {

public static void main(String[] args) {

int[] arr1=new int[5],arr2=new int[5],result=new int[5];

for (int i = 0; i < result.length; i++) {

arr1[i]=(int)Math.round(Math.random()*40+10);

arr2[i]=(int)Math.round(Math.random()*40+10);

result[i]=arr1[i]*arr2[i];

System.out.println("索引\tarr1\tarr2\tresult");

for (int i = 0; i < result.length; i++) {

System.out.println(i+"\t"+arr1[i]+" x "+arr2[i]+" =\t"+result[i]);

(7)java乘法擴展閱讀:

javap 類文件反匯編器數據類型boolean 布爾型

byte 位元組型

char 字元型

short 短整型

int 整形

long 長整形

float 單精度浮點型

double 雙精度浮點型class 類null 空類型

interface 介面

Ⅷ java 九九乘法表

public class Multiply99{
public static void main(String[] args) {
//修改1,把j改成了i
int j,i,result;
String space;
for(i=1;i<10;i++){
//修改2,把10改成了i+1
for(j=1;j<i+1;j++){
result=i*j;
if(result>=10||j==1){
space=" ";
}else{
space=" ";
}
System.out.print(i + "*"+j + "=" +result + space);
}
System.out.print('\n');
}
}
}

Ⅸ 如何使用java實現加減乘除運算

publicclasstest{publicstaticvoidmain(String[]args){inta=5;intb=1;intc=a+b;intd=a-b;inte=a*b;intf=a/c;System.out.println(c,d,e,f);}}

Ⅹ java 怎麼算乘法

packageCMJqimo;
importjava.util.Random;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.*;
publicclasstest{
staticinttrueresult=0;
publicstaticvoidmain(Stringargs[]){
newtest();
}
publictest(){
JPaneljp=newJPanel();
JLabeljl=newJLabel();
JTextFieldjtf;
JFramejf=newJFrame("ExamofMultiplication");
JButtonjb=newJButton();
ContainercontentPane=jf.getContentPane();
contentPane.add(jp);
contentPane.setLayout(newBorderLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300,200);
jp.setLayout(newFlowLayout());
jp.setBackground(Color.GREEN);
jp.setSize(1000,1000);
intnum1=random_number();
intnum2=random_number();
trueresult=num1*num2;
JLabeljll=newJLabel(num1+"x"+num2+"=");
jp.add(jll);
jtf=newJTextField(5);
jp.add(jtf);
jb=newJButton("提交");
jp.add(jb);
jl=newJLabel("");
jp.add(jl);
contentPane.add(jp);
jf.setLocation(400,200);
jf.setVisible(true);
jb.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
intinput=Integer.valueOf(jtf.getText());
Strings=output_result(trueresult,input);
JOptionPane.showMessageDialog(jp,s);
inta=random_number();
intb=random_number();
trueresult=a*b;
jll.setText(a+"x"+b+"=");
jtf.setText("");
}
});
}
publicstaticintread_input(Strings){
returnInteger.parseInt(s);
};
publicstaticintrandom_number(){
Randomr=newRandom();
intnum=r.nextInt(10)+1;
returnnum;
}
publicStringoutput_result(inttrueresult,intinput){
Randomr=newRandom();
if(input==trueresult){
String[]s={"Verygood","Excellent","Greatjob"};
returns[r.nextInt(3)];
}else{
return"No,pleasetryagain";
}
}
}

閱讀全文

與java乘法相關的資料

熱點內容
明日之後為什麼有些伺服器是四個字 瀏覽:100
安卓系統l1是什麼意思 瀏覽:21
伺服器一直崩應該用什麼指令 瀏覽:916
cm202貼片機編程 瀏覽:724
php構造函數帶參數 瀏覽:175
解壓電波歌曲大全 瀏覽:336
為啥文件夾移到桌面成word了 瀏覽:858
命令符的安全模式是哪個鍵 瀏覽:758
編程中學 瀏覽:956
單片機求助 瀏覽:993
ug加工側面排銑毛坯怎麼編程 瀏覽:271
程序員有關的介紹 瀏覽:736
支付寶使用的什麼伺服器 瀏覽:210
安卓看本地書用什麼軟體好 瀏覽:921
經傳軟體滾動凈利潤指標源碼 瀏覽:522
螢石雲視頻已加密怎麼解除 瀏覽:574
一命令四要求五建議 瀏覽:30
qq文件夾遷移不了 瀏覽:19
液體粘滯系數測定不確定度演算法 瀏覽:332
輕棧源碼 瀏覽:426