在我错的地方需要帮助 [英] Need help where I am wrong

查看:66
本文介绍了在我错的地方需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设a =6,b = 7,y = 10

和z = 4.

由以下两个语句打印的内容S

System.out.println(a + b + y + z);

Systemout.println(y + z + a + b);



我尝试过: < br $>


i假设答案是

621

1267

Assume that a = "6" ,b = 7 ,y = 10
and z = 4.
What is printed by the following two statements, S
System.out.println (a + b + y + z);
Systemout.println(y + z + a + b);

What I have tried:

i assume answer are
621
1267

推荐答案

嗯,好像你猜对了。

问题:为什么你要猜?为什么你不能编译,看看实际输出是什么,并试图找出你得到的东西为什么?



简单来说:

如果是

Well, it seems you guessed wrong.
Question: why do you have to guess? Why can't you compile and see what is the actual output and try to figure out why you get what you get?

In simple words:
in case of
System.out.println (a + b + y + z);  // first parameter in the equation is string, all the other parameters afterward was converted into string
Systemout.println(y + z + a + b); // first two parameters are number, from a to rest was converted to string, since a is string. 







这是一个测试




Here is a test

public class Main2 {
  public static void main(String []strings) {
      String a = "6";
      double y = 7.1;
      int b = 10, z = 4;
      TestClass t = new TestClass();
      TestClass t2 = new TestClass();
      System.out.println(t);
      System.out.println(t + a);
      System.out.println(b + t.toString()); //       System.out.println(b + t); Compiler will reject, even System.out.println(t + b); will be rejected
      System.out.println(t2.toString() + t); // System.out.println(t2 + t); // will fail
  }
}

class TestClass {
  int i;
  int j;
  
  TestClass() {
    i = 10;
    j=23;
  }

  @Override
  public String toString() {
    return String.valueOf(i) + j;
  }
}


这篇关于在我错的地方需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆