简体   繁体   English

在一行上写多个句子

[英]Writing multiple sentences on one line

I want my program to write the following to to the console: 我希望我的程序将以下内容写入控制台:

The volume of your Right Rectangular Prism is: * Cm^3 右矩形棱镜的体积为: * Cm ^ 3

But I don't know how to make it write anything after I specify the "& volume" in the call to the WriteLine method. 但是在我对WriteLine方法的调用中指定“&volume”后,我不知道如何编写任何内容。 Is there any way to actually do this? 有没有办法真正做到这一点?

Here is my code for that line: 这是我的代码:

Console.Write("The volume of your right rectangular prism is: " & volume):("Cm^3")

Looks like a line of C#? 看起来像一行C#?

You can use something like: 您可以使用以下内容:

Console.Write("The volume of your right rectangular prism is: ");
Console.WriteLine(volume & " Cm^3");

I like to use String.Format for things like this: 我喜欢使用String.Format这样的事情:

String.Format("The volume of the sphere with radius {0} is {1}", radius, volume)

You can put this inside your Console.WriteLine , or save it to another variable first if you want to keep the line from getting too long. 你可以将它放在Console.WriteLine ,或者如果你想让这条线变得太长,可以先将它保存到另一个变量中。

Try it this way: 试试这种方式:

Console.Write("The volume of your right rectangular prism is: " & volume & " whatever else you want to say")

In fact you could keep going... the basic concept is end your text then do a & , then add the variable. 事实上,你可以继续......基本概念是结束你的文本,然后做一个& ,然后添加变量。 If you need more you can always repeat it the same way. 如果您需要更多,您可以始终以相同的方式重复它。

The syntax of your code is invalid. 代码的语法无效。 The following portion is wrong: 以下部分是错误的:

volume):("Cm^3")

The first closing parentheses ends the call to the Write method. 第一个右括号结束对Write方法的调用。 The colon is used in VB.NET to separate two statements on the same line, much like the semicolon is used in C#, and other similar languages. 在VB.NET中使用冒号来分隔同一行上的两个语句,就像C#中使用的分号和其他类似语言一样。 However, the next statement, after the colon, is invalid. 但是,冒号后的下一个语句无效。 ("Cm^3") is, all on its own, an invalid statement. ("Cm^3")本身就是一个无效的陈述。 It looks like you are specifying parameters to a method without ever specifying which method you are trying to call. 看起来您正在为方法指定参数,而无需指定您尝试调用的方法。 I suspect that what you meant to do was something like this: 我怀疑你打算做的是这样的:

Console.Write("The volume of your right rectangular prism is: " & volume) : Console.Write("Cm^3")

That will work, but it's a bit unusual. 这会奏效,但有点不寻常。 Typically, in VB.NET, you'd just put each statement on its own line, like this: 通常,在VB.NET中,您只需将每个语句放在自己的行中,如下所示:

Console.Write("The volume of your right rectangular prism is: " & volume)
Console.Write("Cm^3")

However, rather than using concatenation, at that point, you could just call Write three times, like this: 但是,在这一点上,您可以只调用Write三次,而不是使用串联,如下所示:

Console.Write("The volume of your right rectangular prism is: ")
Console.Write(volume)
Console.Write("Cm^3")

Or, you could concatenate all three together in a single call to the Write method, like this: 或者,您可以通过一次调用Write方法将所有三个连接在一起,如下所示:

Console.Write("The volume of your right rectangular prism is: " & volume & "Cm^3")

Or, another popular option is to use the string formatting feature like this: 或者,另一个流行的选项是使用字符串格式化功能,如下所示:

Console.Write("The volume of your right rectangular prism is: {0}Cm^3", volume)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM