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

如何修改Java代码使拼接的姓名带空格,实现如Mizanur Rahman Sajid的格式

Fixing Name Concatenation with Space

Hey there! The issue here is super straightforward—you’re just missing a space between the two names when you stitch them together. Let’s tweak the code to fix that right away.

Modified Code

package com.exploreas;
import java.util.Scanner;
public class UserInput {
 public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String name1, name2, value;
 System.out.print("Enter Your First Name : ");
 name1 = input.nextLine();
 System.out.print("Enter Your Second Name : ");
 name2 = input.nextLine();
 // Add a space between the two names during concatenation
 value = name1 + " " + name2;
 System.out.println("Welcome! = "+value);
 }
}

What Changed?

The critical adjustment is in the line value = name1 + " " + name2;. By inserting " " (a single space wrapped in quotes) between name1 and name2, you tell Java to include that space when merging the two strings. Now instead of outputting something like RahmanSajid, you’ll get the spaced format you want—like Mizanur Rahman.

If you ever need to add more name components later (like a middle name), you can just extend this pattern: name1 + " " + middleName + " " + name2; to get full names formatted as Mizanur Rahman Sajid.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 21:17:56