鱼C论坛

 找回密码
 立即注册
查看: 3174|回复: 4

[学习笔记] Java暑期学习Day04

[复制链接]
发表于 2017-7-8 23:42:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
第四天啦,希望能坚持下去


位运算的左移(<<)
    如127 (十进制)——补码和原码一样:0111 1111(二进制)
                          (正整数的补码等于其原码;
                              负整数求原码是其取反后加一<符号位不变>)
<a. 十进制整数转换为二进制整数:十进制整数转换为二进制整数采用"除2取余,逆序排列"法。
  b.十进制小数转换成二进制小数采用"乘2取整,顺序排列"法。
  具体做法是:用2乘十进制小数,可以得到积,将积的整数部分取出,再用2乘余下的小数部分,又得到一个积,再将积的整数部分取出,如此进行,直到积中的小数部分为零,此时0或1为二进制的最后一位。或者达到所要求的精度为止。
  c.已知一个数的补码,求原码的操作其实就是对该补码再求补码:
⑴如果补码的符号位为“0”,表示是一个正数,其原码就是补码。
⑵如果补码的符号位为“1”,表示是一个负数,那么求给定的这个补码的补码就是要求的原码。>


<<      :     左移运算符,num << 1,相当于num乘以2,num << 2,相当于num乘以2再乘以2
>>      :     右移运算符,num >> 1,相当于num除以2,num >> 2,相当于num除以2再除以2
  1. public class D170708 {

  2.         public static void main(String[] args) {
  3.                 int i = 127;
  4.                 int number = 1;
  5.                 int b = i >> number;
  6.                 System.out.println(b);// 63
  7.                 int a = -127;
  8.                 int number2 = 1;
  9.                 int c = a >> number2;
  10.                 System.out.println(c);// -64
  11.         }

  12. }
复制代码


将++或--运算符写在变量前,表示先将变量值加或减1,然后再返回变量值;
                                           后,       先返回变量值           ,然后再对变量加或减1
  1. <font size="4">public class D170708 {

  2. public static void main(String[] args) {
  3. int i=0;
  4. i++;
  5. System.out.println(i);//1
  6. i--;
  7. System.out.println(i);//0
  8. ++i;
  9. System.out.println(i);//1
  10. --i;
  11. System.out.println(i);//0
  12.       
  13. int number=0;
  14. number=++i;
  15. System.out.println(number);//1   相当于i=i+1; number=i
  16.       number=--i;
  17. System.out.println(number);//0
  18. number=i++;
  19. System.out.println(number);//0    相当于number=i; i=i+1
  20.       number=i--;
  21. System.out.println(number);//1    相当于number=i;i=i-1
  22.   }  
  23. }</font>
复制代码

指定运算: &=     ——    a&=b  —— a=a&b
    (求与,同为真1为1,同为0为0,不同为0)
                      >>=   ——    a>>=b —— a=a>>b


类型转换:  在浮点数后加一个F,表示float类型;<若不加,系统默认double类型>
                        在整数后加一个L,表示long类型;<若不加,系统默认int类型>
  1. <div><font size="4"><b>public class D170708 {</b></font></div><div><font size="4"><b> public static void main(String[] args) {
  2.   System.out.println("Integer.MAX_VAlUE=" + Integer.MAX_VALUE);//<font color="red">Integer.MAX_VAlUE=2147483647
  3. </font>  System.out.println("Integer.MIN_VAlUE=" + Integer.MIN_VALUE);//<font color="red">Integer.MIN_VAlUE=-2147483648</font>
  4.   int i = 2147483647;
  5.   i++;
  6.   System.out.println(i);// <font color="red">-2147483648
  7. </font>  i--;
  8.   System.out.println(i);//<font color="red"> 2147483647</font></b></font></div><div><font size="4"><b>  int a=-2147483648;
  9.   a--;
  10.   System.out.println(a);//<font color="red">2147483647</font>
  11.   a++;
  12.   System.out.println(a);//<font color="red">-2147483648</font></b></font></div><div><b><font size="4">
  13. </font></b></div>
复制代码


1.  switch语法架构:


      switch(变量或表达式){


  case整数、字符、字符串或Enum(枚举):
  描述句;
  break;
case整数、字符、字符串或Enum:
  描述句;
  break;

  ...
default(不一定需要):
   描述句;
}

2.  for(初始值;执行结果必须是boolean的重复式;重复式){
        描述句;
         }
  <初始式只执行一次>
  1. <font size="4">public class D170708 {

  2.         public static void main(String[] args) {
  3.                 /*
  4.                  * 求1000以内的素数 质数(prime number)又称素数,有无限个。
  5.                  * 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数。
  6.                  */
  7.                 int s = 0;
  8.                
  9.                 for (int i = 2; i <= 1000; i++) {
  10.                         int g = 1;
  11.                        
  12.                          for (int j = 2;<b> </b><font color="red"><b>j < i</b></font>; j++) {
  13.                                 if (<b><font color="red">i % j == 0</font></b>) {
  14.                                         g = 0;
  15. <font color="red"><b>                                        break;
  16. </b></font>                                }

  17.                         }
  18.                          
  19.                         if (g == 1) {
  20.                                 System.out.print(i + <b>"\n"</b>);
  21. <font color="red"><b>                                s++;</b></font>
  22.                         }
  23.                        
  24.                 }
  25.                
  26.                
  27.         }

  28. }</font>
复制代码


  3. while(条件式){
     描述句;
      } //前测试循环


        do{
         描述句;}while(条件式);

break:结束类似于switch、for、while、do...while的区块、并执行区块后一个描述句
     continue:只略过之后描述句,并回到循环区块开头进行下一次循环


评分

参与人数 3荣誉 +10 鱼币 +10 收起 理由
jiacce + 5 + 5 支持楼主!
零度非安全 + 2 + 2 支持楼主!
小甲鱼 + 3 + 3 热爱鱼C^_^

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-9 00:09:38 | 显示全部楼层
支持马太效应
这名字怎么怪怪的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-9 00:22:48 | 显示全部楼层
零度非安全 发表于 2017-7-9 00:09
支持马太效应
这名字怎么怪怪的

这是一种社会心理现象
之前看一个例子说一个人被人夸奖后专注坚持于一个领域,然后取得了长期优秀的成绩
借此名希望我能坚持下去!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-9 00:25:55 | 显示全部楼层
  1. <div><font size="5">public class D170709 {
  2. public static void main(String[] args) {
  3.   /*
  4.    * 求1000以内的素数 质数(prime number),有无限个。
  5.    * 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。
  6.    */</font></div><div><font size="5">  for (int i = 2; i <= 1000; i++) {
  7.    int g = 1;</font></div><div><font size="5">   for (int j = 2; j < i; j++) {</font></div><div><font size="5">    if (i % j == 0) {
  8.      g = 0;
  9.      break;
  10.     }</font></div><div><font size="5">   }</font></div><div><font size="5">   if (g == 1) {
  11.     System.out.print(i + "\n");</font></div><div><font size="5">   }</font></div><div><font size="5">  }</font></div><div><font size="5"> }</font></div><div><font size="5">}
  12. </font>
  13. </div>
复制代码

我发现可以不用多加一个变量就可以求取1000以内的素数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-9 00:54:00 | 显示全部楼层
  1. public class D170709 {
  2.         public static void main(String[] args) {
  3.                 /*
  4.                  * 求1000以内的素数 质数(prime number),有无限个。
  5.                  * 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。
  6.                  */

  7.                 for (int i = 2; i <= 1000; i++) {
  8.                         int g = 1;

  9.                         for (int j = 2; j < i; j++) {

  10.                                 if (i % j == 0) {
  11.                                         g = 0;
  12.                                         break;
  13.                                 }

  14.                         }

  15.                         if (g == 1) {
  16.                                 System.out.print(i + "\n");

  17.                         }

  18.                 }

  19.         }

  20. }
复制代码

补充九九乘法表,又一个for的嵌套循环,希望我能彻底掌握嵌套循环。
  1. public class D170709 {
  2.         public static void main(String[] args) {
  3.                 for (int j = 1; j < 10; j++) {
  4.                         for (int i = 2; i < 10; i++) {
  5.                                 System.out.printf("%2d*%2d=%2d", i, j, i * j);
  6.                         }
  7.                         System.out.println();
  8.                 }

  9.         }
  10. }
复制代码
  print:  方法必须有参数;不能直接换行   printf: 格式化输出(有参数)   println:方法可以没有参数;可以实现换行






想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 17:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表