java实验作业1

  1 //1已知圆的半径为10,求其周长及面积
  2 package calsswork3;
  3 
  4 public class test3_1 {
  5   //求周长
  6     public static double C(int r) {
  7       return 2*r*Math.PI;
  8     }
  9     //求面积
 10     public static double S(int r) {
 11       return r*r*Math.PI;
 12     }
 13   public static void main(String[] args) {
 14     // TODO Auto-generated method stub
 15         int r=10;
 16         System.out.println("圆的周长是:"+C(r));
 17         System.out.println("圆的面积是:"+S(r));
 18   }
 19 
 20 }
 21 //2根据自由落体公式s=v0t+gt*2/2,求解s,其中v0=4m/s,g=9.8,t=0.5
 22 package calsswork3;
 23 
 24 public class test3_2 {
 25     //位移公式
 26     public static double s(int v,double g, double t) {
 27       return v*t+g*t*t/2;
 28     }
 29   public static void main(String[] args) {
 30     // TODO Auto-generated method stub
 31       int v=4;
 32       double g=9.8;
 33       double t=0.5;
 34       System.out.println("s="+s(v,g,t));
 35   }
 36 
 37 }
 38 //3输入一个整数判断其奇偶性
 39 package calsswork3;
 40 
 41 import java.util.Scanner;
 42 
 43 public class test3_3 {
 44     //判断函数
 45   public static void pd(int x) {
 46     if(x%2==0) {
 47       System.out.println(x+"是偶数");
 48     }
 49     else {
 50       System.out.println(x+"是奇数");
 51     }
 52   }
 53   public static void main(String[] args) {
 54     // TODO Auto-generated method stub
 55       System.out.println("请输入一个整数:");
 56       Scanner in=new Scanner(System.in);
 57       int x=in.nextInt();
 58       pd(x);
 59   }
 60 
 61 }
 62 //4输入三角形的三边长a,b,c判断是否构成三角形.
 63 //如果是三角形,计算出三角形的面积
 64 package calsswork3;
 65 import java.util.Scanner;
 66 public class test3_4 {
 67 
 68     //判断三边能否构成三角形
 69   public static int panduan(double a,double b,double c) {
 70        int f;
 71       if(a>0&&b>0&&c>0&&(a-b)<c&&(a+b)>c&&(a-c)<b&&(a+c)>b&&(b-c)<a&&(b+c)>a) {
 72         f=1;
 73       }
 74       else {
 75         f=0;
 76       }
 77       return f;
 78         
 79     }
 80     //计算其面积
 81   public static double mianji(double a,double b,double c) {
 82     double s,l;
 83     l=(a+b+c)/2;
 84     s=Math.sqrt(l*(l-a)*(l-b)*(l-c));
 85     return s;
 86   }
 87   public static void main(String[] args) {
 88     double a,b,c,s;
 89     int q;
 90     System.out.println("请输入三条边长");
 91     Scanner in=new Scanner(System.in);
 92     a=in.nextDouble();
 93     b=in.nextDouble();
 94     c=in.nextDouble();
 95     q=panduan(a, b, c);
 96     if(q==1) {
 97       s=mianji(a, b, c);
 98       System.out.println("三角形的面积为:"+s);
 99           
100     }
101     else {
102       System.out.println("输入边长有误,其不构成三角形");
103     }
104   }
105 
106 }
107 //5某幼儿园只收2~6岁的小孩,2岁入小班,3~4岁入中班,5~6岁入大班
108 //输入小孩岁数,输出入哪类班
109 package calsswork3;
110 
111 import java.util.Scanner;
112 
113 public class test3_5 {
114     //年龄和班级匹配,用switch语句
115   public static String pp(int x) {
116     String z="";
117     switch(x) {
118     case 0:
119     case 1:{z="年龄不符合,不招生";break;}
120     case 2:{z="小班";break;}
121     case 3:
122     case 4:{z="中班";break;}
123     case 5:
124     case 6:{z="大班";break;}
125     default:{z="年龄不符合,不招生";}
126       
127     }
128     return z;
129   }
130   public static void main(String[] args) {
131     // TODO Auto-generated method stub
132       System.out.println("请输入小孩的年龄:");
133       Scanner in=new Scanner(System.in);
134       int x=in.nextInt();
135       System.out.println("该小孩应入的班级为:"+pp(x));
136   }
137 
138 }
139 //6用if实现下面的计算功能
140 package calsswork3;
141 
142 import java.util.Scanner;
143 
144 public class test3_6 {
145     //if函数
146   public static double jisuan1(double x) {
147     if(x<0) {
148       return 2*x+1;
149     }
150     else if(x>=10) {
151       return 4*x-4;
152     }
153     else {
154       return x*x;
155     }
156   }
157   public static void main(String[] args) {
158     // TODO Auto-generated method stub
159       System.out.println("请输入一个数据:");
160       Scanner in=new Scanner(System.in);
161       double x=in.nextDouble();
162       System.out.println("计算后的值为:"+jisuan1(x));
163   }
164 
165 }
166 //7输入三个整数,从大到小输出
167 package calsswork3;
168 
169 import java.util.Scanner;
170 
171 public class test3_7 {
172     public static void sort(int a,int b,int c) {
173       int max=b,min=a;
174       if(a>b) {
175          max=a;
176          min=b;
177       }
178       if(c>max) {
179         max=c;
180       }
181       if(min>c) {
182         min=c;
183       }
184       System.out.println("从大到小的分别为:");
185       System.out.println(max+" "+(a+b+c-max-min)+" "+min);
186     }
187   public static void main(String[] args) {
188     // TODO Auto-generated method stub
189         System.out.println("请输入三个整数:");
190         Scanner in=new Scanner(System.in);
191         int a,b,c;
192         a=in.nextInt();
193         b=in.nextInt();
194         c=in.nextInt();
195         sort(a, b, c);
196   }
197 
198 }
199 //8输入三个数,求其平均值,输出大于平均值的数
200 package calsswork3;
201 
202 import java.util.Scanner;
203 
204 public class test3_8 {
205     //求平均值
206   public static double avg(double a,double b,double c) {
207     return (a+b+c)/3.0;
208   }
209   //判断是否大于平均值
210   public static void pd(double a,double b,double c) {
211     double avg=avg(a,b,c);
212     if(a>avg) {
213       System.out.println(a+"大于平均值");
214     }
215       if(b>avg) {
216       System.out.println(b+"大于平均值");
217     }
218       if(c>avg) {
219       System.out.println(c+"大于平均值");
220     }
221   }
222   public static void main(String[] args) {
223     // TODO Auto-generated method stub
224        System.out.println("请输入三个数:");
225        Scanner in=new Scanner(System.in);
226        double a,b,c;
227        a=in.nextDouble();
228        b=in.nextDouble();
229        c=in.nextDouble();
230        System.out.println("这三个数的平均值为:"+avg(a, b, c));
231        pd(a, b, c);
232   }
233 
234 }
235 //9随机产生50个1~100之间的整数,输出其中最小的数
236 package calsswork3;
237 
238 public class test3_9 {
239   public static int suiji(int n) {
240     int a[]=new int[n];//用数组保存随机产生的数
241     System.out.println("随机产生的"+n+"个数:");
242     for(int i=0;i<n;++i) {
243       int rnum=(int)(Math.random()*100+1);//random的范围是[0,1)      
244       System.out.println(rnum);
245       a[i]=rnum;
246     }
247     int min=100;
248     //求最小数
249     for(int i=0;i<a.length;++i) {
250       if(min>a[i]) {
251         min=a[i];
252       }
253     }
254     return min;
255   }
256   public static void main(String[] args) {
257     int n=50;
258     System.out.println("最小数为:"+suiji(n));
259   }
260 }
261 //10求100以内所有奇数和(分别用for,do-while,while循环实现)
262 package calsswork3;
263 
264 public class test3_10 {
265     //求奇数和
266   //for循环实现
267   public static int oddsum1(int n) {
268     int i,t=0;
269     for(i=0;i<n;++i) {
270       if(i%2!=0) {
271         t+=i;
272       }
273     }
274     return t;
275   }
276   //do-while循环实现
277   public static int oddsum2(int n) {
278     int i=0,t=0;
279     do {
280       if(i%2!=0) {
281         t+=i;
282       }
283       ++i;
284     }
285     while(i<n);
286     return t;
287   }
288   //while循环实现
289   public static int oddsum3(int n) {
290     int i=0,t=0;
291     while(i<n) {
292       if(i%2!=0) {
293         t+=i;
294       }
295       ++i;
296     }
297     return t;
298   }
299   public static void main(String[] args) {
300     // TODO Auto-generated method stub
301        int n=100;
302        System.out.println("100以内的奇数和为:"+oddsum1(n));
303        System.out.println("100以内的奇数和为:"+oddsum2(n));
304        System.out.println("100以内的奇数和为:"+oddsum3(n));
305   }
306 
307 }
308 //11输出100~200间所有不能被3和4整除的数
309 package calsswork3;
310 
311 public class test3_11 {
312     public static void zhengchu() {
313       for(int i=100;i<=200;++i) {
314         if(i%3!=0&&i%4!=0) {
315           System.out.println(i);
316         }
317       }
318     }
319   public static void main(String[] args) {
320     // TODO Auto-generated method stub
321         System.out.println("100~200间所有不能被3和4整除的数");
322         zhengchu();
323   }
324 
325 }
326 //12找出100~200间满足其各位数字之和等于6
327 package calsswork3;
328 
329 public class test3_12 {
330   public static void shuzihe() {
331       for(int i=100;i<=200;++i) {
332         if(i%10+i/10%10+i/100==6) {
333           System.out.println(i);
334         }
335       }
336     }
337   public static void main(String[] args) {
338     // TODO Auto-generated method stub
339       System.out.println("100~200间满足其各位数字之和等于6的数为:");
340       shuzihe();
341   }
342 
343 }
344 //13输入包括两个运算量和一个运算符的算术表达式,计算并输出运算结果
345 package calsswork3;
346 
347 import java.util.Scanner;
348 
349 public class test3_13 {
350     public static void fun(double x,char a,double y) {
351       switch(a) {
352       case '+':{
353         System.out.println(x+"+"+y+"="+(x+y));break;
354       }
355       case '-':{
356         System.out.println(x+"-"+y+"="+(x-y));break;
357       }
358       case '*':{
359         System.out.println(x+"*"+y+"="+(x*y));break;
360       }
361       case '/':{
362         System.out.println(x+"/"+y+"="+(x/y));break;
363       }
364       }
365     }
366   public static void main(String[] args) {
367     // TODO Auto-generated method stub
368        System.out.println("请输入运算符和数值,如:a + b");
369        double x,y;
370        char a;
371        Scanner in=new Scanner(System.in);
372        x=in.nextDouble();
373        a=in.next().charAt(0);
374        y=in.nextDouble();     
375        fun(x, a, y);
376   }
377 
378 }
379 //14输入年份和月份,输出该月的天数
380 package calsswork3;
381 
382 import java.util.Scanner;
383 
384 public class test3_14 {
385     public static String tianshu(int y,int m) {
386       String z="";
387       switch(m) {
388       case 1:
389       case 3:
390       case 5:
391       case 7:
392       case 8:
393       case 10:
394       case 12:{
395         z="该月有31天";break;
396       }
397       case 2:
398         if(y%4==0&&y%100!=0||y%400==0) {
399           z="该月有29天";break;
400         }
401         else {
402           z="该月有28天";break;
403         }
404       case 4:
405       case 6:
406       case 9:
407       case 11:{
408         z="该月有30天";break;
409       }
410       }
411       return z;
412     }
413   public static void main(String[] args) {
414     // TODO Auto-generated method stub
415      System.out.println("请输入年份和月份:");
416      Scanner in=new Scanner(System.in);
417      int y=in.nextInt();
418      int m=in.nextInt();
419      System.out.println(tianshu(y, m));
420   }
421 
422 }
423 //15分别用for,while,do-while三种循环语句计算一个正数的阶乘
424 package calsswork3;
425 
426 import java.util.Scanner;
427 
428 public class test3_15 {
429     //for循环实现
430   public static long jiecheng1(int x) {
431     long t=1;
432     if(x==0) {
433       return t;
434     }
435     for(int i=1;i<=x;++i) {
436       t=t*i;
437     }
438     return t;
439   }
440   //while循环实现
441   public static long jiecheng2(int x) {
442     long t=1;
443     int i=1;
444     if(x==0) {
445       return t;
446     }
447     while(i<=x) {
448       t*=i;
449       ++i;
450     }
451     return t;
452   }
453   //do-while循环实现
454   public static long jiecheng3(int x) {
455     long t=1;
456     int i=1;
457     if(x==0) {
458       return t;
459     }
460     do {
461       t*=i;
462       ++i;
463     }
464     while(i<=x);
465     return t;
466   }
467   public static void main(String[] args) {
468     // TODO Auto-generated method stub
469        System.out.println("请输入一个正数:");
470        Scanner in=new Scanner(System.in);
471        int x=in.nextInt();
472        System.out.println(x+"的阶乘结果为:"+jiecheng1(x));
473        System.out.println(x+"的阶乘结果为:"+jiecheng2(x));
474        System.out.println(x+"的阶乘结果为:"+jiecheng3(x));
475   }
476 
477 }
478 //16设计程序,从输入数据中统计正整数和负整数的个数.用输入0来结束
479 package calsswork3;
480 
481 import java.util.Scanner;
482 
483 public class test3_16 {
484     public static void jishu(int n) {
485     int i=0,j=0;
486     while(n!=0) {
487       if(n>0) {
488         ++i;
489       }
490       else {
491         ++j;
492       }
493       Scanner in=new Scanner(System.in);
494         n=in.nextInt();
495     }
496     System.out.println("正数的个数为:"+i);
497     System.out.println("负数的个数为:"+j);
498   }
499   public static void main(String[] args) {
500     // TODO Auto-generated method stub
501        System.out.println("请输入一段数据并以0结束:");
502        Scanner in=new Scanner(System.in);
503      int n=in.nextInt();
504        jishu(n);
505   }
506 
507 }

 

posted @ 2020-11-21 19:05  丁帅帅dss  阅读(183)  评论(0)    收藏  举报