




已阅读5页,还剩34页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 标识符可以是任意长的字符,但只有前32个字符有效 关键字:Java保留使用的标识符 判断标识符定义对错: char 0ax_li fLu1 Cy%ty $123 aa 命名习惯: 变量与方法:radius area showInputDialog 类名:ComputeArea 常量:PI MAX_VALUE,Java标识符是大小写敏感的,保留字,常量,定义:程序运行过程中不能改变其值的量 整型常量 不允许包含逗号、小数点和特殊符号(包括美元符号) 默认是int类型 浮点型常量 具有小数点的数字 默认是double类型 字符型常量(Unicode编码集) 布尔型常量: true, false,常量,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 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; / Assign 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; (Unicode) 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 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 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(String args) JOptionPane.showMessageDialog(null, “u6B22u8FCE u03b1 u03b2 u03b3“, “u6B22u8FCE Welcome“, JOptionPane.INFORMATION_MESSAGE); ,Description Escape Sequence Unicode Backspace b u0008 Tab t u0009 Linefeed n u000A Carriage return r u000D Backslash u005C Single Quote 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 the 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“; / 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 Input 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 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(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 into 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 the 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(“请输入第一个整数:“); 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 Scanner 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 value: “); 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.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.表达式,赋值表达式 算术表达式 关系表达式 逻辑表达式 条件表达式 位运算表达式,赋值表达式,赋值运算符:= 赋值表达式:变量=表达式 数据类型转换 数据类型兼容:自动类型转换 不兼容:必须进行数据类型的强制转换 例:x=x+1 x=y=3,算术表达式,单目:+,-,+,- 双目:+,-,*,/,% 优先级:先乘除(取余),后加减 算术赋值运算符:+=,-=,*=,/=,%= 字符类型的数据以该字符的Unicode码值参加运算,关系表达式,关系运算符:,=,y-z f=+x*2=y,逻辑表达式,逻辑运算符: m-3n&(n-8)!=0|y*2x,设年份为year,当year能被4整除但不能被100整除,或者year能被400整除时,year为闰年。写出满足上述逻辑的表达式。 ( year%4=0&year%100!=0)|(year%400=0),import javax.swing.JOptionPane; public class LeapYear public static void main(String args) / Prompt t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 3884.4-2025铜精矿化学分析方法第4部分:铅、锌、镉、镍和氧化镁含量的测定火焰原子吸收光谱法
- 2025江苏连云港市灌云县招聘社区工作者20人备考考试题库附答案解析
- 2025年彭州市教育系统“蓉漂人才荟”赴外公招(32人)笔试备考题库及答案解析
- 2025福建海峡人力资源股份有限公司平潭分公司第六批招聘1人备考考试题库附答案解析
- 2025四川成都市第八人民医院下半年编外招聘52人备考考试题库附答案解析
- 2025年宣城绩溪县开源建设投资发展有限公司公开招聘7名备考考试题库附答案解析
- 2025重庆武隆区事业单位考核招聘21人备考考试题库附答案解析
- 2025广东工业大学管理学院国际合作与认证中心招聘1人备考考试题库附答案解析
- 掌握游戏行业新角色
- 静脉血栓风险评估
- 2025年高考英语新课标Ⅱ卷点评及2026备考方向 课件
- 2025年广东省中考语文试卷真题(含答案解析)
- 2025年学宪法、讲宪法知识竞赛题库及答案
- 可信数据空间解决方案星环科技
- 高中英语新课标3000词汇表(新高考)
- 【MOOC】《中国马克思主义与当代》(北京科技大学)中国大学MOOC慕课答案
- 国家职业技能标准 (2021年版) 燃气供应服务员
- 水利工程设计标准化管理手册
- 蓝花花钢琴谱
- 印度白内障小切口手术学习笔记
- 研究生新生入学教育
评论
0/150
提交评论