正确的消息显示

时间:2013-09-14 16:22:36

标签: java

我没有收到任何错误,但我想显示此消息“没有折扣。您的总数是$ _ ” 使用此代码:

if (!(sales < 10))
            System.out.print("No discount. " + 
                             "Your total is: $" + (int)total);

在此代码中:

if (!(sales < 10))
                System.out.print("No discount. " + 
                                 "Your total is: $" + (int)total);
            else if (sales >= 10 || sales <= 19)  
                rate = 0.20;  
            else if (sales >= 20 || sales <=49)  
                rate = 0.30;  
            else if (sales >= 50 || sales <=99)  
                rate = 0.40;  
            else if (sales > 100)  
                rate = 0.50;  
            else
                System.out.println("Your discount is $" + (int)discount + 
                                   ". Your total is: $" + (int)total);

它确实显示但我只希望它显示何时销售&lt; 10

但如果销售不是&lt; 10 然后我希望它显示“你的折扣是$ _ __。你的总数是$ _ ___。”

任何帮助将非常感谢。谢谢你的时间。

整个代码:

import java.util.Scanner;

public class SoftwareSales
{
   public static void main(String[] args)
   {

      Scanner keyboard = new Scanner(System.in);

      int sales = 99;

      int quantity;
      double total;
      double rate = 0;
      double discount;

      System.out.print ("Enter amount of packages purchased: ");
            quantity = keyboard.nextInt();

      total = quantity * sales;
      discount = total * rate;
      total = total - discount;

      if (sales < 10) {
            if (sales >= 10 || sales <= 19)  
                rate = 0.20;  
            else if (sales >= 20 || sales <=49)  
                rate = 0.30;  
            else if (sales >= 50 || sales <=99)  
                rate = 0.40;  
            else if (sales > 100)  
                rate = 0.50;  
            System.out.println("Your discount is $" + (int)discount + 
                                   ". Your total is: $" + (int)total);
      } else {

                System.out.print("No discount. " + 
                                 "Your total is: $" + (int)total);
}
   }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

if (sales > 10) {
    if (sales >= 10 || sales <= 19)  
        rate = 0.20;  
    else if (sales >= 20 || sales <=49)  
        rate = 0.30;  
    else if (sales >= 50 || sales <=99)  
        rate = 0.40;  
    else if (sales > 100)  
        rate = 0.50;  
    System.out.println("Your discount is $" + (int)discount + 
                                   ". Your total is: $" + (int)total);
} else {

    System.out.print("No discount. " + 
                                 "Your total is: $" + (int)total);
}

答案 1 :(得分:1)

那你为什么要测试你想要做的相反的确切?请注意感叹号(!),它会在您的第一个if中反转您的销售测试。删除它,您的代码应按预期工作。