2016-02-27 85 views
0

的分配有以下税率:包价的 重量每500英里运 2磅以下$ 1.10 超过2磅,但不超过6磅$ 2.20 超过6磅,但不超过10磅$ 3.70 超过10磅$ 3.80运费计算器小姐计算

每500英里的运费不按比例分配。例如,如果一个2磅包装运送502英里,则收费为2.20美元。编写一个程序,要求用户输入包裹的重量,然后显示运费。

我的问题是,我得到错误的答案。这是我走到这一步:

import java.util.Scanner; 
public class ShippingCharges 
{ 
public static void main (String [] args) 
{ 
    double mDrive, rMiles, wPound; 

    Scanner keyboard = new Scanner (System.in); 

    System.out.print ("Enter Weight of Package: "); 
    wPound = keyboard.nextDouble(); 
    System.out.println(""); 

    System.out.print ("Enter Miles Driven: "); 
    mDrive = keyboard.nextDouble(); 
    System.out.println(""); 

    rMiles = mDrive/500; 

    if (wPound <2) 
    { 
     System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*1.10); 
    } 

    if (wPound >=2 && wPound <6) 
    { 
     System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*2.20); 
    } 

    if (wPound >=6 && wPound <10) 
    { 
     System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.70); 
    } 

    if (wPound >= 10) 
    { 
     System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.80); 
    } 

} 
} 

继例如,程序应该做500分之502* 2.2是2.2和程序是显示4.4。任何建议?

+0

根据所提供的指令,代码应该是'如果(wPound <= 2)'和'如果(wPound> 2 && wPound <= 6) '等等等。那么你可以用'if(wPound <= 2)'和'else if(wPound <= 6)'离开,等等。 –

+0

哈罗德:它的工作原理。我应该为另一个做还是只做这个? 卡尔文:仍然给出相同的答案 –

+0

@JonathanSGutierrez阅读Calvins的答案。他是对的。它应该工作,如果你修复你的if语句。保持Math.ceil相同并尝试。 – Kumar

回答

1

你的if语句是罪魁祸首。以下你提供的说明中,陈述应如下所示

if (wPound<=2) { 
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*1.10); 
} 
else if(wPound<=6) { 
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*2.20); 
} 
else if (wPound<=10) { 
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.70); 
} 
else { 
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.80); 
} 
+0

1最后一个问题:如果wPound = 5的值不应该是第二个和第三个语句是真的? –

+0

是的,但使用'else if',它会落入第一个评估为true的语句,并跳过其余部分@JonathanSGutierrez –

+0

感谢您的一切!我对这部分有点困惑。 –