You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Console.Write()正确打印多个字符串变量?代码仅输出第一个

问题:Console.Write仅输出第一个变量,语法错误分析与修复

你的代码中Console.Write的调用存在语法错误,导致输出不符合预期,以下是具体问题和修复方案:

问题根源

你最后一行的Console.Write写法不符合C#语法规范:

Console.Write ("Your Adjectives are " firstAdjective, secondAdjective, thirdAdjective);

字符串字面量与第一个变量之间没有任何合法分隔符(如+、逗号),正常情况下代码无法编译。即使你实际代码中误写了分隔符,若未使用正确的字符串组合方式,后续变量也会被忽略,最终只输出部分内容。

修复方案

以下三种方式都能正确输出所有变量:

1. 字符串拼接(+运算符)

用+将字符串和变量依次连接:

Console.Write("Your Adjectives are " + firstAdjective + ", " + secondAdjective + ", " + thirdAdjective);

2. 字符串插值(C# 6及以上版本支持)

使用$前缀的插值字符串,可读性更强:

Console.Write($"Your Adjectives are {firstAdjective}, {secondAdjective}, {thirdAdjective}");

3. 格式化字符串(占位符)

通过{0}、{1}等占位符对应后续变量参数:

Console.Write("Your Adjectives are {0}, {1}, {2}", firstAdjective, secondAdjective, thirdAdjective);

补充说明

如果你的实际代码是写成Console.Write("Your Adjectives are ", firstAdjective, secondAdjective, thirdAdjective),由于第一个字符串参数没有定义占位符,Console.Write只会输出这个固定字符串,后续变量会被忽略;如果是写成Console.Write("Your Adjectives are " + firstAdjective, secondAdjective, thirdAdjective),则"Your Adjectives are " + firstAdjective会被作为一个完整参数传入,后续变量因无对应占位符也不会被输出,这两种情况都会导致输出内容不完整。

内容的提问来源于stack exchange,提问作者internet_preferences

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 06:30:31