首先可以把計(jì)算器看成是一個(gè)對(duì)象就是一個(gè)類(lèi),它有加、減、乘、除功能,這四個(gè)就是這個(gè)類(lèi)的方法;你可以給這個(gè)類(lèi)定義兩個(gè)成員變量
專(zhuān)注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)漢中免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
int
x、int
y
然后分別用這四個(gè)方法對(duì)x
、y
實(shí)行加、減、乘、除并返回其值
代碼大概如下:
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(){
//加運(yùn)算
return
x+y;
}
public
double
minus(){
//減運(yùn)算
return
x-y;
}
public
double
times(){
//乘運(yùn)算
return
x*y;
}
public
double
divide(){
//除運(yùn)算
return
x/y;
}
}
//測(cè)試類(lèi)
public
class
test{
public
static
void
main(string[]
args){
counter
c=new
counter(5,4);//實(shí)例化
system.out.println(c.adding());//輸出加的結(jié)果
system.out.println(c.minus());//輸出減的結(jié)果
system.out.println(c.times());//輸出乘的結(jié)果
system.out.println(c.divide());//輸出除的結(jié)果
}
}
希望對(duì)你有幫助
public String getPercent(int x,int total){
String result="";//接受百分比的值
double x_double=x*1.0;
double tempresult=x/total;
//NumberFormat nf = NumberFormat.getPercentInstance(); 注釋掉的也是一種方法
//nf.setMinimumFractionDigits( 2 ); 保留到小數(shù)點(diǎn)后幾位
DecimalFormat df1 = new DecimalFormat("0.00%"); //##.00% 百分比格式,后面不足2位的用0補(bǔ)齊
//result=nf.format(tempresult);
result= df1.format(tempresult);
return result;
}
====================
idehub提供云端手機(jī)、平板、pc瀏覽器等方式的java編程平臺(tái),讓你隨時(shí)隨地可以編寫(xiě)java代碼,并有大神在線解答所有技術(shù)問(wèn)題!關(guān)注code4a微信公眾號(hào)!
import java.text.DecimalFormat;
import java.util.Scanner;
public class Zhidao {
public static void main(String[] args) {
String condition = "";
Zhidao zhidao = new Zhidao();
do{
Scanner scanner = new Scanner(System.in);
try{
System.out.print("請(qǐng)輸入第一個(gè)數(shù):");
double x = scanner.nextDouble();
System.out.print("請(qǐng)輸入第二個(gè)數(shù):");
double y = scanner.nextDouble();
System.out.print("請(qǐng)輸入運(yùn)算符:");
String s = scanner.next();
char z = s.charAt(0);
zhidao.yunsuan(x, y, z);
}catch(Exception e){
System.out.println("請(qǐng)輸入正確的數(shù)據(jù)!");
}
System.out.print("是否繼續(xù)?continue:繼續(xù),任意字符:結(jié)束");
condition = scanner.next();
}while("continue".equals(condition));
}
public static void yunsuan(double x,double y,Character z){
DecimalFormat r=new DecimalFormat();
r.applyPattern("#0.00");
if(z.equals('+')){
System.out.println(x+"+"+y+"=" + r.format((x+y)));
} else if(z.equals('-')){
System.out.println(x+"-"+y+"=" + r.format((x-y)));
} else if(z.equals('*')){
System.out.println(x+"*"+y+"=" + r.format((x*y)));
} else if(z.equals('/')){
if(y==0){
System.out.println("被除數(shù)不能為0");
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)));
}
}else{
System.out.println("無(wú)法識(shí)別改運(yùn)算符");
}
}
}
新聞名稱(chēng):java中運(yùn)算代碼怎么寫(xiě),Java代碼怎么寫(xiě)
本文地址:http://jinyejixie.com/article26/hseijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、搜索引擎優(yōu)化、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)