IF 参数

发布于 2024-11-04 03:26:59 字数 1656 浏览 5 评论 0原文

            if (array[r][0].equals("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }
        }
    if (array[r][0]==("Sell")){
        System.out.println(array[r][0]);
        for ( i = 0; i < 8; i++)              
            Sales[r][i] =array[r][i];
    }  

我试图弄清楚该参数是如何工作的,以及我总是使用该参数的方式是 ===<=>,<代码>!=。但现在我尝试使用 .equals 使数组工作,但似乎没有错误。那么我有任何语法错误吗?或者我用错了?

有关我试图做的更多信息,我正在尝试提出论据,看看它是否等于“买入”或“卖出”,它会通过。但我尝试了 == 和 .equals ...没有做任何事情 我尝试过,在“购买”部分上等于它仍然不会通过

            if (array[r][0].equals("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }

通过这条线

if (array[r][0] != null)

我将全部切换为.equals,但出于某种原因它迫使我在此之前

        if (array[r][0] != null)
        for ( r = 0; r < 165; r++){
            System.out.println(array[r][0]);
            if (array[r][0].equalsIgnoreCase("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }
        }
    if (array[r][0].equalsIgnoreCase("Sell")){
        System.out.println(array[r][0]);
        for ( i = 0; i < 8; i++)              
            Sales[r][i] =array[r][i];
    }        

那么为什么我需要这条线来完成这项工作,因为显然它阻止它通过 IF 语句

            if (array[r][0].equals("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }
        }
    if (array[r][0]==("Sell")){
        System.out.println(array[r][0]);
        for ( i = 0; i < 8; i++)              
            Sales[r][i] =array[r][i];
    }  

i'm try to figure out how the argument works and the way i always use the argument is either ==, =<, =>, !=. but now i'm tryin to use a .equals for an array to work, but it seems there is no error. so am i havin any Syntax Error? or am i using it wrong?

For more info of what i was tryin to do, is i'm tryin to make the argument to see if it equals Buy or Sell, it goes thru. but i tried == and .equals ... doesn't do anything
I TRIED ,equals on "buy" part it still wont' go thru

            if (array[r][0].equals("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }

i switched ALL to .equals, but for some reasong it forces me to go thru this line

if (array[r][0] != null)

before this

        if (array[r][0] != null)
        for ( r = 0; r < 165; r++){
            System.out.println(array[r][0]);
            if (array[r][0].equalsIgnoreCase("Buy")){
                System.out.println(array[r][0]);
                for ( i = 0; i < 8; i++)              
                    Buys[r][i] = array[r][i];
            }
        }
    if (array[r][0].equalsIgnoreCase("Sell")){
        System.out.println(array[r][0]);
        for ( i = 0; i < 8; i++)              
            Sales[r][i] =array[r][i];
    }        

so why do i need this line to make this work, cuz apparently it stops it to thru that IF statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

盛夏尉蓝 2024-11-11 03:26:59

if 的“参数”是一个布尔表达式——值是 boolean 类型的表达式,可以是 true。这些运算符(== 等)返回布尔值,equals() 方法以及许多其他方法(例如 contains())也是如此。 List 接口中的方法。您也可以自己编写布尔方法。

THe "argument" to if is a boolean expression -- an expression whose value is of type boolean, which can be either true or false. Those operators (==, etc) return boolean values, as does the equals() method, and many other methods like the contains() method in the List interface. You can also write boolean methods yourself.

海风掠过北极光 2024-11-11 03:26:59

语法是

if(-something-that-evaluates-to-boolean)

这样的,可以使用评估为布尔值的东西,例如 (a < 1)(b == 1) 。也可以使用任何返回布尔值的东西。在您的情况下,equals() 返回布尔值,这就是它起作用并且用法完全有效的原因。

the syntax is

if(-something-that-evaluates-to-boolean)

so things that evaluate to boolean such as (a < 1) or (b == 1) can be uses. Also anything that returns boolean can also be used. In your case, equals() returns boolean and that's why it works and the usage is perfectly valid.

原谅过去的我 2024-11-11 03:26:59
  1. 永远不要将数组命名为“array”。它不传达任何有意义的信息

  2. 字符串是对象,而不是基元。您应该始终使用 .equals() 来比较它们,除非您真的知道自己是什么正在做。这不是语法问题,而是语义问题。

  1. Never name your arrays 'array'. It does not convey any meaningful information

  2. Strings are objects, not primitives. You should always compare them using .equals() unless you really know what you're doing. It's not a matter of syntax, it's a matter of semantics.

陌上青苔 2024-11-11 03:26:59

您仍然有 if (array[r][0]==("Sell"))。让它使用equals(..)。 java中的字符串不能用==进行比较

注意:当然,对于每个对象来说,==都是可以进行比较的。但是对于字符串,除非您使用内部字符串(即相同的对象用于相同的字符串内容),否则它将不起作用

You still have if (array[r][0]==("Sell")). Make it use equals(..). Strings in java can't be compared with ==

Note: of course, as every object, == comparison can be done. But with strings it won't work unless you are using interned strings (that is, the same object for the same string contents)

满意归宿 2024-11-11 03:26:59

if 语句中的参数必须是 truefalse。这意味着您可以使用任何关系运算符(==!=<>、< code><=、>=)或任何返回 boolean 类型的方法。

所有对象都有一个可用的 equals 方法,该方法返回 boolean 并且如果两个对象“相等”,则应该返回 true,无论您喜欢什么定义给定类型对象的相等性。 (例如,两个具有相同长度和字符的字符串相等)因此,这是 if 语句的完全有效的参数。

The argument in an if statement must be something that is either true or false. This means you can use any relational operator (==, !=, <, >, <=, >=) or any method that returns a boolean type.

All objects have an equals method available, which returns boolean and is supposed to return true if two objects are "equal", for however you like to define equality for a given kind of object. (For example, two strings that have the same length and characters are equal) So this is a perfectly valid argument for if statements.

一腔孤↑勇 2024-11-11 03:26:59

在 Java 中,不应使用 == 来比较字符串。使用 String.equals、String.equalsIgnoreCase 或 String.compareTo

You should not use == to compare Strings in Java. Use String.equals, String.equalsIgnoreCase or String.compareTo

旧情勿念 2024-11-11 03:26:59

数组 (array[r][0]) 的内容可以为空。 Stringarray 的默认值为 null,并且由于 null 不存在,因此无法对其调用函数。

但对于“买入”和“卖出”,您知道它们存在并且不能为空。因此最好更换
array[r][0].equals("买入")

"Buy".equals(array[r][0])

这样你就不必提前检查 array[r][0] 是否为空,因为 equals 会为你做这件事。

不能使用 == 检查字符串相等性的原因之一是因为 String 是一个对象,在这种情况下 == 运算符检查两个字符串的内存地址是否相同。大多数时候情况并非如此。 equals 运算符识别该对象是一个 String 并检查 String 对象的内容是否相同。

The contents of the array (array[r][0]) could be empty. The default value of a Stringarray is null, and since null doesn't exist, one can't call functions on it.

But for both "Buy" and "Sell" you know they exist and cannot be null. Therefore it would be better to replace
array[r][0].equals("Buy")
with
"Buy".equals(array[r][0])

That way you won't have to check in advance if array[r][0] is null, since the equals does this for you.

One of the reasons you can't use == to check for string equality is because the String is an object, and in this case the == operator checks whether the memory address is the same for both string. Most of the time this is not the case. The equals operator recognizes that the object is a String and checks whether the contents of the String-object are the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文