java学习笔记包含部分的代码

一.第一个代码

public class MyFirst{
    public static void main(String[] args){
     System.out.println("what");
    }

二./**
*简单的键盘输入 ctrl+c强制结束
*/

import java.util.Scanner;
public class Welcome{

    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        //要求输入用户名,年纪,薪水
        System.out.println("请输入用户名");
        String uname = s.nextLine();
        System.out.println("请输入年纪");
        int age=s.nextInt();
        System.out.println("请输入月薪");
        double monthSalary = s.nextDouble();


        System.out.println("用户名"+uname+"/t年纪"+age+"/t月薪");

        }


    }

三.循环写出乘法口诀


public class ChengFaKoJue {
    public static void main(String[] args){
    for(int i=1;i<=9;i++){
        for(int j=1;j<=i;j++){
            System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
        System.out.println();    
        }

    }

}


四.常量的认识

public class TesConstants{
      
  public static void main(String[] args){
               final double PI = 3.14;
              //PI = 3.1415926; 常量只能初始化一次,不能再更改

                double r = 3;
    double area = PI*r*r;
    double circle = PI*r*2;
    
    Syetem.out.println("面积是"+area);
    System.out.println("周长是"+circle);    

 

          }

  }

五.测试boolean

/**
*测试boolean类型
*/

public class TestBoolean{
    

    public static void main(String[] args){
        boolean b1 = true;
        boolean b2 = flase;

    if(b1){
    Syetem.out.println("I am fool");
    }
    else(){
    System.out.println("I am fool too")
    }      

 

 


}}

六.测试字符

public class TestChar{
    public static void main(String[] args){
    char c1 = 'a';
    char c2 = '中';
    char c3 = '\u0061;
    System.out.println(c1);
    System.out.println(c2);    
    System.out.println(c3);

 

//java 内部的字符串不是基本的数据类型
    string a1 = "浩然牛逼";
    System.out.println(a1);

    \\转义字符
    System.out.println(“c1/nc2/nc3/t//'”);

    }


        }

七.测试浮点数

/**
*测试浮点数的基本用法
*/

public class TestFloatNumber{

    public static void main(String[] args){
    double a1 = 3.14;
    double a2 = 3.14E2;//科学计数法表示小数
    System.out.println(a2);

    //float f1 = 1.65;因为1.65被视为double类型,所以无法向float直接转化    //
    float f1 = 1.65f;//其后可加f或者F即可
    
    float f2 = 0.1f;
    double f3 =1.0/10;
    System.out.println(f3);
    Syetem.out.println(f2==f3);//虽然数值相等,但是浮点数不精确,所以结果不一定正确
    }
}

八.测试整数

public class TestInt{
    public static void main(String[] args){
    byte a = 100;//整数
    int b = 5064054;//十进制
    int c = 05;//八进制
    int d = 0x15;//十六进制
    int e = 0b101011;//二进制

    long f = 555555;//若后面不加l,则视为整数类型
    long f2 = 555555555l;//加了l或者L。即为长整形

                      }    

        }

九.测试流程结构

/**
*流程控制结构:顺序结构.选择结构,循环结构
*/

public class Testjiegou {
    public static void main(String[] args){
    /*double d=Math.random();//返回0~1之间的小数
    System.out.println(d);
    
    int i= (int)6*Math.random()+1;//将随机获得的小数强转为整数,获得1~6整数
    System.out.println(i);
    if(i<=3){
        Syetem.out.println("small");
    }
*/

    System.out.println("#############");
    System.out.println("做人做事不可好高骛远,需脚踏实地,步步为营");
    System.out.println("投掷三骰子,看其总和");
        int a=(int)(6*Math.random())+1;
        int b=(int)(6*Math.random())+1;
        int c=(int)(6*Math.random())+1;
        int count=a+b+c;

    if(count>15){
        System.out.println("今天适合学习,无所不懂,无所不会");
        }

    if(count<=15&&count>=10){
        System.out.println("今天勉强适合学习,大部分都不懂,继续敲");
        }

    if(count<10){
        System.out.println("今天算了,游戏搞起来");
        }

        
    System.out.println("今日得分"+count);


}

}

十./*
*测试运算符
*/

public class TestOprator{

    public static void main(String[] args){
//对于整数运算,只要有long涉及,结果就一定为long。就算只有byte short,结果也一定为int
    int a = 2;
    long b = 4;
    long c = a+b;
    System.out.println(c);

//对于浮点运算,只要有double涉及,结果就一定为double.
    int d = 31/3;
    System.out.println(d);

//去余数
    int e = 10%3;
 
//自增自减
    int f =30;
    f++;//相当于f=f+1;
    f--;//同理
    
    int g =10;
    int h = g++;//先赋值,再自增
    int i = ++g;//先自增,再赋值
    
//扩展运算符
    int j = 9;
    int k = 8;
    j+=k;//等价于j=j+k;以此类推加减乘除取余同理
    System.out.println(b);

//关系运算符和逻辑运算符都会返回blooean,instanceof用来判断这个值是否包含在这个类中。逻辑与&,逻辑或|,短路与&&,短路或||
    boolean  l1 = true;
    boolean  l2 = flase;
    Syetem.out.println(l1&l2); //与:有一个flase为flase
    Syetem.out.println(l1|l2);   //或:有一个true为true
    Syetem.out.println(!l2);     //非:结果相反
    Syetem.out.println(l1^l2); //异或:相同flase,不同为true

//位运算符,是二进制的运算方法。^.&,|,~,<<,>>
    int m1 = 9;
    int m2 = 2;
    int m3 = 5<<2;//相当于5*2*2左移两位
    int m4 = 5>>2;//相当于5/2/2
    System.out.println(m1^m2);
    System.out.println(m1&m2);
    System.out.println(m1|m2);
    System.out.println(~m2);//涉及到补码

//测试字符串
    String n1 = "3 ";
    int n2 = 4;
    Syetem.out.println(n1+n2);//返回字符串

    char n3 ='s';
    char n4 ='b';
    Syetem.out.println(n3+n4);//字符串连接符必须是字符串,char会被当成整数来运算
    Syetem.out.println(""+n3+n4);//小技巧,这般就可输出“sb”

//条件运算符
    int score = 90;
    String o = score<60?"你死定了":"你将对得起人民";
    Syetem.out.println(o);

    if(score<60){
    a='你死定了';
    }else{
    a ='你将对得起人民';
    }

    //条件运算符可以嵌套
    int o1 =-100;
    int flag =o1>0?1:(o1==0?0:-1);
    
    //有关优先级的问题以小括号为主,不用刻意去背
    //易错点:逻辑非>逻辑与>逻辑或
    boolean p1=true,p2=true,p3=flase;
    System.out.println(p1||p2&&p3);//与的优先级高于或
    


}

 

 

}

十一.嵌套

/**
*嵌套循环
*
*/
public class Testqiantao{

    public static void main(String[] args){

        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
            System.out.print(i);//不换行
            }
         Syetem.out.println();//换行
        }
        
    }
}

十二.测试键盘输入

import java.util.Scanner;
/**
*测试获得键盘输入
*/

public class TestScanner{
    
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的名字");
        Sting name=scanner.nextLine();
        System.out.println(name);
        

}

    }

 

/*nextLine获得字符串
nextInt获得整数*/

 

十三.强转

/**
*数据类型的自动转换和强制转换
    前者容量小的可以向容量大的自动转换
     int和float可以无精度损失的转double
**/
public class TestTypeAutoConvert{
    
    public static void main(String[] args){
//前者容量小的可以向容量大的自动转换
    int a=2345;
    long b=a;
            //int c=b;//long 不能自动转换为int

 int和float可以无精度损失的转double
    double d=b;
    float f=b;

//特例整形常量是int可以自动转换为byte short char。只要不超过其对应的范围
    byte h1=123;
    //byte h2=1234;//1234超过了byte的范围{-128~127}

    char h3 =97;//0~65535
    System.out.println(h3);

//强转不能在boolean值中转换

    double p=3.234324235;
    int d =(int)p;

//溢出问题
    int money=100000000;//拾亿
    int year=20;
    int total=money*year;
    System.out.println("total="+total);//为负数

    long total=money*year;
    System.out.println("total="+total);//前者    money*year计算数值为int强转还是会出现数据丢失

    long total=(long)money*year;
    //long total=1L*money*year;
    
        
           }
    

 

 

}
//强转不能在boolean值中转换
//操作比较大的数需要注意是否溢出,解决方法需要将其中一个操作数强转
//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值