java学习—day_3


1.再定义Long或float类型变量的时候,要加L或者F
整数默认是int类型,浮点数默认是double类型

byte,short在定义的时候,他们接收的其实是一个int类型的值
这个是自己做得数据检测,如果不在他们的范围内,就会报错。

2.
128:0B1_00_00_00_00
-128:0B1_00_00_00_00

3.数据类型转化之默认转换:byte(1),short(2),char(2)--int(4)--long(8)--float(4)-double
long :8
float:存储机制不同;底层存储结构不同
float的存储范围比long大

4.java中的字符char可以存储一个汉字吗?
可以,因为char是2个字节

1.运算(operate)符
算数运算符:+ - * / % —— ++
int x = 3;把3赋值给x;
整数相除只能得到整数
赋值运算符:
比较运算符:
逻辑运算符:
位运算符:
三目运算符:

++,--的运算;
+:加法,正号,字符串连接符;
赋值运算符:03.06

比较运算符

逻辑运算符:& | ! ^  && || 用于连接boolean表达式
表达式,就是运算符把常量或者变量链接起来的java语法式子

& 与 &&的区别:
&& 和 || 的区别,
&&有一个短路的作用,其结果与&相同,开发中常用的是&&

位运算:$ | ^ ~
^的特点:对一个数据异或两次,该数本身不变
<< >> >>> 

如何实现两个数交换位置:
方式一:使用第三方变量,
int a = 3;
int b = 4;
int c = a;
a = b;
b = c;
方式二:用位运算,用异或运算来实现
a = a ^ b;
b = a ^ b;
a = a ^ b;


<<左移,右边补零
 >>右移:最高位是0,左边补齐0,最高位是一,左边补齐1
 >>>无符号右移

方式三:
a = a + b;
b = a - b;
a = a - b;

方式四:
b = (a+b) - (a=b);


运算符,三目运算符 z = (a > b) ? 3 :4;

用于比较两个数中最大时得值

键盘数据的录入,为了实现程序的灵活性
1.导包:
格式:import java.util.Scanner;
 位置在class上面
import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner v = new Scanner(System.in);
  System.out.println("input a number");
  int z = v.nextInt();
  System.out.println(z);
 }
//求两个数的和,导包
import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner a = new Scanner(System.in);
  Scanner b = new Scanner(System.in);
  System.out.println("Enter a:");
  int a1 = a.nextInt();
  System.out.println("Enter a:");
  int b1 = b.nextInt();
  int c = a1 + b1;
  System.out.println(c);
 }
}

//
import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner a = new Scanner(System.in);
  System.out.println("enter first number:");
  int b1 = a.nextInt();
  System.out.println("enter second number:");
  int b2 = a.nextInt();
  System.out.println(b1 + b2);
 }
}

//import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("enter a");
  int a = sc.nextInt();
  System.out.println("int b");
  int b = sc.nextInt();
  boolean flag = (a == b);
  System.out.println(flag);
 }
}

//import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("enter a:");
  int a = sc.nextInt();
  System.out.println("enter b");
  int b = sc.nextInt();
  System.out.println("enter c");
  int c = sc.nextInt();
  int max = (((a > b) ? a:b) > c) ? ((a > b) ? a:b):c;
  System.out.println(max);
 }
}
流程控制语句:
顺序结构(order,sequence)
选择结构(choice,两种选择结构):
if (关系表达式)
{
 控制表达式;
}
 比较表达式无论简单还是复杂,结果必须是boolean类型
 控制语句表达式一个可以省略大括号,建议以后永远不要这样做;
{
这是代码块;
}
//
import java.util.Scanner;
class Choice
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("enter a number:");
  int a1 = sc.nextInt();
  System.out.println("enter a nummber:");
  int a2 = sc.nextInt();
  if(a1 == a2)
  {
   System.out.println("right");
  }
  else
   System.out.println("wrong");
 }

import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter a number");
  int x = sc.nextInt-6
  ();
  if(x >= 3)
  {
   System.out.println("y="+(2*x + 1));
  }
  else if(x >= -1)
  {
   System.out.println("y="+(2*x));
  }
  else
  {
   System.out.println("y="+(2*x - 1));
  }
 }
}
import java.util.Scanner;
class Day
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  int a = sc.nextInt();
  int b = sc.nextInt();;
  int c = sc.nextInt();;
  int max;
  if(a > b)
  {
   if(a > c)
   {
     max = a;
   }
   else
     max = c;
  }
  else
  {
   if(c < b)
   {
     max = b;
   }
   else
     max = c;
  }
  System.out.println("max = "+ max);
 }
}
switch
循环结构(loop)


































































你可能感兴趣的:(java学习之路)