章节一
import java.util.Scanner;
public class homework2 {
public static void main(String[] args){
double money=100000;
int count=0;
while (true){
if (money>50000){
money=money-money*(0.05);
count++;
}
else if(money<=50000&money>1000){
money-=1000;
count++;
}
else {
break;
}
}
System.out.println("总共100000,"+"剩余"+money+",一共可以过"+count+"个路口");
}
}
什么也不输出。很多同学错误的认为输出"OK2"。
public class homework2_6 {
public static void main(String[] args){
int count=0;
for (int i=1;i<=100;i++){
if(i % 5!=0){
count++;
System.out.print(i+"\t");
if (count % 5==0){
System.out.println();
}
}
}
}
}
在这里有一个注意点,System.out.
print()
与 System.out.println()
是有区别的哦!
System.out.print不换行
,System.out.println输出之后会自动换行
。
public class homework2_7 {
public static void main(String[] args) {
for (char c = 'a'; c <= 'z'; c++) {
System.out.print(c + " ");
}
System.out.println( " ");
for (char c1= 'Z'; c1 >= 'A'; c1--) {
System.out.print(c1 + " ");
}
}
}
public class HOMEWORK2_8 {
public static void main(String[] args){
double sum=0;
for (int j=1;j<=100;j++){
if (j%2!=0){
sum+=(1.0/j);
}
else {
sum-=(1.0/j);
}
}
System.out.println(sum);
}
}
一共又100个数字,分子为1,分母从1-100;
当分母为奇数时,前面是+,当分母为偶数时,前面是-;
可用for+判断即可完成
把结果存放在double sum
这里有一个隐藏的陷阱,要把公式1写成1.0才能得到精确的结果