while语句-案例题
···java
1、求1-100的和
public class tt1 {
public static void main(String[] args) {
//求1-100的和
int i = 1, j = 0;
while (i <= 100) {
j = i + j;
i++;
}
System.out.println("1-100的和为:" + j);
}
}
2、求1-100奇数的和
public class tt2 {
public static void main(String[] args) {
//求1-100奇数的和
int i = 1, j = 0;
while (i % 2 != 0 && i <= 100) {
j = i + j;
i += 2;
}
System.out.println("1-100奇数和为:" + j);
}
}
3、求1-100偶数的和
public class tt2 {
public static void main(String[] args) {
//求1-100奇数的和
int i = 1, j = 0;
while (i % 2 != 0 && i <= 100) {
j = i + j;
i += 2;
}
System.out.println("1-100奇数和为:" + j);
}
}
4、求1-100中10的倍数的数值
public class tt4 {
public static void main(String[] args) {
//求1-100中10的倍数的数值
int i = 1;
while (i <= 100) {
if (i % 10 == 0) {
int j = i;
System.out.println(j);
}
i++;
}
}
}
5、求1-100中奇数和与偶数和的差
public class tt5 {
public static void main(String[] args) {
//求1-100中奇数和与偶数和的差
int i = 1, j = 0, z = 0;
while (i <= 100) {
if (i % 2 != 0) {
j = i + j;
} else {
z = i + z;
}
i++;
}
System.out.println("1-100中奇数和与偶数和的差为:" + (j - z));
}
}
6、求1-100中5的倍数的数值的个数
public class tt6 {
public static void main(String[] args) {
//求1-100中5的倍数的数值的个数
int i = 0, j = 0;
while (i <= 100) {
if (i % 5 == 0) {
j++;
}
i++;
}
System.out.println(j);
}
}
7、求1-100中偶数的个数
public class tt7 {
public static void main(String[] args) {
//求1-100中偶数的个数
int i = 0, j = 0;
while (i <= 100) {
if (i % 2 == 0) {
j++;
}
i++;
}
System.out.println(j);
}
}
8、求1-100中能被5和4整除的数值
public class tt8 {
public static void main(String[] args) {
//求1-100中能被5和4整除的数值
int i = 1;
while (i <= 100) {
if (i % 5 == 0 && i % 4 == 0) {
int j = i;
System.out.println(j);
}
i++;
}
}
}
9、求1-100中的奇数,且能被3整除的数
public class tt9 {
public static void main(String[] args) {
//求1-100中的奇数,且能被3整除的数
int i = 1;
while (i <= 100) {
if (i % 2 != 0 && i % 3 == 0) {
int j = i;
System.out.println(j);
}
i++;
}
}
}
10、求1-100中的偶数,且能被8整除的偶数的个数
public class tt10 {
public static void main(String[] args) {
//求1-100中的偶数,且能被8整除的偶数的个数
int i = 1, j = 0;
while (i <= 100) {
if (i % 2 == 0 && i % 8 == 0) {
j++;
}
i++;
}
System.out.println(j);
}
}
···