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.