如何修改Java代码使拼接的姓名带空格,实现如Mizanur Rahman Sajid的格式
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

