首页 > 解决方案 > Concatenating issue when printing out objects with System.out.println();

问题描述

I'm learning to code on Codecademy and keep coming up with a concatenating issue. For example, when I wrote:

System.out.println(lemonadeStand + cookieShop);

I got back the error:

Store.java:32: error: bad operand types for binary operator '+' System.out.println(lemonadeStand + cookieShop);

But when I wrote:

System.out.println(lemonadeStand);
System.out.println(cookieShop);

The code worked. Can someone tell me why doesn't the first one work? (And thanks)

---edit--- Thanks for all the help everyone! This was my first time posting a question here and I'm amazed at how kind and helpful the community is!

标签: javaconcatenationconcat

解决方案


请在连接两个变量之前,您必须验证它们是相同类型或尝试 System.out.println(first + " " + second);


推荐阅读