无效方法无法返回值 [英] Void methods cannot return a value

查看:105
本文介绍了无效方法无法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注CS106A的在线讲座.我正在阅读第12讲中的代码,但这给了我Eclipse错误.

I'm following the CS106A lectures online. I'm going through the code on Lecture 12, but it's giving me errors in Eclipse.

这是我的代码.似乎该错误是由于我的main方法中的单词void引起的.我尝试删除main方法,但是Java没有它当然不能运行.

This is my code. It seems the error is because of the word void in my main method. I tried deleting the main method, but of course Java can't run without it.

我是新手,没有人解释过String[] args的真正含义,但是有人告诉我,请忽略它并使用它.如果有人也能向我解释,我将不胜感激.

I'm a newbie and no one has explained what the String[] args thing really means, but I've been told to just ignore it and use it. I'd appreciate if someone could explain that to me as well.

此错误也出现在"toLower"方法上;不知道这是什么意思: 参数toLower的非法修饰符;只能进入决赛

This errors also comes up on the 'toLower' method; no idea what it means: Illegal modifier for parameter toLower; only final is permitted

(如果有帮助,代码的重点是将大写字母转换为小写字母)

(if it helps; the point of the code is to convert an uppercase letter to a lowercase one)

public class CS106A {

    public static void main(String[] args){

        public char toLower(char ch);
            if (ch >= 'A' && ch <= 'Z'){
                return ((ch - 'A') + 'a');
        }
        return ch;      
    }

}

谢谢

推荐答案

您应该在main之外定义方法,例如:

You should be defining your method outside of main, like:

public class YourClass
{
    public static void main(String... args)
    {

    }

    public char yourMethod()
    {
         //...
    }
}

Java不支持嵌套方法.但是,有解决方法,但它们不是您想要的.

Java does not support nested methods; however, there are workarounds, but they are not what you're looking for.

对于您有关args的问题,它只是与命令行参数相对应的Strings数组.请考虑以下内容:

As for your question about args, it is simply an array of Strings that correspond to command line arguments. Consider the following:

public static void main(String... args) //alternative to String[] args
{
    for (String argument: args)
    {
        System.out.println(argument);
    }
}

通过java YourClass Hello, World!

将打印

你好,
Word!

Hello,
Word!

这篇关于无效方法无法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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