package lesson05;
public class TestExample13 {
public void testMethod() {
// 使用for循环完成将1到100的相加
int sum = 0;
for (int i = 1; i < 101; i++) {
sum = sum + i;
}
System.out.println(\"1~100相加后的和为:\" + sum);
}
public void testMethod1() {
// 使用while循环完成1到1000的相加
int i = 1;
int sum = 0;
while (i < 101) {
sum = sum + i;
i++;
}
System.out.println(\"1~100相加后的和为:\" + sum);
}
public void testMethod2() {
// 使用do...while循环完成1到100的相加
int i = 1;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i < 101);
System.out.println(\"1~100相加后的和为:\" + sum);
}
public void testMethod3() {
// 使用For-Each循环完成1到100的输出
int tempArray[] = new int[100];
for (int i = 1; i < 101; i++) {
tempArray[i - 1] = i;
}
for (int k : tempArray) {
System.out.print(k + \" \");
}
}
public static void main(String[] args) {
TestExample13 te1 = new TestExample13();
te1.testMethod3();
}
}
package lesson05;
public class TestExample10 {
public void testMethod() {
int a = 10;
int b = 20;
if (((a - b) > b) || ((b - a) == b)) {
System.out.println(\"ok!\");
}
int c = 9999;
byte b1 = (byte)c; //精度的损失
System.out.println(b1);
float a1 = 3.1415926f;
int a2 = (int)a1;
System.out.println(a2);
double d = 'c';
System.out.println(d);
}
public static void main(String[] args) {
TestExample10 te1 = new TestExample10();
te1.testMethod();
}
}
package lesson05;
import java.util.Scanner;
public class TestExample11 {
public void testMethod() {
Scanner aa = new Scanner(System.in);
System.out.println(\"请输入你的性别(男/女):\");
String temp = aa.nextLine();
if (temp.equals(\"男\")) {
System.out.println(\"男生!\");
} else if (temp.equals(\"女\")) {
System.out.println(\"女生!\");
} else {
System.out.println(\"有没有搞错!\");
}
}
public void testMethod1() {
Scanner aa = new Scanner(System.in);
System.out.println(\"请输入星期<请使用英文缩写>:\");
String temp = aa.nextLine();
if (temp.equals(\"MON\")) {
System.out.println(\"星期一\");
} else if (temp.equals(\"TUE\")) {
System.out.println(\"星期二\");
} else if (temp.equals(\"WED\")) {
System.out.println(\"星期三\");
}else if (temp.equals(\"THU\")) {
System.out.println(\"星期四\");
}else if (temp.equals(\"FRI\")) {
System.out.println(\"星期五\");
}else if (temp.equals(\"SAT\")) {
System.out.println(\"星期六\");
}else {
System.out.println(\"星期日\");
}
}
public static void main(String[] args) {
TestExample11 te1 = new TestExample11();
te1.testMethod1();
}
}
package lesson05;
import java.util.Scanner;
public class TestExample12 {
public void testMethod() {
Scanner aa = new Scanner(System.in);
System.out.println(\"请输入1~7的数字表示星期:\");
int a = aa.nextInt();
switch (a) {
case 1:
System.out.println(\"星期一\");
break;
case 2:
System.out.println(\"星期二\");
break;
case 3:
System.out.println(\"星期三\");
break;
case 4:
System.out.println(\"星期四\");
break;
case 5:
System.out.println(\"星期五\");
break;
case 6:
System.out.println(\"星期六\");
break;
case 7:
System.out.println(\"星期日\");
break;
default:
System.out.println(\"错误!\");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestExample12 te1 = new TestExample12();
te1.testMethod();
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务