java有关循环的作业

练习1(基础):
输入数量不确定的正数和负数(一次输入一个),
然后打印正数和负数的个数,当输入0时,程序停止
练习2(基础):
使用while循环方式打印100以内4的倍数
练习3(基础):
要求用户输入用户名和密码,只要不是admin和111就提示用户名或密码不正确,请重新输入。正确时结束循环,提示登陆成功。
练习4(基础):
已知判断闰年的规则是:能被4整除,但是不能被100整除。或者能被400整除。打印从1900年至2050年期间所有的闰年。
练习5(基础):
输入序号选择不用的功能
效果如下:

练习6(基础):
编写程序,设计一个图平面积计算,选择不同的图形输入数字计算面积,选择4退出程序。
效果如下:

练习7(基础):
不断要求用户输入一个数字(假定用户输入的数字都是正整数),当用户输入end的时候显示刚才输入数字中的最大值

练习8(选作):
有一只猴子摘了一堆桃子,每天吃掉一半桃子还不过瘾再多吃一个,吃到第十天的时候发现还剩下一个桃子,问猴子第一天一共摘了多少个桃子?。
解题思路:
第10天:桃子数是:1
第9天:桃子数是:(1+1)*2=4
第8天:桃子数是:(4+1)*2=10

总结:前一天的桃子数=(后一天的桃子数+1)*2

public class HomeworkTest {

	public static void main(String[] args) {
//		  Test1();
//		  Test2();
//		  Test3();
//		  Test4();
//		  Test5();
//        Test6();
//		  Test7()
		  Test8();
	}
    public static void Test1(){
    	/*输入数量不确定的正数和负数(一次输入一个),
    	然后打印正数和负数的个数,当输入0时,程序停止*/
    	System.out.println("请输入任意数量的正数或负数:");
    	Scanner sc=new Scanner(System.in);
    	int input=sc.nextInt();
    	int count1=0;
    	int count2=0;
        while(input!=0){
        
    		 if(input>0){
    			 count1++;
    		 }else{
    			 count2++;
    		 }
        input=sc.nextInt();
       	}
        System.out.println("正数个数为:   "+count1+"负数个数为:"+count2);
    }
   
    
    public static void Test2(){
//    	使用while循环方式打印100以内4的倍数
    	int x=1;
        while(x<100){
              if(x%4==0){
    		  System.out.println(x);
              }
       	      x++;   		
    	 }
    }
    public static void Test3(){
    	/*要求用户输入用户名和密码,
    	只要不是admin和111就提示用户名或密码不正确,
    	请重新输入。
    	正确时结束循环,提示登陆成功。*/
    	System.out.println("请输入用户名:");
    	Scanner sc=new Scanner(System.in);
    	String admin=sc.next();
    	System.out.println("请输入密码:");
    	String  pwd=sc.next();
    	while(!admin.equals("admin")||!pwd.equals("111")){
     		System.out.println("用户名或密码不正确,请重新输入!");
     		System.out.println("请输入用户名:");
        	 sc=new Scanner(System.in);
        	 admin=sc.next();
        	System.out.println("请输入密码:");
        	 pwd=sc.next();
    	}
    	System.out.println("登陆成功!");
    	
    }
    public static void Test4(){
    	/*已知判断闰年的规则是:能被4整除,但是不能被100整除。
    	或者能被400整除。打印从1900年至2050年期间所有的闰年。*/
    	int year=1900;
    	while(year<=2050){
    		if((year%4==0&&year%100!=0)||year%400==0){
    			System.out.println(year);
    		}
    		year++;
    	}
    }
    public static void Test5(){
       	Scanner sc=new Scanner(System.in);
        int num=0;
    	while(num<3){
    		System.out.println("*******************");
        	System.out.println("1.管理员   2.顾客    3.退出");
        	System.out.println("*******************");
            System.out.println("请输入数字选择登陆:");
            num=sc.nextInt();
            if(num==1){
            	System.out.println("管理员登陆过程省略");
            }else if(num==2){
            	System.out.println("顾客登陆过程省略");
            }else{
            	System.out.println("系统退出");
            }
    	}
     }
    public static void Test6(){
    	
        Scanner sc=new Scanner(System.in);
        int shape=0;
        while(shape<4){
          	System.out.println("计算面积");
        	System.out.println("1.矩形  "+"2.三角形  "+"3.圆  "+"4.退出");
            System.out.println("请选择数字对应的功能:");
            shape=sc.nextInt();
        	if(shape==1){
               System.out.println("你选择了矩形");
       	       System.out.println("请输入矩形的宽:");
       	       double  jw=sc.nextDouble();
       	       System.out.println("请输入矩形的高:");
       	       double  jh=sc.nextDouble();
       	       double s=jw*jh;
       	       System.out.println("该矩形的面积为:"+s);
            }else if(shape==2){
       	       System.out.println("请输入三角形的底:");
       	       double  sw=sc.nextDouble();
       	       System.out.println("请输入三角形的高:");
       	       double  sh=sc.nextDouble();
       	       double s=(sw*sh)/2;
       	       System.out.println("该三角形的面积为:"+s);
            }else if(shape==3){
       	       System.out.println("请输入圆的半径:");
       	       double  yr=sc.nextDouble();
       	       double s=yr*yr*3.14;
       	       System.out.println("该三角形的面积为:"+s);
           }else{
        	System.out.println("退出计算");
        }
        	
      }
   }
   public static  void Test7(){
	   /*不断要求用户输入一个数字(假定用户输入的数字都是正整数),
	   当用户输入end的时候显示刚才输入数字中的最大值*/
	   
 	Scanner sc=new Scanner(System.in);
   	String input="0";
   	int max=0;
   	System.out.println("请输入数字:");
          while(!input.equals("end")){
        	  input=sc.next();
        	  if(input.equals("end")){
        		 System.out.println(max);
        	  }else{
          	    
   		        max = Integer.parseInt(input) >= max ? Integer.parseInt(input) : max;
        	  }
   	   	}
     
      }
     public static void Test8(){
    	
    	 int day=10;
    	 int peach=1;
    	while(day!=1){
    		peach=(peach+1)*2;
    		day--;
    		
    	}
         System.out.println(peach);
   }



}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值