Java 无法从 void 转换为 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19281716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:40:48  来源:igfitidea点击:

Cannot convert from void to String

java

提问by 24116

Driver class:

驱动类:

String world = methods.printHelloWorld("Hello, world!");
{
    System.out.println(world);
}

method class:

方法类:

public void printHelloWorld(String world)
{
    world = "Hello, world!";
}

Prompt: (part of a larger project)

提示:(较大项目的一部分)

void printHelloWorld()

无效的打印HelloWorld()

Parameters: none

参数:无

Return nothing. Directly prints to System.out the String "Hello, world!"

什么都不回。直接向 System.out 打印字符串“Hello, world!”

I'm getting the error that I cannot convert from voidto String. I can't use a return in my method class. I know I can change public void printHelloWorld(String world)to public String printHelloWorld(String world), but that isn't what my teacher wants us to do. (I tried those already)

我收到我不能从转换的错误voidString。我不能在我的方法类中使用 return。我知道我可以改变public void printHelloWorld(String world)to public String printHelloWorld(String world),但这不是我老师希望我们做的。(我已经试过了)

采纳答案by M. Abbas

The exception is very clear: You cannot convert from void to String.

异常很明显:您不能从 void 转换为 String。

This is how you can achieve what you are trying to do:

这是您实现目标的方法:

public void printHelloWorld(String world) {
    System.out.println(world);
}

Then

然后

methods.printHelloWorld("Hello, world!");

回答by Eng.Fouad

I know I can change public void printHelloWorld(String world) to public String printHelloWorld(String world), but that isn't what my teacher wants us to do. (I tried those already)

我知道我可以将 public void printHelloWorld(String world) 更改为 public String printHelloWorld(String world),但这不是我的老师希望我们做的。(我已经试过了)

Then you can print inside the method itself. Something like:

然后您可以在方法本身内部打印。就像是:

public void printHelloWorld(String world)
{
    System.out.println(world);
}

回答by Ted Hopp

You can't convert to a Stringbecause there's nothing to convert. A return type of voidmeans "does not return a value".

你不能转换为 aString因为没有什么可以转换的。返回类型的void意思是“不返回值”。

Also, you can assign values inside a method to a formal parameter all you want (unless it is declared final) and it won't change the value of the actual parameter used in the call to the method.

此外,您可以将方法内的值分配给您想要的形式参数(除非已声明final),并且它不会更改调用该方法时使用的实际参数的值。

Perhaps you were trying to do this:

也许你试图这样做:

String world = "Hello, world!";
methods.printHelloWorld(world);

and

public void printHelloWorld(String world)
{
    System.out.println(world);
}

回答by Victor

public void printHelloWorld(String world)
{
    world = "Hello, world!";
    System.out.println(world);
}

回答by Kaushik Sivakumar

in method Class have

在方法类中有

  public static String world = "Hello, world!";

in driver class

在司机班

 String world1 = methods.world;
 System.out.println(world1);

回答by Sachin Thapa

Your method should return String

你的方法应该返回 String

public String printHelloWorld(String world)
{
    world = "Hello, world!";
    return world;
}

or Simply

或者干脆

public String printHelloWorld(String world)
{
    return "Hello, world!";
}

Cheers !!

干杯!!