当前位置 :
【twoJavaproblemsQuestion:Writeaprogramwhichaskstheusertwonumbersandthecalculation(+,-,*or/)tobeperformed.Theprogramshallprintthecalculationaswellastheanswer.Takecareofthesituationthedivisorbeingzero.Myanswer:】
更新时间: 2025-08-19 20:20:23
1人问答
问题描述:

twoJavaproblems

Question:Writeaprogramwhichaskstheusertwonumbersandthecalculation(+,-,*or/)tobeperformed.Theprogramshallprintthecalculationaswellastheanswer.Takecareofthesituationthedivisorbeingzero.

Myanswer:

importjava.util.Scanner;

publicclasstask5{

publicstaticvoidmain(String[]args){

Scannerstdin=newScanner(System.in);

System.out.println("Pleaseenteranumber");

doublea=stdin.nextDouble();

System.out.println("Pleaseenteranumberagain");

doubleb=stdin.nextDouble();

StringreadEnter=stdin.nextLine();

System.out.println("Pleaseenteracalculation(+-*/)");

Stringc=stdin.nextLine();

stdin.close();

StringP="+",M="-",MU="*",D="/";

doubleresult=0;

charcc=c.charAt(0);

if(cc.equals(P)){

result=a+b;

System.out.println("Theresultfor"+a+c+b+"is"+result);

}

if(cc.equals(M)){

result=a-b;

System.out.println("Theresultfor"+a+c+b+"is"+result);

}

if(cc.equals(MU)){

result=a*b;

System.out.println("Theresultfor"+a+c+b+"is"+result);

}

if(cc.equals(D)){

if(b==0){

System.out.println("Thedivisorcannotbe0.");

}

else{

result=a/b;

System.out.println("Theresultfor"+a+c+b+"is"+result);

}

}

}

}

Whyisitnotcorrect?

Thesecondone:

Question:Writeaprogramwhichcalculatesandprintsthediscounttableforashop.Thetableisprintedfortheoriginalpricesfrom10eurosto100euroswith5eurosinterval.Thediscountpercentageisaskedfromtheuser.

Forexample,iftheuserwouldenter15forthediscountpercent,thediscounttableshouldlookasfollows:

OriginalPriceCut-price(discount:15%)

10euros8.5euros

15euros12.75euros

20euros17euros

......

100euros85euros

Myanswer:

importjava.util.

马彦恒回答:
  1、StringP="+",M="-",MU="*",D="/";doubleresult=0;charcc=c.charAt(0);if(cc.equals(P)){result=a+b;System.out.println("Theresultfor"+a+c+b+"is"+result);}你cc是char,而P是String.所以把charcc=c.ch...
最新更新
保卡通(baokatong.com)汇总了汉语字典,新华字典,成语字典,组词,词语,在线查字典,中文字典,英汉字典,在线字典,康熙字典等等,是学生查询学习资料的好帮手,是老师教学的好助手。
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
Copyright©2009-2021 保卡通 baokatong.com 版权所有