Java入门教程笔记(五)

Java入门教程笔记(一)

Java入门教程笔记(二)

Java入门教程笔记(三)

Java入门教程笔记(四)

Java入门教程笔记系列仅适用于有过一定编程基础的人学习java时进行参考和借鉴
不适用于作为入门级教程

一、键盘输入和格式化输出
1、对话框
JOptionPane.showInputDialog(String s)

package IO;
import javax.swing.JOptionPane;
public class test {

	public static void main(String[] args)
	{
		String name = JOptionPane.showInputDialog("What is your name?");
		String input = JOptionPane.showInputDialog("How old are you?");
		int age = Integer.parseInt(input);//类型转换
		System.out.println("Hello," + name + ". Next year, you'll be " + (age + 1));
		System.exit(0);
	}
}

input1:
在这里插入图片描述
input2:
在这里插入图片描述
result:

在这里插入图片描述

JOptionPane.showInputDialog调用时系统会创建新的线程(thread),
所以在每一次使用时都需要调用System.exit(0); 来关闭所有的线程

2、格式化输出
(1)System.out.printf("%.2lf",x);
//C语言式
//补充输出时间的格式控制符
在这里插入图片描述
(2)java.text.*包中XxxFormat类完成格式化输出

package IO;
import java.text.*;
import java.util.*;
public class test {

	public static void main(String[] args)
	{
		NumberFormat N = NumberFormat.getInstance();
		System.out.println(1000/3.0 + " " + 10000);
		System.out.println(N.format(1000/3.0) + " " + N.format(10000));
		NumberFormat nf1 = NumberFormat.getInstance();
		System.out.println(nf1.format(123456.78)); // 得到本地的缺省格式
		NumberFormat nf2 =NumberFormat.getInstance(Locale.GERMAN); // 得到德国的格式
		System.out.println(nf2.format(123456.78));
		double avprice=28234.2534;
		java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
		String aveprice=df.format(avprice);
		System.out.print(aveprice);//保留两位小数
	}
}

NumerFormat往往用于指定地区
DecimalFormat更多用于具体的格式控制

二、科学计算
java.long.Math
两个属性常量:

public final static double Math.PI; //数学中PI的值
public final static double Math.E;//科学计数法中e的值

取绝对值:

public static int abs(int a);
public static long abs(long a);
public static float abs(float a);
public static double abs(double a);

取整

public static double ceil(double d);//返回比d大,最接近于d的整数(返回值用double类型来存储),即为上取整
public static double floor(double d);//返回比d小,最接近于d的整数(返回值用double类型来存储),即为下取整。
public static int round(float f);
public static long round(double d);//用四舍五入的方法取整

最大值和最小值

public static int max(int a, int b);
public static long max(long a, long b);
public static float max(float a, float b);
public static double max(double a, double;
//取最大值
public static int min(int a, int b);
public static long min(long a, long b);
public static float min(float a, float b);
public static double min(double a, d);
//取最小值

指对数运算

//求a的b次幂:
public static double pow(double a, double b);
//求Math.E的d次幂:[exp(d)]
public static double exp(double d);
//求以Math.E为底的d的对数:[ln(d)]
public static double log(double d);
//求d开平方后的结果:
public static double sqrt(double d);

三角函数:

public static double sin(double d);
public static double cos(double d);
public static double tan(double d);

三、字符串(String 和StringBuffers)
都在java.long包内
String和StringBuffer类的区别在于:
String对象存储的字符串数据一旦创建之后是只读的,
其设定的内容不能修改。
StringBuffer对象存储的字符串是可以修改的,其设定
内容可以根据需要改变

String的两种创建方式

String str1 = "you and me!";
String str2 = new String("you and me!");

String类中的concat方法和”+”具有相同的功能,可以连接两个字符串

String motto = "Program once";
motto = motto + ", execute everywhere.";
motto = motto.concat(" Don't bet on it!");
System.out.println(motto);

提取子串的方法

String substring(int beginIndex); //获得该字符串中从beginIndex开始到字符串结束的子串
String substring(int beginIndex, int endIndex);//获得该字符串中从beginIndex开始的,到endIndex-1的子串。

去掉原字符串中的空白字符

public String trim();//trim去掉的空白字符包括了回车换行符、空白符、tab符号等。

查找特定子串

int indexOf(String str); //返回子串str在该字符串中首次出现位置,若未找到则返回-1
int indexOf(String str, int fromIndex); //返回子串str在该字符串中索引fromIndex之后首次出现位置
int indexOf(int ch)//返回字符ch在该字符串中首次出现位置,若未找到则返回-1. 
int indexOf(int ch, int fromIndex)//返回字符ch,在该字符串中索引fromIndex之后首次出现位置

逆向查找特定子串

int lastIndexOf(String str); //返回子串str在该字符串中最后一次出现位置。
int lastIndexOf(String str, int lastIndex);//返回子串str在该字符串中索引lastIndex之前最后一次出现位置。
int lastIndexOf(int ch); //返回字符ch在该字符串中最后一次出现位置。
int lastIndexOf(int ch , int lastIndex);//返回字符串ch在该字符串中索引lastIndex之前最后一次出现位置。

字符串修改:

String replace(char oldChar, char newChar); //产生一个新的String字符串对象,将该字符串中所有oldChar字符替换为newChar
String toLowerCase(); //生成一个新的String字符串对象,将原字符串中所有字符转为小写
String toUpperCase(); //将原字符串中所有字符转为大写

字符串比较
等号“ ==“ 不能够比较两个字符串

boolean equals(String str);//区分大小写比较字符串内容
boolean equalsIgnoreCase(String str)//不区分大小写比较字符串内容
int compareTo(String other);// 如果按照字典顺序,字符串位于other之前,就返回负值,如果字符串位于other之后就返回正直,如果相等返回0;
boolean endsWith(String suffix); //如果以suffix结尾返回true。
boolean startsWith(String prefix); //如果以prefix开始返回true

string1.compareTo(string2);//如果 string1 的字母顺序在string2之前,则返回负数。反之返回正数,如果相等返回零。(字典序)

StringBuffer

StringBuffer (String str)//通过一个String对象创建StringBuffer类的对象。该对象的初始空间的大小为str的长度+16。
StringBuffer() //创建一个空的StringBuffer对象。该对象的初始空间的大小为16。
StringBuffer(int length)//创建一个指定空间大小的StringBuffer对象,可容纳length个字符。

艹,还有好多,不想写了,以后有心情在再写吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值