continue 继续, con全部 tin拿住 + ue,全部拿住,做事拿到尾,继续。
return
当return用在方法时,表示退出该方法,用在main时,表示退出程序。
这和C也是一致的,不过在C中不叫方法,叫函数。(这句话不太确定)
public class HW01 {
public static void main(String[] args) {
double money = 100000;
int count = 0;
while (money > 0) {
if (money > 50000) {
money *= 0.95;
} else {
money -= 1000;
}
++count;
}
System.out.println("次数是 " + count);
}
}
这题我欠考虑了,当钱<1000时,不能交1000元了。
改
public class HW01 {
public static void main(String[] args) {
double money = 100000;
int count = 0;
while (money >= 1000) {
if (money > 50000) {
money *= 0.95;
} else {
money -= 1000;
}
++count;
}
System.out.println("次数是 " + count);
}
}
money >= 1000的时候才考虑交钱。
我的
import java.util.Scanner;
public class HW04 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int num = myScanner.nextInt();
int temp = num;
int sum = 0;
int n = 0;
while (temp != 0) {
n = temp % 10;
sum += n * n * n;
temp /= 10;
}
if (sum == num) {
System.out.println("是水仙花数");
} else {
System.out.println("不是水仙花数");
}
}
}
老师
import java.util.Scanner;
public class HW04 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int num = myScanner.nextInt();
int n1 = num % 10;
int n2 = num % 100 / 10;
int n3 = num / 100;
if (n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3 == num) {
System.out.println("Y");
} else {
System.out.println("N");
}
}
}
思路最重要!
public class HW06 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; ++i) {
if (i % 5 != 0) {
System.out.print(i + "\t");
++count;
if (count % 5 == 0)
System.out.println();
}
}
}
}
注意这个i + '\t'和i + "\t"的区别
i + '\t'是i的值和\t的unicode码相加等到的数字,而""引起来的才是字符串连接
我的
public class HW07 {
public static void main(String[] args) {
for (int i = 0; i <= 25; ++i) {
System.out.print((char)(97 + i) + " ");
System.out.println((char)(65 + i) + " ");
}
}
}
老师
public class HW07 {
public static void main(String[] args) {
for (char c = 'a'; c <= 'z'; ++c) {
System.out.print(c + " ");
}
System.out.println();
for (char c = 'Z'; c >= 'A'; --c) {
System.out.print(c + " ");
}
}
}