无法正确输出价格和折扣

时间:2013-06-25 14:29:36

标签: java java.util.scanner

我正在做我的讲师给出的任务,所有输出都是正确的但折扣后的价格和付款没有正确输出,我不知道问题出在哪里..有人可以帮我这个吗?我仍然试图理解编码..

这是我的编码..

import java.util.*;

public class Apps {

public static void main (String args []) {

    double price=0.0, discount=0.0, total=0.0;

Scanner sc = new Scanner (System.in);
String lineSeparator = System.getProperty("line.separator");

System.out.print("Please input your name : ");
String name = sc.next();

System.out.print("What is your status [A-Adult | C-Children] : ");
String status = sc.next();

System.out.print("Please enter type of treatment[1-Restoration | 2-Extraction | 3-Scaling] : ");
int type = sc.nextInt();

if(status=="C"){
    if(type=='1'){
        price = 6.0;
        discount = price * 0.90;}
    else if(type=='2'){
        price = 15.5;
        discount = price * 0.90;}
    else{
        price = 4.0;
        discount = price * 0.90;}}
else if(status=="A"){
    if(type=='1'){
        price = 7.5;
        discount = price * 0.95;}
    else if(type=='2'){
        price = 18.0;
        discount = price * 0.95;}
    else{
        price = 5.5;
        discount = price * 0.95;}}

System.out.println("        \n\n                                   HUSNA DENTAL");
System.out.println("      ====================================================================");
System.out.println("                  Name                   : "+name);
System.out.println("                  Type of treatment      : "+type);
System.out.println("                  Payment before discount: "+price);
System.out.println("                  Payment after discount : "+(total=price-discount));
System.out.println("      ====================================================================");
}
}

输出就像这样..

请输入您的姓名:deena

你的身份是什么[A-Adult | C-Children]:C

请输入治疗类型[1 - 恢复| 2-Extraction | 3-Scaling]:1

                               HUSNA DENTAL
  ====================================================================
              Name                   : deena
              Type of treatment      : 1
              Payment before discount: 0.0
              Payment after discount : 0.0
  ====================================================================

3 个答案:

答案 0 :(得分:4)

几个问题:

1)if(status=="C"){

应该是

if(status.equals("C")){

使用equals()进行字符串比较。这同样适用于代码中的其他字符串比较。

2)type == 1用于int比较NOT type=='1',同样适用于代码中的其他位置。

答案 1 :(得分:0)

对于对象比较,使用Object#equals()方法。 ==仅比较参考文献。

因此,而不是if(status ==“C”)使用if(“C”.equals(status))。与“C”相比,您将免于NPE

对于int ''不是必需的。

您可能需要的另一点是,在计算时使用BigDecimal而不是双倍。

答案 2 :(得分:0)

import java.util.*;

public class Apps {

public static void main (String args []) {

    double price=0.0, discount=0.0, total=0.0;

Scanner sc = new Scanner (System.in);
String lineSeparator = System.getProperty("line.separator");

System.out.print("Please input your name : ");
String name = sc.next();

System.out.print("What is your status [A-Adult | C-Children] : ");
String status = sc.next();

System.out.print("Please enter type of treatment[1-Restoration | 2-Extraction | 3-Scaling] : ");
int type = sc.nextInt();

if(status=="C"){
    if(type=='1'){
        price = 6.0;
        discount = price * 0.90;}
    else if(type=='2'){
        price = 15.5;
        discount = price * 0.90;}
    else{
        price = 4.0;
        discount = price * 0.90;}}
else if(status=="A"){
    if(type=='1'){
        price = 7.5;
        discount = price * 0.95;}
    else if(type=='2'){
        price = 18.0;
        discount = price * 0.95;}
    else{
        price = 5.5;
        discount = price * 0.95;}}

System.out.println("        \n\n                                   HUSNA DENTAL");
System.out.println("      ====================================================================");
System.out.println("                  Name                   : "+name);
System.out.println("                  Type of treatment      : "+type);
System.out.println("                  Payment before discount: "+price);
System.out.println("                  Payment after discount : "+(total=price-discount));
System.out.println("      ====================================================================");
}
}

以下是问题

  • 比较字符串时,始终使用equals方法而不是==作为equals方法检查字符串相等性,==检查两个引用是否引用同一对象
  • 您已使用int阅读int type = sc.nextInt();表单中的值,因此您应使用if(type==1)而非if(type=='1')进行比较