在编码中,方法,整数到布尔值的理解

huangapple go评论93阅读模式
英文:

While code, Method, Int to boolean understanding

问题

I'm reviewing some code for a college assignment and we've been given examples to assist us. I'm little confused on what the below is doing as it's using the assignment operator instead of the .equals or == method.

如果我用 == 替换这段代码(并创建一个局部变量来进行比较),代码会开始无限循环并显示默认值。

如果我用 == 替换这段代码(并创建一个局部变量来进行比较),代码会开始无限循环并显示默认值。

int select = 0;
     
do {
    switch (select) {
        case 1:
            Problem();
            break; 
        default:
            System.out.println("Invalid");
            break;
    } 
} while ((select = getSelection()) !=3);

From my limited understanding, the above assigns "Select" to the value from the "getSelection" method, it's also stating do not accept inputs that are 3 e.g. System.exit0 at this point. Have I understood correctly?

根据我有限的理解,上述代码将 "Select" 赋值为 "getSelection" 方法的返回值,它还表示在这一点上不接受值为 3 的输入,例如 System.exit0。我理解得对吗?

(Further example as requested)
I would do something along the lines of:

按要求进一步举个例子:

int select = 0; 
int select1 = 0; 
do {
    switch (select) {
        case 1:
            Problem();
            break; 
        default:
            System.out.println("Invalid");
            break;
    } 
} while (select == select1);

I am attempting to think of a logical equivalent to the lecturers' example but seem to be unable to do this without breaking the while loop.

我试图考虑一个与讲师示例逻辑等价的方法,但似乎无法做到这一点,而不破坏 while 循环。

英文:

I'm reviewing some code for a college assignment and we've been given examples to assist us.
I'm little confused on what the below is doing as it's using the assignment operator instead of the .equals or == method.

If I replace the code with the == (and create a local variable to compare it to) the code starts infinite looping and displaying out the default value.

 int select = 0;
 
  do {
    
        switch (select) {
            case 1:
                Problem();
                break; 
            default:
            System.out.println("Invalid");
            break;
            } 

    } while ((select = getSelection()) !=3);

 public static int getSelection () { 
(Return function here with has.nextInt and scanner class to receive input)
}

From my limited understanding, the above assigns "Select" to the value from the "getSelection" method, it's also stating do not accept inputs that are 3 e.g. System.exit0 at this point.

Have I understood correctly?

(Further example as requested)
I would do something along the lines of:

int select = 0; 
int select1 = 0; 
    do {

    switch (select) {
        case 1:
            Problem();
            break; 
        default:
        System.out.println("Invalid");
        break;
        } 
 } while (select == select1);

I am attempting to think of a logical equivalent to the lecturers example but seem to be unable to do this without breaking the while loop.

答案1

得分: 4

在Java(以及其他"C语言风格"的语言)中,赋值语句的结果是被赋的值,即这段代码:

do {
    // other code
} while ((select = getSelection()) != 3)

与以下代码是相同的:

do {
    // other code
    select = getSelection();
} while (select != 3)

这种风格被称为内联条件,通常被认为是应该避免使用的一种风格。

对此存在Checkstyle违规 - 参见AvoidInlineConditionals

英文:

In java, (and other "C like" languages) the result of an assignment is the value assigned, ie this code:

do {
    // other code
} while ((select = getSelection()) !=3)

is the same as:

do {
    // other code
    select = getSelection();
} while (select != 3)

This style, known as in-line conditional, is generally considered a style to be avoided.

There is a checkstyle violation for it - see AvoidInlineConditionals

huangapple
  • 本文由 发表于 2020年8月5日 14:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63259728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定