Java:无法获取要打印的递增值。

发布于 2024-07-18 04:26:50 字数 762 浏览 16 评论 0原文

我正在学习一些 Java 101 语法的东西。 在我尝试做的练习中,我无法打印增量值。 有任何想法吗?

这是我的代码:

class StaticTest { 
    static int i = 47;  
}
class incrementable { 
    static void increment() { StaticTest.i++; } 
}

class DataOnly { 

    int i;
    double d;
    boolean b; 

    public static void main (String[] args) { 
        incrementable t = new incrementable(); 
        DataOnly df = new DataOnly(); 
        df.i = t.increment(); 
        System.out.println(df.i);
    } 

}

我得到的错误是:

aTyp0eName.java:18: incompatible types
found: void 
required: int 
df.i = t.increment(); 

df.i是一个intt.increment也是如此。 我猜这是因为 increment()void

I'm learning to do some Java 101 syntax stuff. In an exercise I'm trying to do, I can't get the incremented value to print. Any ideas?

Here is my code:

class StaticTest { 
    static int i = 47;  
}
class incrementable { 
    static void increment() { StaticTest.i++; } 
}

class DataOnly { 

    int i;
    double d;
    boolean b; 

    public static void main (String[] args) { 
        incrementable t = new incrementable(); 
        DataOnly df = new DataOnly(); 
        df.i = t.increment(); 
        System.out.println(df.i);
    } 

}

The error I get is:

aTyp0eName.java:18: incompatible types
found: void 
required: int 
df.i = t.increment(); 

df.i is an int and so is t.increment. I'm guessing it's because increment() is void?

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

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

发布评论

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

评论(6

傲娇萝莉攻 2024-07-25 04:26:50

您的方法签名是:

static void

这意味着该方法没有返回任何内容。

因此,尝试将 increment() 分配给 df.i 将不起作用。

your method signature is:

static void

Which means nothing is returned from this method.

Hence, attempting to assign increment() to df.i won't work.

一梦等七年七年为一梦 2024-07-25 04:26:50

increment() 没有返回值。 您需要让它返回一个 int 才能使您的代码正常工作:

class incrementable { 
    static int increment() { 
        StaticTest.i++;
        return StaticTest.i;
    } 
}

void”是为不返回任何值的方法保留的关键字。 否则,您指定返回什么类或基元(intdoubleString 等)。 您可以在此处了解有关返回值的更多信息。

另外,为了将来的参考,类名应该大写,所以你应该使用“Incrementable”而不是“incrementable”。 (在我的示例代码中,我将其与您的代码保持一致,因此您现在可以将代码放入其中。)

increment() doesn't have a return value. You'd need to have it return an int for your code to work:

class incrementable { 
    static int increment() { 
        StaticTest.i++;
        return StaticTest.i;
    } 
}

"void" is a keyword reserved for methods which don't return any value. Otherwise, you specify what class or primitive you return (int, double, String, whatever). You can read more about return values here.

Also, for future reference, class names should be capitalized, so you should be using "Incrementable" rather than "incrementable". (In my sample code I kept it the same as you had it so you could just drop the code in for now.)

掀纱窥君容 2024-07-25 04:26:50

我得到的错误是:

aTyp0eName.java:18: incompatible types
found: void 
required: int 
df.i = t.increment();

df.i 是 int,t.increment 也是 int [错误?]。 我猜这是因为increment() 无效?

Eeeexactly,你的猜测是正确的。

这是逐行解释的错误消息:

aTyp0eName.java:18: incompatible types

You are attempts to allocate "void" to an int in line 18.

df.i = t.increment();  

这是错误所在。

found: void

您在方法“签名”中声明返回类型是什么。
方法签名是:

<access modifiers> <return type> <method name> (<parameters> )
static                 void       increment        ()

所以返回类型是void。

下一行:

required: int 

df.i 如您之前所述是 int 。

所以,你自己的问题基本上已经有了答案。

拥有编译器的好处是,当出现问题时它会告诉您。

坏事(对人类来说)你必须学会​​阅读这些消息。 它们因编程语言而异,甚至因编译器而异。

这将是一个更正的版本:

class StaticTest { 
    static int i = 47;  
}
class Incrementable { 
    static int increment() { 
        return ++StaticTest.i;
    } 

}

class DataOnly { 

    int i;
    double d;
    boolean b; 

    public static void main (String[] args) { 
        Incrementable t = new Incrementable(); 
        DataOnly df = new DataOnly(); 
        df.i = t.increment(); 
        System.out.println(df.i);
    } 

}

还可以进行一些其他增强,例如向方法和属性添加访问修饰符,但现在我认为这会有所帮助。

The error I get is:

aTyp0eName.java:18: incompatible types
found: void 
required: int 
df.i = t.increment();

df.i is an int and so is t.increment [fault?]. I'm guessing it's because increment() is void?

Eeeexactly, your guess is correct.

This is the error message explained line by line:

aTyp0eName.java:18: incompatible types

You are trying to assign "void" to an int in line 18.

df.i = t.increment();  

Here is where the error is.

found: void

You declare what's the return type in the method "signature".
The method signature is :

<access modifiers> <return type> <method name> (<parameters> )
static                 void       increment        ()

So the return type is void.

Next line:

required: int 

df.i is int as you have previously stated.

So, pretty much you have already your own question answered.

The good point of having a compiler is that it tells you when something is wrong.

The bad thing ( for humans ) you have to learn to read those messages. They vary from programming language to another and even from compiler to compiler.

This would be a corrected version:

class StaticTest { 
    static int i = 47;  
}
class Incrementable { 
    static int increment() { 
        return ++StaticTest.i;
    } 

}

class DataOnly { 

    int i;
    double d;
    boolean b; 

    public static void main (String[] args) { 
        Incrementable t = new Incrementable(); 
        DataOnly df = new DataOnly(); 
        df.i = t.increment(); 
        System.out.println(df.i);
    } 

}

There are some other enhancements could be done, such as adding access modifiers to the methods and attributes, but for now I think this would help.

独木成林 2024-07-25 04:26:50

答案是increment()方法返回一个void。 话虽这么说,有很多违反程序员通常使用的Java标准语法的情况,即:

  1. 类名应该以大写字母开头。
  2. 字段不应该从不同的类中变异,而辅助方法应该在同一个类中
  3. 如果你想调用静态方法,不要引用实例变量,而是使用 ClassName.reference
  4. 如果变量只在本地需要,它不应该是一个字段(df.i 可能只是我在 main 方法中声明的 int)。

编辑:
同一类中的辅助方法的示例:

class StaticTest { 
    private static int i = 47; //keep i private and mutate and access via methods
    public static void increment() { i++; }
    public static int getI() { return i; }
} 

The answer is that the increment() method returns a void. That being said, there are a lot of violations of the Java standard syntax that programmers generally use, Namely:

  1. Class names should start with a capital letter.
  2. fields should not be mutated from different classes, rather a helper method should be within the same class
  3. If you want to call a static method, don't reference an instance variable, rather use ClassName.reference
  4. If a variable is only needed locally, it should not be a field (df.i could just be an int i declared within the main method).

EDIT:
An example of a helper method within the same class:

class StaticTest { 
    private static int i = 47; //keep i private and mutate and access via methods
    public static void increment() { i++; }
    public static int getI() { return i; }
} 
淡淡離愁欲言轉身 2024-07-25 04:26:50

另外,如果实例化可增量,则不应将方法increment() 定义为静态。 或者你可以,但不需要。

如果它是静态的,你可以简单地使用incrementable.increment()。

只是一个提示。 至于你的问题的答案,toolkit已经说得对了。

also, if you instantiate incrementable, you shouldn't define the method increment() as static. or you can, but don't need to.

you could simply incrementable.increment() if it's static.

just a tip. as for the answer to your question, toolkit already said that right.

梦在深巷 2024-07-25 04:26:50

另外,您的 incrementable 类确实应该是 Incrementable

Also, your incrementable class should really be Incrementable.

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