-1

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

这是我的编码..

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-成人 | C-儿童] : C

请输入治疗类型[1-修复 | 2-萃取 | 3-缩放]:1

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

3 回答 3

4

几个问题:

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

应该

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

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

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

于 2013-06-25T14:32:58.627 回答
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通过 using 读取表单中的值,int type = sc.nextInt();因此您应该比较 usingif(type==1)而不是if(type=='1')
于 2013-06-25T14:43:28.030 回答
0

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

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

而对于 int''不是必需的。

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

于 2013-06-25T14:40:54.880 回答