侧边栏壁纸
  • 累计撰写 57 篇文章
  • 累计创建 10 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

3、Java基本程序设计结构

yilee
2023-04-04 / 0 评论 / 0 点赞 / 56 阅读 / 0 字
温馨提示:
本文最后更新于2024-05-31,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

第三章 Java基本程序设计结构

3.3 数据类型

​ Java 是一种强类型语言。这就意味着必须为每一个变量声明一种类型: 在Java 中, 一共有8种基本类型(primitive type ), 其中有4 种整型、2 种浮点类型、1 种用于表示Unicode 编码的字符单元的字符类型char和1 种用于表示真值的boolean 类型。

溢出的三种情况(浮点数):

  • 正无穷大(Float.POSITIVE_INFINITY**(1.0f / 0.0f)**,Double.POSITIVE_INFINITY (1.0 / 0.0)
  • 负无穷大(Float.NEGATIVE_INFINITY**(-1.0f / 0.0f),Double.NEGATIVE_INFINITY(-1.0 / 0.0)**)
  • NaN(Float.NaN**(0.0f / 0.0f),Double.NaN(0.0 / 0.0)**),非数字不能使用等于符号检测,可以使用Double.isNaN(x)

3.5 运算符

​ 在 Java 中,使用算术运算符 +、-、*、/ 表示加、减、乘、除运算。当参与/ 运算的两个操作数都是整数时, 表示整数除法;否则, 表示浮点除法。整数的求余操作(有时称为取模)用% 表示。例如, 15/2 等于7, 15%2 等于1 , 15.0/2 等于7.50需要注意, 整数被0 除将会产生一个异常, 而浮点数被0 除将会得到无穷大或NaN 结果。

  1. Math.floorMod() 方法解决整数的余数为负数的方法

  2. 数据类型转化

    image-20211007144937801.png

  3. >>> 运算符会用0 填充高位,这与 >> 不同,它会用符号位填充高位。不存 <<< 运算符

3.7 输入输出

  1. 读取输入(用于输入输出的控制台

    // 输入定义
    Scanner in = new Scanner(System.in);
    // Scanner 类不适用于从控制台读取密码,因此再用Console
    // 必须使用控制台运行(cmd、linux控制台),否则无法获取到控制台
    Console cons = System.console();
    String username = cons.readLine("User name: ");
    // 读取密码不会展示
    char[] passwd = cons.readPassword("Password:");
    // 格式化输出,标志如下图一所示
    System.out.print(x);
    System.out.printf("%s", "nbidfo");
    // 可以使用静态的String.format 方法创建一个格式化的字符串, 而不打印输出:
    String msg = String.format ("%s", "nbidfo");
    // 日期也可格式化,日期标志如下图二所示
    String msg = String.format ("%F", new Date());
    
    // 要想对文件进行读取, 就需要一个用File对象构造一个Scanner 对象
    Scanner in = new Scanner(Paths.get("myfile.txt") , "UTF-8");
    // 文件输入输出,如果文件不存在, 创建该文件。
    // PrintWriter 可以像输出到System.out一样使用print、println以及printf命令。
    PrintWriter out = new PrintWriter("myfile.txt", "UTF-8");
    // 启动路径就是命令解释器的当前路径。可以使用下面的调用方式找到路径的位置:
    String dir = System.getProperty("user.dir");
    
  2. 格式化输出

    image-20211007150042465.png

    image-20211007150316276.png

3.8 控制流程

​ 与任何程序设计语言一样, Java 使用条件语句和循环结构确定控制流程。

循环打断(多重):

label:
for(int i = 0; i < 10; i++){
	for(int j = 0; j < 10; j++){
		if(i == 6 && j == 5){
			break label;
		}
	}
}

API注释

3.6.7 String API

java.lang.String 1.0

char charAt(int index)
// 返回给定位置的代码单元。除非对底层的代码单元感兴趣, 否则不需要调用这个方法。
int codePointAt(int Index) 5.0
// 返回从给定位置开始的码点。
int offsetByCodePoints(int startIndex, int cpCount) 5.0
// 返回从startIndex 代码点开始, 位移cpCount 后的码点索引。
int compareTo(String other)
// 按照字典顺序, 如果字符串位于other 之前, 返回一个负数;如果字符串位于other 之后,返回一个正数; 如果两个字符串相等,返回0。
IntStream codePoints() 8
// 将这个字符串的码点作为一个流返回。调用toArray 将它们放在一个数组中。
new String(int[] codePoints, int offset, int count) 5.0
// 用数组中从offset 开始的count 个码点构造一个字符串。
boolean equals(Object other)
// 如果字符串与other 相等, 返回true。
boolean equalsIgnoreCase(String other)
// 如果字符串与other 相等(忽略大小写),返回true。
boolean startsWith(String prefix)
boolean endsWith(String suffix)
// 如果字符串以suffix 开头或结尾, 则返回true。
int indexOf(String str)
int indexOf(String str, int fromIndex)
int indexOf(int cp)
int indexOf(int cp, int fromIndex)
// 返回与字符串str 或代码点cp 匹配的第一个子串的开始位置。这个位置从索引0 或fromIndex 开始计算。如果在原始串中不存在str, 返回-1。
int lastIndexOf(String str)
int lastIndexOf(String str, int fromIndex)
int lastindexOf(int cp)
int lastindexOf(int cp, int fromIndex)
// 返回与字符串str 或代码点cp 匹配的最后一个子串的开始位置。这个位置从原始串尾端或fromIndex 开始计算。
int length()
// 返回字符串的长度。
int codePointCount(int startIndex, int endIndex) 5.0
// 返回startIndex 和endludex-l 之间的代码点数量。没有配成对的代用字符将计入代码点。參String replace(CharSequence oldString,CharSequence newString)返回一个新字符串。这个字符串用newString 代替原始字符串中所有的oldString。可以用String 或StringBuilder 对象作为CharSequence 参数。
String substring(int beginIndex)
String substring(int beginIndex, int endIndex)
// 返回一个新字符串。这个字符串包含原始字符串中从beginIndex 到串尾或endIndex-l的所有代码单元。
String toLowerCase()
String toUpperCase()
// 返回一个新字符串。这个字符串将原始字符串中的大写字母改为小写, 或者将原始字符串中的所有小写字母改成了大写字母。
String trim()
// 返回一个新字符串。这个字符串将删除了原始字符串头部和尾部的空格。
String join(CharSequence delimiter, CharSequence... elements) 8
// 返回一个新字符串, 用给定的定界符连接所有元素。

3.6.9 构建字符串

java.lang.StringBuilder 5.0

StringBuilder()
// 构造一个空的字符串构建器。
int length()
// 返回构建器或缓冲器中的代码单元数量。
StringBuilder append(String str)
// 追加一个字符串并返回this。
StringBuilder append(char c)
// 追加一个代码单元并返回this。
StringBuilder appendCodePoint(int cp)
// 追加一个代码点,并将其转换为一个或两个代码单元并返回this。
void setCharAt(int i, char c)
// 将第i 个代码单元设置为c。
StringBuilder insert(int offset, String str)
// 在offset 位置插入一个字符串并返回this。
StringBuilder insert(int offset, char c)
// 在offset 位置插入一个代码单元并返回this。
StringBuilder delete(int startindex, int endlndex)
// 删除偏移量从startindex 到-endlndex-1 的代码单元并返回this。
String toString()
// 返回一个与构建器或缓冲器内容相同的字符串

3.7.1 读取输入

java.util.Scanner 5.0

Scanner(InputStream in)
// 用给定的输人流创建一个Scanner 对象。
String nextLine()
// 读取输入的下一行内容。
String next()
// 读取输入的下一个单词(以空格作为分隔符)。
int nextInt()
double nextDouble()
// 读取并转换下一个表示整数或浮点数的字符序列。
boolean hasNext()
// 检测输人中是否还有其他单词。
boolean hasNextInt()
boolean hasNextDouble()
// 检测是否还有表示整数或浮点数的下一个字符序列。

java.Iang.System 1.0

static Console console() 6
// 如果有可能进行交互操作, 就通过控制台窗口为交互的用户返回一个Console 对象,否则返回null。对于任何一个通过控制台窗口启动的程序, 都可使用Console 对象。否则, 其可用性将与所使用的系统有关s

java.io.Console 6

static char[] readPassword(String prompt, Object...args)
static String readLine(String prompt, Object...args)
// 显示字符串prompt 并且读取用户输入,直到输入行结束。args 参数可以用来提供输人格式。有关这部分内容将在下一节中介绍。

3.7.3 文件输入与输出

java.util.Scanner 5.0

Scanner(File f)
// 构造一个从给定文件读取数据的Scanner。
Scanner(String data)
// 构造一个从给定字符串读取数据的Scanner。

java.io.PrintWriter 1.1

PrintWriter(String fileName)
// 构造一个将数据写入文件的PrintWriter。文件名由参数指定。

java.nio.file.Paths 7

static Path get(String pathname)
// 根据给定的路径名构造一个Path。

3.9 大数值

java.math.Biglnteger 1.1

BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multipiy(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
// 返冋这个大整数和另一个大整数other的和、差、积、商以及余数。
int compareTo(BigInteger other)
// 如果这个大整数与另一个大整数other 相等, 返回0; 如果这个大整数小于另一个大整
// 数other, 返回负数; 否则, 返回正数。
static BigInteger valueOf(long x)
// 返回值等于x 的大整数。

java.math.BigDecimal 1.1

BigDecimal add(BigDecimal other)
BigDecimal subtract(BigDecimal other)
BigDecimal multipiy(BigDecimal other)
BigDecimal divide(BigDecimal other RoundingMode mode) 5.0
// 返回这个大实数与另一个大实数other 的和、差、积、商。要想计算商, 必须给出舍入方式(rounding mode)。 RoundingMode.HALF_UP 是在学校中学习的四舍五入方式( BP, 数值0 到4 舍去, 数值5 到9 进位)。它适用于常规的计算。有关其他的舍入方式请参看ApI文档。
int compareTo(BigDecimal other)
// 如果这个大实数与另一个大实数相等, 返回0 ; 如果这个大实数小于另一个大实数,返回负数; 否则,返回正数。
static BigDecimal valueOf(long x)
static BigDecimal valueOf(long x ,int scale)
// 返回值为X 或x / 10scale 的一个大实数。

3.10.5 数组排序

java,util.Arrays 1.2

static String toString(type[] a) 5 . 0
// 返回包含a 中数据元素的字符串, 这些数据元素被放在括号内, 并用逗号分隔。
// 参数: a 类型为int、long、short、char、byte、boolean、float 或double 的数组。
static type copyOf(type[] a, int length)6
static type copyOfRange(type[] a, int start, int end)6
// 返回与a 类型相同的一个数组, 其长度为length 或者end-start, 数组元素为a 的值。
// 参数:a 类型为int、long、short、char、byte、boolean、float 或double 的数组。
// 		start 起始下标(包含这个值)0
// 		end 终止下标(不包含这个值)。这个值可能大于a.length。在这种情况下,结果为0 或false。
// 		length 拷卩!的数据元素长度c 如果length 值大于a.length, 结果为0 或false ;否则, 数组中只有前面length 个数据元素的拷W 值。
static void sort(type[] a)
// 采用优化的快速排序算法对数组进行排序。
// 参数:a 类型为int、long、short、char、byte、boolean、float 或double 的数组。
static int binarySearch(type[] a, type v)
static int binarySearch(type[] a, int start, int end, type v) 6
// 采用二分搜索算法查找值v。如果查找成功, 则返回相应的下标值; 否则, 返回一个负数值r。 -r-1 是为保持a 有序v 应插入的位置。
// 参数:a 类型为int、long、short、char、byte、boolean 、float 或double 的有序数组。
// 		start 起始下标(包含这个值)。
// 		end 终止下标(不包含这个值)。
// 		v 同a 的数据元素类型相同的值。
static void fill(type[] a, type v)
// 将数组的所有数据元素值设置为V。
// 参数:a 类型为int、long、short、char、byte、boolean 、float 或double 的数组。
// 		v 与a 数据元素类型相同的一个值。
static boolean equals(type[] a, type[] b)
// 如果两个数组大小相同, 并且下标相同的元素都对应相等, 返回true。
// 参数:a、b 类型为int、long、short、char、byte、boolean、float 或double 的两个数组。
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区