[Java] Introduction to Java Programming 笔记 Chapter 4. 数学函数等

  • 书上是错的,下面是关于 Math.ceil(), Math.floor(), Math.rint(), Math.round()的测试输出:

    Math.ceil(2.1): 3.0
    Math.ceil(2.0): 2.0
    Math.ceil(-2.0): -2.0
    Math.ceil(-2.1): -2.0
    Math.floor(2.1): 2.0
    Math.floor(2.0): 2.0
    Math.floor(-2.0): -2.0
    Math.floor(-2.1): -3.0
    Math.rint(2.1): 2.0
    Math.rint(-2.0): -2.0
    Math.rint(-2.1): -2.0
    Math.rint(2.5): 2.0
    Math.rint(4.5): 4.0
    Math.rint(-2.5): -2.0
    Math.round(2.6f): 3
    Math.round(2.0): 2
    Math.round(-2.0f): -2
    Math.round(-2.6): -3
    Math.round(-2.4): -2

  • ceil: 向上取整, floor 向下取整, rint 取左右两边最近的那个整数, 如果等距,取偶数。round 四舍五入(符号后加)。

  • Math 类包含在java.lang 包中,java.lang 中的所有类都被隐式导入java程序。

  • 以下两条语句对等:

char letter = 'A';
char letter = '\u0041'; // Character A's Unicode is 0041
  • 数字转换为字符,取低16位, 高位被舍弃:
char ch = (char)0XAB0041; // The lower 16 bits hex code 0041 is assigned to ch
System.out.println(ch); // ch is character A
  • 浮点数转换为字符,取整再转换:
char ch = (char)65.25; // Decimal 65 is assigned to ch
System.out.println(ch); // ch is character A
  • Character 类有很多很好用的方法:
MethodDescription
isDigit(ch)Returns true if the specified character is a digit.
isLetter(ch)Returns true if the specified character is a letter.
isLetterOfDigit(ch)Returns true if the specified character is a letter or digit.
isLowerCase(ch)Returns true if the specified character is a lowercase letter.
isUpperCase(ch)Returns true if the specified character is an uppercase letter.
toLowerCase(ch)Returns the lowercase of the specified character.
toUpperCase(ch)Returns the uppercase of the specified character.

- String 类的方法:

MethodDescription
length()Returns the number of characters in this string.
charAt(index)Returns the character at the specified index from this string.
concat(s1)Returns a new string that concatenates this string with string s1.
toUpperCase()Returns a new string with all letters in uppercase.
toLowerCase()Returns a new string with all letters in lowercase
trim()Returns a new string with whitespace characters trimmed on both sides.
  • 如果 i = 1, j = 2, 下列语句输出i + j is 12, 必须加括号才能输出正确结果:
System.out.println("i + j is " + i + j);
  • trim()方法将删除字符串首末两端的空白字符(whitespace characters): ' ', \t, \f, \r, 或\n

  • char类型自增自减:

char ch = 'a';
System.out.println(++ch);
  • 从控制台读取字符串使用 next() 方法,如果是读取整行,则用 nextLine() 方法:

  • 下面的输入,空格分开:

Scanner input = new Scanner(System.in);
System.out.print("Enter three words separated by spaces: ");
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println("s1 is " + s1);
System.out.println("s2 is " + s2);
System.out.println("s3 is " + s3);
  • 但是,如果为了避免输入错误,必须先使用 nextLine(), 然后再使用 nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), 或 next(),原因不知道。

  • 从控制台读取单个字符,读一行,然后调用 charAt(0)

Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
String s = input.nextLine();
char ch = s.charAt(0);
System.out.println("The character entered is " + ch);
  • 字符串的比较:
方法描述
equals(s1)如果字符串与s1相等,返回true.
equalsIgnoreCase(s1)同上,但不区分大小写.
compareTo(s1)大于返回整数,等于返回0,小于返回负数.
compareToIgnoreCase(s1)同上,但不区分大小写.
startsWith(prefix)如果以指定prefix开头,返回true.
endsWith(suffix)如果以指定suffix结尾,返回true.
contains(s1)如果s1是子串,返回true.
  • == 与 equals 的区别: == 判断两个String 变量是否指向相同的对象,equals则判断两个变量是否内容相同。
    获取子串:

  • substring(beginIndex): 从beginIndex指定的字符开始,直到字符串结束。
    substring(beginIndex, endIndex): 从beginIndex指定的字符开始,直到 endIndex - 1指定的字符。

  • String 的另外两个方法:lastIndexOf(ch, fromIndex)lastIndexOf(s, fromIndex) , 以 fromIndex为分界,取前一部分的子串,然后返回最大索引值。

  • 格式化输出:
    使用System.out.printf() 实现格式化输出, 默认右对齐,加负号左对齐,如果指定的宽度不够,则自动加宽。

FormatSpecifierOutput Example
%ba Boolean valuetrue or false
%ca character‘a’
%da decimal integer200
%fa floating-point number45.460000
%ea number in standard scientific notation4.556000e+01
%sa string“Java is cool”

- 格式指示符必须和对应性完全匹配,例如,如果格式指示符为 %f 或 %e, 那么变量值必须类似于 40.0, 不能为40,否则将出现运行时错误。

  • 下列语句:
System.out.printf("amount is %f %e\n", 32.32, 32.32);
System.out.printf("amount is %5.2f%% %5.4e\n", 32.327, 32.32);
System.out.printf("%6b\n", (1 > 2));
System.out.printf("%6s\n", "Java");
System.out.printf("%-6b%s\n", (1 > 2), "Java");
System.out.printf("%6b%-8s\n", (1 > 2), "Java");

输出如下:

amount is 32.320000 3.232000e+01
amount is 32.33% 3.2320e+01
 false
  Java
 false Java
 falseJava    

Introduction to Java Programming, chapter 4

This book is a brief version of Introduction to Java Programming, Comprehensive Version, 8E. This version is designed for an introductory programming course, commonly known as CS1. This version contains the first twenty chapters in the comprehensive version. This book uses the fundamentals-first approach and teaches programming concepts and techniques in a problem-driven way. The fundamentals-first approach introduces basic programming concepts and techniques before objects and classes. My own experience, confirmed by the experiences of many colleagues, demonstrates that new programmers in order to succeed must learn basic logic and fundamental programming techniques such as loops and stepwise refinement. The fundamental concepts and techniques of loops, methods, and arrays are the foundation for programming. Building the foundation prepares students to learn object-oriented programming, GUI, database, and Web programming. Problem-driven means focused on problem-solving rather than syntax. We make introductory programming interesting by using interesting problems. The central thread of this book is on solving problems. Appropriate syntax and library are introduced to support the writing of a program for solving the problems. To support teaching programming in a problemdriven way, the book provides a wide variety of problems at various levels of difficulty to motivate students. In order to appeal to students in all majors, the problems cover many application areas in math, science, business, financials, gaming, animation, and multimedia. 精美文字版pdf电子书。
本书是Java语言的经典教材,文版分为基础篇和进阶篇,主要介绍程序设计基础、面向对象编程、GUI程序设计、数据结构和算法、高级Java程序设计等内容。本书以示例讲解解决问题的技巧,提供大量的程序清单,每章配有大量复习题和编程练习题,帮助读者掌握编程技术,并应用所学技术解决实际应用开发遇到的问题。您手的这本是其的基础篇,主要介绍了基本程序设计、语法结构、面向对象程序设计、继承和多态、异常处理和文本I/O、抽象类和接口等内容。本书可作为高等院校程序设计相关专业的基础教材,也可作为Java语言及编程开发爱好者的参考资料。 作者:(美国)粱勇(Y.Daniel Liang) 译者:戴开宇 梁勇(Y.Daniel Liang),现为阿姆斯特朗亚特兰大州立大学计算机科学系教授。之前曾是普度大学计算机科学系副教授。并两次获得普度大学杰出研究奖。他所编写的Java教程在美国大学Java课程采用率极高。同时他还兼任Prentice Hall Java系列丛书的编辑。他是“Java Champion”荣誉得主,并在世界各地为在校学生和程序员做JAVA程序设计方法及技术方面的讲座。 戴开宇,复旦大学软件学院教师,工程硕士导师。国计算机学会会员。博士毕业于上海交通大学计算机应用专业,2011~2012年在美国佛罗里达大学作访问学者。承担多门本科专业课程、通识教育课程以及工程硕士课程,这些课程被评为校精品课程,上海市重点建设课程,IBM—教育部精品课程等。
Programming skills are indispensable in today’s world, not just for computer science students, but also for anyone in any scientific or technical discipline. Introduction to Programming in Java, Second Edition, by Robert Sedgewick and Kevin Wayne is an accessible, interdisciplinary treatment that emphasizes important and engaging applications, not toy problems. The authors supply the tools needed for students and professionals to learn that programming is a natural, satisfying, and creative experience, and to become conversant with one of the world’s most widely used languages. This example-driven guide focuses on Java’s most useful features and brings programming to life for every student in the sciences, engineering, and computer science. Coverage includes Basic elements of programming: variables, assignment statements, built-in data types, conditionals, loops, arrays, and I/O, including graphics and sound Functions, modules, and libraries: organizing programs into components that can be independently debugged, maintained, and reused Algorithms and data structures: sort/search algorithms, stacks, queues, and symbol tables Applications from applied math, physics, chemistry, biology, and computer science Drawing on their extensive classroom experience, throughout the text the authors provide Q&As;, exercises, and opportunities for creative engagement with the material. Together with the companion materials described below, this book empowers people to pursue a modern approach to teaching and learning programming. Companion web site (introcs.cs.princeton.edu/java) contains Chapter summaries Supplementary exercises, some with solutions Detailed instructions for installing a Java programming environment Program code and test data suitable for easy download Detailed creative exercises, projects, and other supplementary materials Companion studio-produced online videos (informit.com/sedgewick) are available for purchase and provide students and professionals with th
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值