在Java zybooks中添加空格

发布于 2025-01-24 20:12:00 字数 794 浏览 1 评论 0 原文

这是我现在拥有的:

import java.util.Scanner;

public class Welcome {
   public static void main(String[] args) {
      // Complete this statement to create a new instance of Scanner passing in System.in
      Scanner scan = new Scanner(System.in);

      // Complete this statement to use the scanner object to read a string from the user
      String user_name = scan.next();
  
      // Add the statement to print the desired output
      System.out.println("Hello" + user_name + "and welcome to CS Online!");
  
   }
}

如果用户名=乔,则输出:HelloDoeand欢迎来到CS Online! 我通过在“ Hello”之后放置一个空间和“这样:

System.ou.println("Hello " + user name + " and welcome to CS Online!");

我的问题是,有更好的方法可以在变量和字符串之间添加空格?我做的方式是,我的问题是我的修复吗?这似乎不是很好的做法。

Here's what I have right now:

import java.util.Scanner;

public class Welcome {
   public static void main(String[] args) {
      // Complete this statement to create a new instance of Scanner passing in System.in
      Scanner scan = new Scanner(System.in);

      // Complete this statement to use the scanner object to read a string from the user
      String user_name = scan.next();
  
      // Add the statement to print the desired output
      System.out.println("Hello" + user_name + "and welcome to CS Online!");
  
   }
}

If user name = Joe, this outputs: HelloJoeand welcome to CS Online!
I fixed it by putting a space after "Hello" and a space before "and" like this:

System.ou.println("Hello " + user name + " and welcome to CS Online!");

My question is, is there a better way to add whitespace between the variable and strings? The way I did it doesn't seem like good practice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2025-01-31 20:12:00

您可以使用 system.out.printf (立即打印)或 string.format (返回在 string )中,可以仅使用单个字符串带有占位符值,该值被变量代替,%s String 变量的占位符。

当您拥有要在 String 内打印的多个变量时,这可能特别有用。

String userName = scan.next();
System.out.printf("Hello %s and welcome to CS Online!", userName);

请注意,我还将 user_name 更改为用户名在您询问良好实践时,请遵循适当的Java命名骆驼命名约定。

这是一个具有多个变量的示例:

String firstName = scan.next();
String lastName = scan.next();
  
System.out.printf("Hello my first name is %s and my last name is %s!", firstName, lastName);

You can use System.out.printf (immediately prints) or String.format (returns result in a String) instead which lets you use only a single String with a placeholder value that gets replaced by a variable, %s is the placeholder for a String variable.

This can be especially useful when you have multiple variables you want to print inside of a String.

String userName = scan.next();
System.out.printf("Hello %s and welcome to CS Online!", userName);

Note that I also changed user_name to userName to follow proper Java naming conventions of camelCasing as you were asking about good practices.

Here is an example with multiple variables:

String firstName = scan.next();
String lastName = scan.next();
  
System.out.printf("Hello my first name is %s and my last name is %s!", firstName, lastName);
风铃鹿 2025-01-31 20:12:00

我不知道为什么应该是不好的做法。我的意思是,这是您想要的 - 有两个空间,一个又一个“你好”,一个之前的“和”,但实际上有更好的方法可以实现这一目标。通过使用格式,您只需一个带有一个参数的字符串。

System.out.printf("Hello %s and welcome to CS Online!\n", user.name);

I don't know about why it should be bad practise. I mean that's litteraly what you want - to have two spaces, one after "Hello" and one before "and", but there is, in fact, better way to achive this. By using formating you'll have only one string with one parameter.

System.out.printf("Hello %s and welcome to CS Online!\n", user.name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文