Java基础练习小程序

时间:2023-02-13 13:58:03

One

 

/*

需求:测试你的体重是否标准。

         标准体重:22*身高(米)的平方

         体重指数:体重(公斤)/身高(米)的平方

         正常体重:体重指数18-25

         超    重:体重指数25-30

         轻度肥胖:体重指数>30

         中度肥胖:体重指数>35

         重度肥胖:体重指数>40

*/

 

import java.io.*;

public class BMI

{

         publicstatic void main(String[] args) throws IOException

         {

                   //以下六句是用来读取键盘上输入的数据

                   InputStreamReaderreader=new InputStreamReader(System.in);

                   BufferedReaderbf= new BufferedReader(reader);                                     //创建字符输入流对象.

                  

                  

                   System.out.print("请输入您的身高(米):");

                   Stringh = bf.readLine();

                   System.out.print("请输入您的体重(公斤):");

                   Stringw = bf.readLine();

                  

                  

                   //将从 键盘接收的数据转换为Ddouble型

                   doubleheight = Double.parseDouble(h);

                   doubleweight = Double.parseDouble(w);

                   doublebztz = 22*height*height;

                   doubletzzs = weight/(height*height);

                   System.out.println("\t您的标准体重为:"+(int)bztz);

                   System.out.println("\t您的体重指数为:"+(int)tzzs);

                   if(tzzs>=18&& tzzs<=25)

                   {

                            System.out.println("\t您的体重标准!");

                   }

                   elseif (tzzs>25 && tzzs<=30)

                   {

                            System.out.println("您的体重超重!");

                   }

                   elseif (tzzs>30 && tzzs<=35)

                   {

                            System.out.println("您的体重轻度肥胖!");

                   }

                   elseif (tzzs>35 && tzzs<=40)

                   {

                            System.out.println("您的体重中度肥胖!");

                   }

                   elseif  (tzzs>45)

                   {

                            System.out.println("您的体重重度肥胖!");

                   }

                   else

                   {

                            System.out.println("输入数据错误!");

                   }

         }

        

}

two

 

class HanShu

{

         publicstatic void main(String[] args)

         {

                   //System.out.println("HelloWorld!");

                   JuXing(10,10);

                   System.out.println("--------------------");

                   PX(4,5);

                   WZ();

         }

 

         /*需求:构造一个函数,使其打印输出一个任意行列的矩形。

           思路:1.确定结果,没有,所以返回值为void;

                            2.是否有未知数,有,因为行和列不确定;

         */

         publicstatic void JuXing(int a,int b)

         {

          for (int x=a;x>0 ;x-- )

          {

                    for (int y=b;y>0 ;y-- )

                    {

                             System.out.print("*");

                    }

                   System.out.println();

          }

 

         }

   /*

         需求:打印输出一个平开四边形!

         */

                   publicstatic void PX(int a,int b)

         {

          for (int x=a;x>0 ;x-- )

          {

                    for (int n=1;n<x ;n++ )

                    {

                             System.out.print(" ");

                    }

                    for (int y=b;y>0 ;y-- )

                    {

                             System.out.print("*");

                    }

                    

                   System.out.println();

          }

         System.out.println("--------------------");

          for (int x=a;x>0 ;x-- )

          {

                    for (int n=a-x;n>0 ;n-- )

                    {

                             System.out.print(" ");

                    }

                    for (int y=b;y>0 ;y-- )

                    {

                             System.out.print("*");

                    }

                    

                   System.out.println();

          }

 

 

         }

 

         /*

         未知惊喜!

         */

         publicstatic void WZ()

         {

                   for(intx=0;x<5;x++)

                   {

                   for(inty=0;y<=x;y++)

                            {

                            System.out.print("*");

                            }

                            System.out.println();

                   }

                  

                   System.out.println("---------------------");

 

 

                   for(intx=1;x<6;x++)

                   {

                   for(inty=1;y<=x;y++)

                            {

                            System.out.print(y);

                            }

                            System.out.println();

                   }

                  

                   System.out.println("---------------------");

 

 

                   for(intx=1;x<10;x++ )

                   {

                   for(inty=1;y<=x;y++ )

                            {

                            System.out.print(y+"*"+x+"="+x*y+"\t");

                            }

                            System.out.println();

                   }

 

                   System.out.println("---------------------");

 

                   for(intx=1;x<=5;x++)

                   {

                            for(inty=5;y>=x;y--)

                            {

                                     if(y== x)

                                     {

                                     for(int l=1;l<=x ; l++)

                                     {

                                               System.out.print("*");

                                              

                                     }

                                     break;

                                     }

                            System.out.print("-");

 

                            }

                            System.out.println();

                   }

        

                  

         }

 

 

}

 

Three

class  Exchange

{

       public static void main(String[] args)

       {

              //toB(6);

              //toHex(-123);

              //toBin(64);

              sl(0);

       }

      

       //需求:十进制转二进制。

       public static void er(int num)

       {

              trans(num,1,1);

       }

       //需求:十进制转八进制。

 

       public static void ba(int num)

       {

              trans(num,7,3);

       }

              //需求:十进制转十六进制。

 

       public static void sl(int num)

       {

              trans(num,15,4);

       }

 

 

       public static void trans(int num,intx,int y)

       {

              if (num==0)

              {

                     System.out.println('0');

                     return ;

              }

              char ch []={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

              char []arr=new char [32];

              int pos=arr.length;

              while (num!=0)

              {

                     int temp=num & x;

                     arr[--pos]=ch[temp];

                     num=num>>>y;

              }

              for (int n=pos;n<arr.length;n++ )

              {

                     System.out.print(arr[n]);

              }

       }

      

 

 

   //需求:十进制转二进制查表法。

       public static void toBin(int num)

       {

              char ch[]={'0','1'};

              char arr[]=new char [32];

              int pos=arr.length;

              while (num!=0)

              {

                     int temp=num %2;

                     arr[--pos]=ch[temp];

                     num=num>>>1;

              }

              for (int x=pos;x<arr.length ;x++)

              {

                     System.out.print(arr[x]);

              }

       }

 

              //需要:十进制利用查表法转换成十六进制。

              /*

              分析:1.创建一个数组用来存储十六进制元素表

                      2.创建一个指针,用来指向转换后数据在数组表中的下标

                       3..创建另一个数组用来记录所输入数据转换成十六进制后在数组元素表中数据                 

              */  

       public static void toHex(int num)

       {

              char ch[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

              char arr []=new char [8];

              int poss=arr.length;

              while (num!=0)

              {

                     int temp=num & 15;

                     arr[--poss]=ch[temp];

                     num= num >>> 4;

              }

 

                     for (intx=poss;x<arr.length ;x++ )

                     {

                            System.out.print(arr[x]);

                     }

       }

 

      

      

       //需求:十进制-->十六进制。

       public static void toH(int num)

       {

              StringBuffer sb=newStringBuffer();

              for (int x=0;x<8 ;x++ )

              {

                     int temp=num & 15;

                     if (temp>9)

                     {

                            sb.append((char)(temp-10+'A'));

                     }

                     else

                     {

                            sb.append(temp);

                     }

                     num=num >>> 4;

              }

              System.out.println(sb.reverse());

       }

 

       //需求:十进制-->二进制。

       public static void toB(int num)

       {

              StringBuffer sb=newStringBuffer();

              while (num>0)

              {

                     //System.out.print(num%2);

                     sb.append(num%2);

                     num=num/2;

              }

              System.out.println(sb.reverse());

       }

}

Four

class MaoPao

{

       publicstatic void main(String[] args)

       {

              intx[]={1,2,3,6,55,4,9,8};

              maopao(x);

              for(int i=0;i<x.length ;i++ )

              {

                     if(i!=x.length-1)

              {

                     System.out.print(x[i]+",");

              }

              else

              {

                     System.out.println(x[i]);

              }

              }

       }

 

       //需求:冒泡排序。

 

       publicstatic void maopao(int[] x)

       {

              for(int a=x.length-1;a>=0 ;a-- )

              {

                     for(int b=0;b<a ;b++ )

                     {

                            if(x[b]<x[b+1])

                            {

                                   swap(x,b,b+1);

                            }

                     }

              }

       }

 

       publicstatic void swap(int []arr,int a,int b)

       {

              inttemp=arr[a];

              arr[a]=arr[b];

              arr[b]=temp;

       }

}

Five

/*

【程序3FindDaffodilNumber.java

题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:

153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

java.lang.Math.pow(a, b);

*/

 

 

 

class FDN

{

       publicstatic void main(String[] args)

       {

              Sxh(100,999);

       }

 

 

       publicstatic void Sxh(int x , int y)

       {

              intge,shi,bai;

              doublehe;

              //int[]sz=new int[y-x];

              for(;x<=y ;x++ )

              {

                     bai=x/100;

                     shi=(x-bai*100)/10;

                     ge=x-bai*100-shi*10;

                     he=java.lang.Math.pow(bai,3)+java.lang.Math.pow(shi,3)+java.lang.Math.pow(ge, 3);

                     //he=bai*bai*bai+shi*shi*shi+ge*ge*ge;

                     if(he==x)

                     {

                            System.out.print(x+"");

                     }

              }

       }

}

Six

classMaoPao

{

       public static void main(String[] args)

       {

              int x[]={1,2,3,6,55,4,9,8};

              maopao(x);

              for (int i=0;i<x.length ;i++ )

              {

                     if (i!=x.length-1)

              {

                     System.out.print(x[i]+",");

              }

              else

              {

                     System.out.println(x[i]);

              }

              }

       }

 

       //需求:冒泡排序。

 

       public static void maopao(int[] x)

       {

              for (int a=x.length-1;a>=0 ;a--)

              {

                     for (int b=0;b<a ;b++ )

                     {

                            if (x[b]<x[b+1])

                            {

                                   swap(x,b,b+1);

                            }

                     }

              }

       }

 

       public static void swap(int []arr,inta,int b)

       {

              int temp=arr[a];

              arr[a]=arr[b];

              arr[b]=temp;

       }

}