English 中文(简体)
为什么返回——99次?
原标题:Why does this return -99 all the time?
  • 时间:2012-04-13 20:14:26
  •  标签:
  • java

我在相当一段时间没有使用ava,我对此感到沮丧。 它总是回报——99,但我不知道逻辑在哪里是错误的。

public static void main(String[] args){
    System.out.println(shippingCost( P ,10));
}

public static int shippingCost(char packageType, int weight)
{
    String e1 = "Legal Values: Package Type must be P or R";
    String e2 = "Legal Values: Weight < 0";
    int cost = 0;
        if((packageType !=  P )||(packageType !=  R ))
        {
             //throw new Exception(e1);
             return -99;
        } else {
            return -1;
        }
最佳回答

您是否应当使用<代码>AND。 既然现在有<条码>OR,那么你包裹的实际情况是: 类型既不是P,也不是R。

问题回答

如果是错的话。 (无P)或(无R)永远是真实的,因为并不总是真实的。

Your if statement is checking if the packageType is not equal to P OR it is not equal to R . This will always resolve to true, which means your function will always return -99.

I think you meant to use AND instead of OR. Try

if((packageType !=  P ) && (packageType !=  R ))
                        ^^

因为包装类型不是R !

我认为,如果说的话,你的问题就存在。 由于你的情况是“不是POR而不是R”,那么如果你把P重新采用这种方法,那么,你将评估这一状况。 这是因为它不是P,但它也不是R,因此它评估是真实的。

Sonny





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签