void 方法中的return语句,跳出的范围是他所在的上层方法的块,不是整个类,也不是他的上一级括号

例子:

代码
package com.java.test;

publicclass T {
int countNumber=4;

publicvoid test(){
if(countNumber >0)
{
countNumber
--;
System.out.println(countNumber);
return;
}
System.out.println(
"test");
}

publicvoid test1(){
if(countNumber >0)
{
countNumber
--;
System.out.println(countNumber);
return;
}
System.out.println(
"test1");
}
publicstaticvoid main(String args[]){
T test
=new T();
test.test();
test.test1();
System.out.println(
"haha");
}
}

 

输出结果:

3
2
haha

相关文章:

  • 2021-06-30
  • 2021-11-16
  • 2021-10-24
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2023-03-30
  • 2022-12-23
  • 2021-09-17
  • 2021-09-05
相关资源
相似解决方案