break 的用法很简单,就是循环结构中的一条语句:
break;
public class TestBreak {
public static void main(String args[]) {
int [] num = {10, 20, 30, 40, 50};
//遍历数组
for(int x : num ) {
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
}
//输出结果
System.out.print( x );
System.out.print("\n");
}
}
}
10
20
continue 就是循环体中一条简单的语句:
continue;
public class TestContinue {
public static void main(String args[]) {
int [] num = {10, 20, 30, 40, 50};
//遍历数组
for(int x : num ) {
if( x == 30 ) {
continue;
}
//输出结果
System.out.print( x );
System.out.print("\n");
}
}
}
10
20
40
50
//推荐
return 与方法相同类型的变量;
//不推荐
return;
public class TestReturn {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入数1:");
double num1 = input.nextDouble(); // 获取用户输入的数1
System.out.println("请输入数2:");
double num2 = input.nextDouble(); // 获取用户输入的数2
double d = sum(num1, num2);
System.out.println(num1 + "+" + num2 + "=" + d);
}
/**
* 创建sum() 方法,返回double 类型的数值
i 操作数1
j 操作数2
两个操作数之和
*/
public static double sum(double i, double j) {
double sum = i + j;
return sum;
}
}
运行后的结果如下所示:
请输入操作数1:
500
请输入操作数2:
203
500.0+203.0=703.0
请输入操作数1:
5.66
请输入操作数2:
3.0158
5.66+3.0158=8.6758
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有