窗移影月吧 关注:50贴子:2,181

每日java作业

只看楼主收藏回复


人但有恒,事无不成 -----曾国藩


IP属地:湖南1楼2014-10-25 08:30回复
    import java.util.Scanner;
    public class InputCode_1 {
      public static void main(String[] args)
      {
         Scanner scanner = new Scanner(System.in);
         System.out.println("请输入您的身份证号");
         String line = scanner.nextLine();//打印对输入文本的描述
         System.out.println("原来你的身份证号是" + line.length() + "位数字啊!\n");
      }
    }
    运行结果:
    =====================
    请输入您的身份证号
    430514199406056555
    原来你的身份证号是18位数字啊!
    =====================


    IP属地:湖南2楼2014-10-25 08:32
    回复
      2025-06-15 07:48:33
      广告
      import java.util.Scanner;
      public class ParityCheck {
        public static void main(String[] args)
        {
           Scanner scan = new Scanner(System.in);
           System.out.println("请输入一个整数:");
           long number = scan.nextLong();
           String check = (number%2 == 0)?"这个数是偶数":"这个数是: 奇数";
           System.out.println(check);
        }
      }
      运行结果:
      =====================
      请输入一个整数:
      65
      这个数是: 奇数
      =====================


      IP属地:湖南3楼2014-10-25 08:34
      回复
        /*
        需求:根据编程语言确认员工号分配部门
        */
        import java.util.Scanner;
        public class Example {
          public static void main(String[] args)
          {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入员工姓名:");
            String name = scanner.nextLine();
            System.out.println("请输入员工应编程的语言:");
            String language = scanner.nextLine();
            //根据编程语言确认员工号分配部门
            switch(language.hashCode())
            {
              case 3254818: //java的哈希码
              case 2301506: //java的哈希码
              case 2269730: //java的哈希码
                 System.out.println("员工" + name + "被分配到JAVA程序开发部门");
                 break;
              case 3104: //C#的哈希码
              case 2112: //C#的哈希码
                 System.out.println("员工" + name + "被分配到C#程序开发部门");
                break;
              case -709190099: //Asp.net的哈希码
              case 955469181: //Asp.net的哈希码
              case 9745901: //Asp.net的哈希码
                System.out.println("员工" + name + "被分配到Asp.net程序开发部门");
                 break;
             default:
                 System.out.println("本公司不需要" + language + "语言的程序开发人员");
             }
          }
        }
        运行结果:
        =====================
        请输入员工姓名:
        诸葛亮
        请输入员工应编程的语言:
        java
        员工诸葛亮被分配到JAVA程序开发部门
        =====================


        IP属地:湖南4楼2014-10-25 08:37
        回复
          更精确的使用浮点数
          /*
          目的:熟悉scanner的用法
          */
          import java.math.BigDecimal;
          public class AccuraateryFloat {
            public static void main(String[] args)
            {
              double money = 2;
              double price = 1.1;
              double result = money - price;
              System.out.println("非精确计算");
              System.out.println("剩余金额" + result);
              //精确浮点数的解决方法
              BigDecimal money1 = new BigDecimal("2");
              BigDecimal price1 = new BigDecimal("1.1");
              BigDecimal result1 = money1.subtract(price1);
              System.out.println("精确计算");
              System.out.println("剩余金额" + result1);
            }
          }
          /*
          运行结果是
          =============================================
          非精确计算
          剩余金额0.8999999999999999
          精确计算
          剩余金额0.9
          =============================================
          */


          IP属地:湖南5楼2014-10-25 08:41
          回复
            不用乘法运算符实现2X16
            /*
            日期:2014/10/16 14:25:45
            目的:<<左移运算的活运用
            */
            import java.util.Scanner;
            public class Exanple {
               public static void main(String[] args)
               {
                 Scanner scanner = new Scanner(System.in);
                 System.out.println("请输入一个数字:");
                 long number = scanner.nextLong();
                 System.out.println("您输入的数字是: " + number);
                 System.out.println("该数字乘以2的结果是: " + (number<<1));
                 System.out.println("该数字乘以4的结果是: " + (number<<2));
                 System.out.println("该数字乘以8的结果是: " + (number<<3));
                 System.out.println("该数字乘以16的结果是: " + (number<<4));
               }
            }
            /*
            运行结果是
            =============================================
            请输入一个数字:
            45
            您输入的数字是: 45
            该数字乘以2的结果是: 90
            该数字乘以4的结果是: 180
            该数字乘以8的结果是: 360
            该数字乘以16的结果是: 720
            =============================================
            */
            知识要点:
            一个整数每次执行位移运算中的左移运算N次,相当于这个数乘以2的N次方


            IP属地:湖南7楼2014-10-25 08:44
            回复
              判定某年是否为闰年
              /*
              目的:条件语句的使用
              */
              import java.util.Scanner;
              public class LeapYear {
                public static void main(String[] args)
                {
                  Scanner scanner = new Scanner(System.in);
                  System.out.println("请输入一个年份");
                  long year = scanner.nextLong();
                  if (year%4 == 0 && year%100 != 0 ||year%400 == 0)
                  {
                    System.out.println(year + "是闰年");
                  } else
                  {
                    System.out.println(year + "不是闰年");
                  }
                }
              }
              /*
              运行结果是
              =============================================
              请输入一个年份
              2009
              2009不是闰年
              =============================================
              */


              IP属地:湖南8楼2014-10-25 08:45
              回复
                验证登录信息的合法性
                /*
                目的:equals的运用
                */
                import java.util.Scanner;
                public class CheckLogin {
                  public static void main(String[] args)
                  {
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入您的账号: ");
                    String username = scanner.nextLine();
                    System.out.println("请输入您的密码: ");
                    String password = scanner.nextLine();
                    if (!username.equals("剑南")){
                       System.out.println("账号输入错误");
                       } else if (!password.equals("窗移影月")){
                         System.out.println("密码输入错误");
                         } else {
                         System.out.println("输入正确,欢迎您");
                       }
                  }
                }
                运行结果是
                =============================================
                请输入您的账号:
                剑南
                请输入您的密码:
                窗移影月
                输入正确,欢迎您
                =============================================
                */
                知识要点:
                字符串属于对象而非基本数据类型,不能用“==”来判断两个字符串是否相当,所以需要equals()方法来判断两个字符串内容是否相同。


                IP属地:湖南9楼2014-10-25 08:47
                回复
                  2025-06-15 07:42:33
                  广告
                  用Switch语句根据消费者消费金额计算折扣
                  /*
                  目的:switch的使用
                  */
                  public class ProductPrice {
                     public static void main(String[] args)
                     {
                       float money = 1260;
                       float rebate = 0f;
                       if(money > 200)
                       {
                         int grade = (int) (money / 200);
                         switch(grade)
                         {
                           case 1:
                             rebate = 0.95f;
                             break;
                           case 2:
                             rebate = 0.90f;
                             break;
                           case 3:
                             rebate = 0.85f;
                             break;
                           case 4:
                             rebate = 0.83f;
                             break;
                           case 5:
                             rebate = 0.80f;
                             break;
                           case 6:
                             rebate = 0.78f;
                             break;
                           case 7:
                             rebate = 0.75f;
                             break;
                           case 8:
                             rebate = 0.73f;
                             break;
                           case 9:
                             rebate = 0.70f;
                             break;
                           case 10:
                             rebate = 0.65f;
                             break;
                           default:
                             rebate = 0.60f;
                             break;
                          }
                        }
                  System.out.println("您的累计消费金额是: " + money);
                        System.out.println("您将享受" + rebate + "折优惠");
                      }
                  }
                  运行结果是
                  =============================================
                  您的累计消费金额是: 1260.0
                  您将享受0.78折优惠
                  =============================================
                  */


                  IP属地:湖南10楼2014-10-25 08:50
                  回复
                    判断用户输入月份的季节
                    /*
                    目的:switch的运用复习
                    */
                    import java.util.Scanner;
                    public class JudgeMonth {
                       public static void main(String[] args)
                       {
                         Scanner scanner = new Scanner(System.in);
                         System.out.println("请输入月份");
                         int month = scanner.nextInt();
                         System.out.println("请输入月份是" + month);
                         switch(month)
                         {
                           case 12:
                           case 1:
                           case 2:
                              System.out.println("您输入的月份属于冬天");
                              break;
                           case 3:
                           case 4:
                           case 5:
                              System.out.println("您输入的月份属于春天");
                              break;
                           case 6:
                           case 7:
                           case 8:
                              System.out.println("您输入的月份属于夏天");
                              break;
                           case 9:
                           case 10:
                           case 11:
                              System.out.println("您输入的月份属于秋天");
                              break;
                           default:
                              System.out.println("您输入的月份有错误");
                         }
                       }
                    }
                    运行结果是
                    =============================================
                    请输入月份
                    10
                    请输入月份是10月
                    您输入的月份属于秋天
                    =============================================


                    IP属地:湖南11楼2014-10-25 08:52
                    回复
                      用for循环输出杨辉三角
                      /*
                      日期:2014/10/17 7:56:47
                      目的:遍历二维数组
                      */
                      public class YanghuiTriangle {
                         public static void main(String[] args)
                         {
                           int triangle[][] = new int[7][];
                           //遍历二维数组的第一层
                           for (int i=0; i<triangle.length; i++)
                           {
                              triangle[i] = new int[i+1];
                              //遍历第二层数组
                              for (int j=0; j<=triangle[i].length-1; j++)
                              {
                                 if (i==0 || j==0 ||j==triangle[i].length-1){
                                   triangle[i][j] = 1;
                                   }else {
                                     triangle[i][j] =triangle[i-1][j] + triangle[i-1][j-1];
                                   }
                                   //输出组元素
                                 System.out.print(triangle[i][j] + " ");
                                 }
                                 System.out.println();
                              }
                         }
                      }
                      /*
                      运行结果是
                      =============================================
                      1
                      1 1
                      1 2 1
                      1 3 3 1
                      1 4 6 4 1
                      1 5 10 10 5 1
                      1 6 15 20 15 6 1
                      =============================================
                      */


                      IP属地:湖南13楼2014-10-25 08:56
                      回复
                        for循环输出空心菱形
                        /*
                        目的:熟悉掌握for循环
                        */
                        public class Diamond {
                           public static void main(String[] args)
                           {
                             printHollowRhombus(10);
                           }
                           public static void printHollowRhombus(int size)
                           {
                             if (size%2 == 0)
                             {
                               size = size + 1;
                             }
                             for (int i=0; i<size/2+1; i++)
                             {
                               for (int j=size/2+1; j>i+1; j--)
                               {
                                  System.out.print(" ");
                               }
                               for (int j=0; j<2*i+1; j++)
                               {
                                 if (j==0 || j==i*2)
                                   System.out.print("*");
                                  else
                                   System.out.print(" ");
                               }
                               System.out.println();
                             }
                             for (int i=size/2+1; i<size; i++)
                             {
                               for (int j=0; j<i-size/2; j++)
                               {
                                 System.out.print(" ");
                               }
                               for (int j=0; j<2*size-1-2*i; j++)
                               {
                                 if (j==0 || j==2*(size-i-1))
                                   System.out.print("*");
                                 else
                                  System.out.print(" ");
                               }
                               System.out.println(" ");
                             }
                           }
                        }
                        运行结果是
                        ===========================
                        *
                        * *
                        * *
                        * *
                        * *
                        * *
                        * *
                        * *
                        * *
                        * *
                        *
                        ===========================


                        IP属地:湖南15楼2014-10-25 09:01
                        回复
                          使用嵌套循环在控制台上输出九九乘法表
                          /*
                          目的:switch的使用
                          */
                          import java.util.ArrayList;
                          import java.util.List;
                          public class UserForeach {
                            public static void main(String[]args)
                            {
                               List<String>list = new ArrayList<String>();
                               list.add("abc");
                               list.add("def");
                               list.add("ghi");
                               list.add("jkl");
                               list.add("mno");
                               list.add("pqr");
                               list.add("stu");
                               System.out.print("foreach遍历集合: \n\t");
                               for (String string : list)
                               {
                                 System.out.print(string);
                               }
                               System.out.println();
                               String[] strs = new String[list.size()];
                               list.toArray(strs);
                               System.out.print("foreach遍历数组: \n\t");
                               for (String string : strs)
                               {
                                 System.out.print(string);
                               }
                            }
                          }
                          /*
                          运行结果是
                          ============================================================
                          foreach遍历集合:
                          abcdefghijklmnopqrstu
                          foreach遍历数组:
                          abcdefghijklmnopqrstu
                          =================================================================
                          */
                          知识要点:
                          foreach循环式for循环的一种简写格式,只用于遍历数据集合或数组,语法如下:
                          for(Type e:collections)
                          参数说明:
                          e:其类型Type是集合和数组中元素值的类型,该参数是集合或出租collections的一个元素。
                          collections:要遍历集合或数组,也可以是迭代器。


                          IP属地:湖南16楼2014-10-25 09:04
                          回复
                            终止循环体
                            /*
                            目的:break的使用
                            */
                            public class BreakCyc {
                               public static void main(String[] args)
                               {
                                 System.out.println("\n------------------中断循环的例子-------------------");
                                 //创建数组
                                 String[] array = new String[] {"柯南", "毛利兰", "服部平次", "远山和叶","毛利小五郎", "虹          猫", "蓝兔", "灰太狼"};
                                 for (String string : array)
                                   System.out.print(string + " ");
                                 System.out.println();
                                 System.out.println();
                                 System.out.println("这些卡通人物属于《名侦探柯南》的有:");
                                 for (String string : array)
                                 {
                                    if (string.equals("虹猫"))
                                    break;
                                    System.out.print(string + " ");
                                 }
                               }
                            }
                            /*
                            运行结果是
                            ============================================================
                            ------------------中断循环的例子-------------------
                            柯南 毛利兰 服部平次 远山和叶 毛利小五郎 虹猫 蓝兔 灰太狼
                            这些卡通人物属于《名侦探柯南》的有:
                            柯南 毛利兰 服部平次 远山和叶 毛利小五郎
                            =================================================================
                            */
                            知识要点:
                            foreach循环式for循环的一种简写格式,只用于遍历数据集合或数组,语法如下:
                            for(Type e:collections)
                            参数说明:
                            e:其类型Type是集合和数组中元素值的类型,该参数是集合或出租collections的一个元素。
                            collections:要遍历集合或数组,也可以是迭代器。


                            IP属地:湖南17楼2014-10-25 09:06
                            回复
                              2025-06-15 07:36:33
                              广告
                              循环体的过滤器
                              /*
                              日期:2014/10/18 12:49:17
                              目的:continue语句的使用
                              */
                              public class CycFilter {
                                 public static void main(String[] args)
                                 {
                                   //创建数组
                                   String[] array = new String[]{"白鹭", "丹顶鹤", "黄莺", "鹦鹉", "乌鸦", "喜鹊","乌鸦", "百灵鸟", "乌鸦", "灰纹鸟", "喜鹊"};
                                   System.out.println("小明家的公园有很多鸟类,但他不喜欢乌鸦,请您为他赶走");
                                   int CrowCount = 0;
                                   for(String string : array)
                                   {
                                     if (string.equals("乌鸦"))
                                     {
                                        System.out.println("发现一只乌鸦,已赶走");
                                        CrowCount ++;
                                        continue;
                                     }
                                     System.out.println("搜集鸟类,发现了: " + string);
                                   }
                                   System.out.println("一共赶走了:" + CrowCount + "只乌鸦");
                                 }
                              }
                              /*
                              运行结果是
                              ============================================================
                              小明家的公园有很多鸟类,但他不喜欢乌鸦,请您为他赶走
                              搜集鸟类,发现了: 白鹭
                              搜集鸟类,发现了: 丹顶鹤
                              搜集鸟类,发现了: 黄莺
                              搜集鸟类,发现了: 鹦鹉
                              发现一只乌鸦,已赶走
                              搜集鸟类,发现了: 喜鹊
                              发现一只乌鸦,已赶走
                              搜集鸟类,发现了: 百灵鸟
                              发现一只乌鸦,已赶走
                              搜集鸟类,发现了: 灰纹鸟
                              搜集鸟类,发现了: 喜鹊
                              一共赶走了:3只乌鸦
                              =================================================================
                              */
                              知识要点:
                              循环体中可以通过break语句来终止整个循环,但对于某些特殊的如需放弃部分循环处理而不是整个循环体,则需要使用语句continue,continue可以放弃本次循环的其他代码,不执行它们而开始下一轮循环。


                              IP属地:湖南18楼2014-10-25 09:08
                              回复