Java知识(002)--数据类型和常用运算

来源:互联网 发布:翻 墙软件 编辑:程序博客网 时间:2024/05/16 07:17

第二天:数据类型和常用运算

  • 基本语言元素

    • 关键字:程序中有特殊含义和通途的单词
    • 标识符:给变量、方法、类等起的名字

      • 字母、数字、下划线和$,数字不能开头
      • 大小写敏感(区分大小写)
      • 不能跟关键字相同,不能包含特殊字符
      • 见名知意,驼峰标识
    • 运算符:指定某种运算的特殊符号

      • 算术运算符:+、-、*、/、%
      • 赋值运算符:=、+=、-=、*=、/=、%=、……
      • 关系运算符:>、<、>=、<=、==、!=
      • 短路运算符:&&、||
      • 条件运算符:? :
      • 自增自减运算符:++、–
      • 类型转换运算符:()
      • 其他运算符:逻辑运算符、位运算符、移位运算符、下标运算符、成员运算符等
    • 字面量:程序中不变的部分

      • 引用型字面量:null
      • 布尔型字面量:true和false
      • 字符型字面量:‘q’,‘\n’,‘\t’,‘\ddd’[*]
      • 整型字面量:29,035,0x1d
      • 实型字面量:3.14,.25e2,5.5f
      • 字符串字面量:“Hello, world”
      • 类字面量:String.class,int.class
    • 分隔符:空格、花括号、方括号、圆括号、分号、逗号、冒号等

  • 数据类型

    • 基本类型(primitive type)

      • 整型:byte、short、int、long
      • 实型:float、double
      • 布尔型:boolean
      • 字符型:char
    • 枚举类型(enumeration type):用于定义符号常量。

    • 引用类型(reference type):除了基本数据类型和枚举类型,剩下的类型都是引用类型。
  • 变量和常量

    • 变量:计算机语言中能储存计算结果或能表示值抽象概念。变量可以通过变量名访问。在指令式语言中,变量存储的值通常是可变的,因此称之为变量。
    • 常量:在程序运行时,不会被修改的量。Java中可以使用final关键字定义常量。

练习1:输入两个数找出其中较大的那个数。

package com.lovoinfo;import java.util.Scanner;public class FindMax {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("请输入两个数: ");        int a = sc.nextInt();        int b = sc.nextInt();        if(a >= b) {            System.out.println(a);        }        else {            System.out.println(b);        }        System.out.println(a >= b? a : b);        sc.close();    }}

练习2:输入身高(cm)和体重(kg)判断身材是否正常。判断标准"身高-110>=体重"认为是正常的。

package com.lovoinfo;import java.util.Scanner;public class AreYouFat {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("请输入你的名字: ");        String name = sc.nextLine();        System.out.print("请输入你的身高: ");        int height = sc.nextInt();        System.out.print("请输入你的体重: ");        int weight = sc.nextInt();        /*        if(height - 110 >= weight) {            System.out.println(name + "的身材正常!");        }        else {            System.out.println(name + "是个胖子!");        }        */        System.out.println(name +            (height - 100 >= weight? "身材正常!" : "是个胖子!"));        sc.close();    }}

练习3:输入一个年份,判断是不是闰年。

package com.lovoinfo;import java.util.Scanner;public class IsLeapYear {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("请输入一个年份: ");        int year = sc.nextInt();        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {            System.out.println(year + "是闰年");        }        else {            System.out.println(year + "不是闰年");        }        sc.close();    }}

练习4:输入圆的半径,计算圆的周长和面积。

package com.lovoinfo;import java.util.Scanner;public class CalcCircle {    private static final double PI = 3.14;    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("请输入圆的半径: ");        double radius = sc.nextDouble();        double area = PI * radius * radius;        double circumference = 2 * PI * radius;        System.out.println("周长: " + circumference);        System.out.println("面积: " + area);        sc.close();    }}

练习5:输入三个整数,按从小到大的顺序输出。

package com.lovo;import java.util.Scanner;public class SortThreeNumber {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("请输入三个数: ");        int a = sc.nextInt();        int b = sc.nextInt();        int c = sc.nextInt();        if(a > b) {            int temp = a;            a = b;            b = temp;        }        if(a > c) {            int temp = a;            a = c;            c = temp;        }        if(b > c) {            int temp = b;            b = c;            c = temp;        }        System.out.printf("%d\t%d\t%d\n", a, b, c);        sc.close();    }}

作业:输入三个整数,输出其中最大的数。

public class Max {    public static void main(String[] args) {        Scanner sc=new Scanner(System.in);        System.out.println("请输入你的第一个数字");        int a=sc.nextInt();        System.out.println("请输入你的第二个数字");        int b=sc.nextInt();        System.out.println("请输入你的第三个数字");        int c=sc.nextInt();        if(a>b)            System.out.println("最大数是:"+(a>c?a:c));        else        System.out.println("最大数是:"+(b>c?a:c));        sc.close();    }}
0 0
原创粉丝点击