基本数据类型与表达式.ppt

上传人:xin****828 文档编号:15455141 上传时间:2020-08-11 格式:PPT 页数:39 大小:486.32KB
收藏 版权申诉 举报 下载
基本数据类型与表达式.ppt_第1页
第1页 / 共39页
基本数据类型与表达式.ppt_第2页
第2页 / 共39页
基本数据类型与表达式.ppt_第3页
第3页 / 共39页
资源描述:

《基本数据类型与表达式.ppt》由会员分享,可在线阅读,更多相关《基本数据类型与表达式.ppt(39页珍藏版)》请在装配图网上搜索。

1、Java程序基本结构解析,程序解析: 1、注释 2、类声明 3、标识符 4、关键字 5、字符串 6、标准输出对象 7、语句 8、方法与参数,第二章 基本数据类型与表达式,数据类型 常量与变量 输入与输出 表达式,1. 数据类型,基本数据类型,数据类型转换,自动数据类型转换 低级别向高级别转换:byte-short-int-long-float-double 例:3*a+6.85 强制数据类型转换 格式:(数据类型名称)表达式 可能导致精度损失,2.常量与变量,标识符 标识符只能由字母、数字、美元符和下划线组成,如root-1,my=you,100%等不符合要求 第一个字符不能是数字,如3abc

2、 标识符可以是任意长的字符,但只有前32个字符有效 关键字:Java保留使用的标识符 判断标识符定义对错: char 0ax_li fLu1 Cy%ty $123 aa 命名习惯: 变量与方法:radius area showInputDialog 类名:ComputeArea 常量:PI MAX_VALUE,Java标识符是大小写敏感的,保留字,常量,定义:程序运行过程中不能改变其值的量 整型常量 不允许包含逗号、小数点和特殊符号(包括美元符号) 默认是int类型 浮点型常量 具有小数点的数字 默认是double类型 字符型常量(Unicode编码集) 布尔型常量: true, false,

3、常量,final datatype CONSTANTNAME= VALUE; final double PI = 3.14159; final int SIZE = 3;,变量,Declaring Variables int x; / Declare x to be an integer variable; double radius; / Declare radius to be a double variable; char a; / Declare a to be a character variable; Assignment Statements x = 1; / Assign 1

4、to x; radius = 1.0; / Assign 1.0 to radius; a = A; / Assign A to a; Declaring and Initializing in One Step int x = 1; double d = 1.4;,Trace a Program Execution,public class ComputeArea /* Main method */ public static void main(String args) final double PI=3.14159; double radius; double area; / Assig

5、n a radius radius = 20; / Compute area area = radius * radius * PI; / Display results System.out.println(The area for the circle of radius + radius + is + area); ,20,radius,memory,1256.636,area,print a message to the console,char letter = A; (ASCII) char numChar = 4; (ASCII) char letter = u0041; (Un

6、icode) char numChar = u0034; (Unicode),Four hexadecimal digits.,NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = a; System.out.println(+ch);,字符类型,Unicode

7、 Format,Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the worlds diverse languages. Unicode takes two bytes, preceded by u, expressed in four hexadecimal numbers that run from u0000

8、to uFFFF. So, Unicode can represent 65535 + 1 characters.,Unicode u03b1 u03b2 u03b3 for three Greek letters,Example: Displaying Unicodes,Write a program that displays two Chinese characters and three Greek letters.,import javax.swing.JOptionPane; public class DisplayUnicode public static void main(S

9、tring args) JOptionPane.showMessageDialog(null, u6B22u8FCE u03b1 u03b2 u03b3, u6B22u8FCE Welcome, JOptionPane.INFORMATION_MESSAGE); ,Escape Sequences for Special Characters,Description Escape Sequence Unicode Backspace bu0008 Tab tu0009 Linefeed nu000A Carriage return ru000D Backslash u005C Single Q

10、uote u0027 Double Quote u0022,Appendix B: ASCII Character Set,ASCII Character Set is a subset of the Unicode from u0000 to u007f,ASCII Character Set, cont.,ASCII Character Set is a subset of the Unicode from u0000 to u007f,String message = Welcome to Java; String is actually a predefined class in th

11、e Java library just like the System class and JOptionPane class. The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable.,字符串类型,String Concatenation,/ Three strings are concatenated String message = Welcome + to + Java;

12、/ String Chapter is concatenated with number 2 String s = Chapter + 2; / s becomes Chapter2 / String Supplement is concatenated with character B String s1 = Supplement + B; / s becomes SupplementB,Obtaining Input,Using JOptionPane input dialogs Using the JDK 1.5 Scanner class,Getting Input from Inpu

13、t Dialog Boxes,String string = JOptionPane.showInputDialog( null, “Prompting Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);,Two Ways to Invoke the Method,There are several ways to use the showInputDialog method. For the time being, you only need to know two ways to invoke it. One is to use

14、 a statement as shown in the example: String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); where x is a string for the prompting message, and y is a string for the title of the input dialog box. The other is to use a statement like this: JOptionPane.showInputDialog(

15、x); where x is a string for the prompting message.,Converting Strings to Integers,The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number. To convert a string int

16、o an int value, you can use the static parseInt method in the Integer class as follows: int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.,Converting Strings to Doubles,To convert a string into a double value, you can use the static parseDouble method in t

17、he Double class as follows: double doubleValue =Double.parseDouble(doubleString); where doubleString is a numeric string such as “123.45”.,import javax.swing.JOptionPane; /import java.lang.String; public class Addition public static void main(String args) String inputX=JOptionPane.showInputDialog(请输

18、入第一个整数:); int x = Integer.parseInt(inputX); String inputY=JOptionPane.showInputDialog(请输入第二个整数:); int y = Integer.parseInt(inputY); int sum; sum = x + y; char ch = A; System.out.printf(sum=%d, sum); JOptionPane.showMessageDialog(null,sum+ sum); ,Example:,Getting Input Using Scanner,1. Create a Scann

19、er object Scanner scanner = new Scanner(System.in); 2. Use the methods next(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or nextBoolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. For example, System.out.print(Enter a double valu

20、e: ); Scanner scanner = new Scanner(System.in); double d = scanner.nextDouble();,Example:,import java.util.Scanner; /import java.lang.String; public class Addition public static void main(String args) Scanner sc = new Scanner(System.in); System.out.print(请输入第一个整数:); int x = sc.nextInt(); System.out.

21、print(请输入第二个整数:); int y = sc.nextInt(); int sum; sum = x + y; System.out.printf(sum=%d, sum) ,Output,JOptionPane.showMessageDialog(null,x); System.out. print方法输出后光标不换行 println方法输出后光标换行 printf 格式化输出(占位符 %s) 注意:printf方法在jdk1.5之后,3.表达式,赋值表达式 算术表达式 关系表达式 逻辑表达式 条件表达式 位运算表达式,赋值表达式,赋值运算符:= 赋值表达式:变量=表达式 数据类

22、型转换 数据类型兼容:自动类型转换 不兼容:必须进行数据类型的强制转换 例:x=x+1 x=y=3,算术表达式,单目:+,-,+,- 双目:+,-,*,/,% 优先级:先乘除(取余),后加减 算术赋值运算符:+=,-=,*=,/=,%= 字符类型的数据以该字符的Unicode码值参加运算,关系表达式,关系运算符:,=,y-z f=+x*2=y,逻辑表达式,逻辑运算符: m-3n public class LeapYear public static void main(String args) / Prompt the user to enter a year String yearStrin

23、g = JOptionPane.showInputDialog(Enter a year); / Convert the string into an int value int year = Integer.parseInt(yearString); / Check if the year is a leap year boolean isLeapYear = (year % 4 = 0) ,条件表达式,三目运算符: ? : 格式:表达式1 ? 表达式2 : 表达式3 例: z=xy?x:y, s=(s=xy?x:y)z?s:z,位运算表达式,位运算是用来对整数的二进制位进行操作。在计算机内

24、部,整数以二进制补码形式存储。 正整数a的补码=a的二进制表示 负整数-a的补码=(a的二进制表示)1 运算符:&(与),|(或),(异或),(取反),(右移),运算符优先级,按从高到低顺序列出 . () + - ! * / % = = = != & | & | ?: =,op=,小结,JAVA数据类型 变量的使用,注意字符型和String型 掌握用JOptionPane.showInputDialog和Scanner进行输入; 用JOptionPane.showMessagDialog和System.out进行输出 运算符的优先级、结合性及表达式,作业,编写程序读入下列信息并打印工资单 雇员的名字(例如Smith) 每周工时数(例如10) 每小时工资(例如6.75) 税率(例如5) 要求编写两种版本的程序: (a)使用对话框获得输入并显示输出; (b)使用控制台进行输入与输出 2. 准备实验二,

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!