可以在Java的If条件中使用定制方法吗? [英] Can a custom made method be used within a If condition in Java?

查看:300
本文介绍了可以在Java的If条件中使用定制方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面,我使用两种方法将If Else语句的结果作为结果.但这给了我一些我无法纠正的错误.请解决我的问题?我的Java版本是Java SE 6.0.

源代码

At the below I am using two methods to have as the results of If Else statement. But it gives me several errors that I cannot correct. Please solve my problem? My Java Version is Java SE 6.0.

Source Code

01 
02     public class If_Else{
03
04        public static void main (String[] args){
05            int x = 5;
06            int y = 5;
07
08            public void ResultOk(){
09
10             System.out.println("Good!");
11
12            }
13
14
15            public void ResultBad(){
16
17             System.out.println("Bad!");
18
19            }
20
21            if(x==y) {
22
23            ResultOk();
24
25            }else{
26
27            ResultBad();
28
29            }
30        }
31    }




错误




Errors

If_Else.java:8: illegal start of expression
                        public void ResultOk(){
                        ^
If_Else.java:21: illegal start of type
                        if(x==y) {
                        ^
If_Else.java:21: <identifier> expected
                        if(x==y) {
                            ^
If_Else.java:21: <identifier> expected
                        if(x==y) {
                               ^
If_Else.java:25: illegal start of type
                        }else{
                         ^
If_Else.java:31: class, interface, or enum expected
        }
        ^



请尝试解决我的问题?

谢谢.



Please try to solve my problem?

Thanks.

推荐答案

您已经将ResultOk()ResultBad()方法放入了main()方法中,这是非法的.将它们移到main()末尾之外.
You have put your ResultOk() and ResultBad() methods inside your main() method, which is illegal. Move them outside of the end of main().


这篇关于可以在Java的If条件中使用定制方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆