您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Java中,if条件语句和循环语句可以结合使用,以便根据特定条件执行某些操作。以下是一些常见的结合使用if条件语句和循环语句的示例:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
public class Main {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
if (i % 2 == 0) {
sum += i;
}
i++;
}
System.out.println("Sum of even numbers between 1 and 100: " + sum);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int i = 2;
boolean isPrime = true;
do {
if (num % i == 0) {
isPrime = false;
}
i++;
} while (i <= num / 2);
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
这些示例展示了如何在循环中使用if条件语句来根据特定条件执行操作。你可以根据需要修改这些示例,以便在你的项目中使用。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。