本文目錄一覽:
JAVA的加,減,乘,除運算方法
首先可以把計算器看成是一個對象就是一個類,它有加、減、乘、除功能,這四個就是這個類的方法;你可以給這個類定義兩個成員變量
int
x、int
y
然後分別用這四個方法對x
、y
實行加、減、乘、除並返回其值
代碼大概如下:
class
counter{
private
int
x;
private
int
y;
public
counter(){
}
public
counter(int
x,int
y){
this.x=x;
this.y=y;
}
public
double
adding(){
//加運算
return
x+y;
}
public
double
minus(){
//減運算
return
x-y;
}
public
double
times(){
//乘運算
return
x*y;
}
public
double
divide(){
//除運算
return
x/y;
}
}
//測試類
public
class
test{
public
static
void
main(string[]
args){
counter
c=new
counter(5,4);//實例化
system.out.println(c.adding());//輸出加的結果
system.out.println(c.minus());//輸出減的結果
system.out.println(c.times());//輸出乘的結果
system.out.println(c.divide());//輸出除的結果
}
}
希望對你有幫助
java 中實現複數的加減
(1):具體代碼(附註釋)
複數類:
public class Complex {
private float shibu;
private float xubu;
Complex()
{this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}
public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print(“加法結果為:”+result.shibu+”+”+result.xubu+”i”);
}
public void Sub(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print(“加法結果為:”+result.shibu+”+”+result.xubu+”i”);
}
public void Mul(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu+p.shibu*this.xubu;
System.out.print(“乘法結果為:”+result.shibu+”+”+result.xubu+”i”);
}
public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一個例子:
源代碼:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println(“請輸入數據:”);
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“請輸入第一個複述的實部:”);
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
System.out.println(“請輸入第一個複述的虛部:”);
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
System.out.println(“請輸入第二個複述的實部:”);
try{
s = in.readLine();
c =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
System.out.println(“請輸入第二個複述的虛部:”);
try{
s = in.readLine();
d =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
Book h;
h=new Book(a,b);
Book j;
j=new Book(c,d);
System.out.println(“您輸入的一個數為:”);
toString(h);
System.out.println(“您輸入的二個數為:”);
toString(j);
Book k;
k=new Book();
char z=’y’;
do{
System.out.println(“請選擇您要進行的計算:”);
System.out.println(“1 :進行加法運算”);
System.out.println(“2 :進行減法運算”);
System.out.println(“3 :進行修改”);
System.out.println(“4 :進行乘法運算”);
System.out.println(“5 :進行除法運算”);
System.out.println(“6 :查看修改結果”);
int i=0;
try{
i= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
switch(i)
{
case 1:
k.sb=jia(h.sb,j.sb);
k.xb=jia(h.xb,j.xb);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 2:
k.sb=jian(h.sb,j.sb);
k.xb=jian(h.xb,j.xb);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 3:
System.out.println(“請輸入您要修改哪個實數:”);
int l=0;
try{
l= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break ;
case 4:
double f=0;
double e=0;
f=cheng(h.sb,j.sb)+cheng(h.xb,j.xb);
e=cheng(h.sb,j.xb)+cheng(h.xb,j.sb);
k.sb=(double)(Math.round(f*100)/100.0);
k.xb=(double)(Math.round(e*100)/100.0);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 5:
double chushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
double beichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
double beichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));
k.sb=chu(beichushus,chushu);
k.xb=chu(beichushux,chushu);
System.out.println(“計算結果的實部為:”+k.sb);
System.out.println(“計算結果的虛部為:”+k.xb);
toString(k);
break ;
case 6:
System.out.println(“修改後的結果為:”);
System.out.println(“第一個複數:”+toString(h));
System.out.println(“第二個複數:”+toString(j));
break ;
}
System.out.println(“請問您是否還要繼續 y/n:”);
try{
z=(char)System.in.read();
System.in.skip(2); //忽略回車換行
}
catch(IOException e){}
} while(z==’y’);
}
public static double gets(Book a){
return a.sb;
}
public static double getx(Book b){
return b.xb;
}
public static double xiugais(Book a)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“請輸入您要修改的實部:”);
double m=0;
try{
m= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
a.sb=m;
System.out.println(“修改成功:”);
return 0;
}
public static double xiugaix(Book b)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“請輸入您要修改的虛部:”);
double n=0;
try{
n= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println(“拋擲異常”);}
b.xb=n;
System.out.println(“修改成功:”);
return 0;
}
public static double jia(double a,double b)//
{
double c=0;
c=a+b;
System.out.println(“加法成功:”);
return c ;
}
public static double jian(double a,double b)
{
double c=0;
c=a-b;
System.out.println(“減法成功:”);
return c;
}
public static double cheng(double a,double b)
{
double c=0;
c=a*b;
System.out.println(“乘法成功:”);
return c;
}
public static double chu(double a,double b)
{
double d=0;
double c=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println(“除法成功:”);
return c ;
}
public static double toString(Book a){
System.out.println(“結果為:”+a.sb+”+”+a.xb+”*i”);
return 0;
}
}
(3)測試結果截圖:
加法的Java 中的加法
Java中的加法很簡答,使用運算符+表示。例如: publicclassadd{publicstaticvoidmain(String[]args){intx=5;inty=7;intsum=x+y;System.out.println(“x和y的和為”+sum);}}上述代碼聲明了整數x和整數y,並分別賦值了5和7。最後在聲明sum,賦值為x+y,並最終輸出。
最終輸出結果為12。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/154737.html