-1

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

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);

它确实显示,但我只希望它在销售额< 10时显示

但如果销售额不小于 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);
}
   }
}
4

2 回答 2

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);
}
于 2013-09-14T16:25:09.517 回答
1

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

于 2013-09-14T16:25:44.783 回答