Java第一次实训作业

以下是我的实训作业

第一个程序(判断奇偶数):

 1 package javaApplication;
 2 import java.util.*;
 3 public class Odevity 
 4 {
 5     public static void main(String[] args) 
 6     {
 7         Scanner scanner=new Scanner(System.in);
 8         int x;
 9         System.out.println("请输入一个数:");
10         x=scanner.nextInt();
11         if(x%2==0)
12         {
13             System.out.println("这个数是偶数");
14         }
15         else
16         {
17             System.out.println("这个数是奇数");
18         }
19         scanner.close();
20     }    
21 }

运行结果如下:

第二个程序(求圆的面积):

 1 package javaApplication;
 2 import java.util.*;
 3 public class Circle1 
 4 {
 5     final static double PI=3.14159;
 6     public static void main(String[] args)
 7     {
 8         Scanner scanner=new Scanner(System.in);
 9         int r;
10         double s=0;
11         System.out.println("请输入半径:");
12         r=scanner.nextInt();
13         
14         s=PI*r*r;
15         System.out.printf("面积为:%.2f",s);
16         scanner.close();    
17     }
18 }

运行结果如下:

第三个程序(加密数字):

 1 package javaApplication;
 2 import java.util.Scanner;
 3 public class Encipher
 4 {
 5     public static void main(String[] args)
 6     {
 7         Scanner scanner=new  Scanner(System.in);
 8         System.out.println("请输入要加密的数:");
 9         int x=scanner.nextInt();
10         int y=(int)((x*10+5)/2+3.14159);
11         System.out.println("加密后为:"+y);
12         scanner.close();
13     }
14 }

运行结果如下:

第四个程序(公鸡、母鸡、小鸡):

 1 package javaApplication;
 2 
 3 public class Chicken
 4 {
 5     public static void main(String[] args) 
 6     {
 7         System.out.println("可能的方案为:");
 8         for(int i=0;i<20;i++) //i表示公鸡
 9         {
10             for(int j=0;j<33;j++) //j表示母鸡
11             {
12                 for(int l=0;l<100;l+=3) //l表示小鸡
13                 {
14                     if((i*5+j*3+l/3.0==100)&&(j+i+l==100))
15                     {
16                         System.out.printf("公鸡%d只   母鸡%d只   小鸡%d只",i,j,l);
17                         System.out.println();
18                     }
19                 }
20             }
21         }    
22     }
23 }

运行结果为:

第五个程序(不同且不重复的三位数):

 1 package javaApplication;
 2 
 3 public class ThreeDigitNumber 
 4 {
 5     public static void main(String[] args)
 6     {
 7         
 8         int n=0;
 9         for(int i=1;i<=4;i++) //i表示百位
10         {
11             for(int j=1;j<=4;j++) //j表示十位
12             {
13                 if(i==j)
14                 {
15                     continue;
16                 }
17                 for(int l=1;l<=4;l++) //l表示各个位
18                 {
19                     if(l==j||l==i) 
20                     {
21                         continue;
22                     }
23                     n++;
24                     System.out.print((i*100+j*10+l)+" ");
25                     if(n%6==0)
26                     {
27                         System.out.println();
28                     }    
29                 }
30             }
31         }
32         System.out.println("共有"+n+"个不同的三位数。");    
33     }
34 
35 }

运行结果:

第六个程序(求素数):

 1 package javaApplication;
 2 import java.util.Scanner;
 3 public class Prime
 4 {
 5     public static void main(String args[])
 6     {
 7         Scanner scanner=new Scanner(System.in);
 8         System.out.println("请输入一个数:");
 9         int x=scanner.nextInt();
10         for(int i=2;i<x;i++)
11         {
12             
13             if(x%i==0)
14             {
15                 System.out.println("该数不是素数!");
16                 scanner.close();
17                 return ;
18             }
19         }    
20         System.out.println("该数是素数!");    
21         scanner.close();        
22     }    
23 }

(注意该程序中的两处scanner.close() )

运行结果如下:

猜你喜欢

转载自www.cnblogs.com/duwenze/p/10650445.html