two Java problemsQuestion:Write a program which asks the user two numbers and the calculation (+, - , * or /) to be performed. The program shall print the calculation as well as the answer. Take care of the situation the divisor being zero.My answer:

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/15 17:54:36
two Java problemsQuestion:Write a program which asks the user two numbers and the calculation (+, - , * or /) to be performed. The program shall print the calculation as well as the answer. Take care of the situation the divisor being zero.My answer:
xV͎#5~RK+u:鑆E Āajq3++8"N3< @g1hخ﫪j۵LTy/gr%Ǐ5 ` KN@̹dY|2mșP"h.Àׇ JðVA cz" *$BKD JIANY3ijۼpS>eZEx~?N"*Sǘ(\DgHfy*8*Arz@)2))\(>2<ÏPDuu]VZ8R|N `0`" )Sh%O)"ɞ`hkЌt wlx_+a\IH_LT(P-O4 QtI thfra] &4rm8g҈}aiSߕ ڨ5E  ^ -/||Cr'dտ YelZbVvs4댅Rk͕jm*Щ |V0cd) Ex5.%6>_Vk(OpFʿQ6½QipY~֛IY+,:eDVv=ؿ_T!~ms-+[ҋx mo+5'x߮>kC1=ÍB;ܱS'A."6_^=ͳgs6_|/Uc<5~qs _3&Ɉ

two Java problemsQuestion:Write a program which asks the user two numbers and the calculation (+, - , * or /) to be performed. The program shall print the calculation as well as the answer. Take care of the situation the divisor being zero.My answer:
two Java problems
Question:Write a program which asks the user two numbers and the calculation (+, - , * or /) to be performed. The program shall print the calculation as well as the answer. Take care of the situation the divisor being zero.
My answer:
import java.util.Scanner;
public class task5 {
public static void main(String[] args) {
Scanner stdin=new Scanner(System.in);
System.out.println("Please enter a number ");
double a=stdin.nextDouble();
System.out.println("Please enter a number again");
double b=stdin.nextDouble();

String readEnter=stdin.nextLine();

System.out.println("Please enter a calculation(+-*/)");
String c=stdin.nextLine();

stdin.close();

String P="+", M="-", MU="*",D="/";
double result=0;

char cc=c.charAt(0);

if(cc.equals(P)){
result=a+b;
System.out.println("The result for "+a+c+b+" is "+result);
}
if(cc.equals(M)){
result=a-b;
System.out.println("The result for "+a+c+b+" is "+result);
}
if(cc.equals(MU)){
result=a*b;
System.out.println("The result for "+a+c+b+" is "+result);
}
if(cc.equals(D)){
if(b==0){
System.out.println("The divisor can not be 0.");
}
else{
result=a/b;
System.out.println("The result for "+a+c+b+" is "+result);
}
}
}
}
Why is it not correct?
The second one:
Question:Write a program which calculates and prints the discount table for a shop. The table is printed for the original prices from 10 euros to 100 euros with 5 euros interval. The discount percentage is asked from the user.
For example, if the user would enter 15 for the discount percent, the discount table should look as follows:
Original Price Cut-price (discount: 15%)
10 euros 8.5 euros
15 euros 12.75 euros
20 euros 17 euros
... ...
100 euros 85 euros
My answer:
import java.util.Scanner;
public class task6 {
public static void main(String[] args) {
Scanner stdin=new Scanner(System.in);
System.out.println("Please enter the discount: (IN %)");
String discountinpercent=stdin.nextLine();
double discount=Integer.parseDouble(discountinpercent);
double Rdiscount=discount/100;

System.out.println("Original Price Cut-price");

double price=10;
double cutdiscount=price*(1-Rdiscount);

while(price

two Java problemsQuestion:Write a program which asks the user two numbers and the calculation (+, - , * or /) to be performed. The program shall print the calculation as well as the answer. Take care of the situation the divisor being zero.My answer:
1、
String P="+",M="-",MU="*",D="/";
double result=0;
char cc=c.charAt(0);
if(cc.equals(P)){
result=a+b;
System.out.println("The result for "+a+c+b+" is "+result);
}
你cc是char,而P是String.所以把 char cc=c.charAt(0);换成:String cc=c.substring(0,1);
2.double discount=Integer.parseDouble(discountinpercent);
把Integer换成:Double