第5章 while循环结构 和do—while循环结构 课后作业:


  1.   用while循环 100,95,90,85...........5
public class Exerises {
	public static void main(String[] args) {
	 int i = 100;
	 while(i>=5) {
		 System.out.println(i);
		 i-=5;
	 }
   }
}

2.计算1~50中7的倍数的数值:
 public class Exeirses1 {
public static void main(String[] args) {
 int i =1;
 int h =0;
 do {
 if(i%7==0) {
 h+=+i;   //被7整除的数之和
System.out.println(i);
 }            //if结构判断
 i++;
 }while(i <= 50);
        System.out.println(h);
}
}

3.从键盘输入一批整数,比较其中的最大值和最小值,输入0结束循环。
public class Exerises2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int max =0 ;
int min =0 ;
while(true){
System.out.println("请输入一个整数(输入0结束):");
int num = input.nextInt();
if(num>max) {
max = num;
}else if(num==0) {
break;
}else if(num
4.从键盘输入一位整数,当输入1~7时,显示下面对应的英文星期名称的缩写。
import java.util.Scanner;
public class Exeirses3 {
public static void main(String[] args) {
   Scanner input = new Scanner(System.in);  
          String day ="";
          System.out.print("请输入数字1~7(输入0结束):");     
          num = input.nextInt();
         while(num!=0) {
        if(num>=1&&num<=7) {          
        switch(num){
        case 1:
        day = "MON";
        System.out.println("今天是\t"+day);
        break;
        case 2:
        day = "TUE";
        System.out.println("今天是\t"+day);
        break;
        case 3:
        day = "WED";
        System.out.println("今天是\t"+day);
        break;
        case 4:
        day = "THU";
        System.out.println("今天是\t"+day);
        break;
        case 5:
        day = "FRI";
           System.out.println("今天是\t"+day);
           break;
        case 6:
        day = "SAT";
        System.out.println("今天是\t"+day);
        break;
        case 7:
        day = "SUN";
           System.out.println("今天是\t"+day);
           break;
           }
        }else{
        System.out.println("输入有误,请重新输入!!");
        } 
         System.out.print("请输入数字1~7(输入0结束):");    
                   num = input.nextInt();
            } 
         System.out.print("程序结束!");
}    

}


你可能感兴趣的:(第5章 while循环结构 和do—while循环结构 课后作业:)