1.使用StringBuilder构建字符串
public class Length_zf {
public static void main(String []args) {
StringBuilder str = new StringBuilder(“I want to learn “);//构建字符串
str.append(” java !”); //追加字符串
str.insert(2, “go”); //插入字符串
str.delete(4,8); //删除字符串
System.out.println(str);
}
}
2.读取输入
import java.util.*;
//input test
public class InputTest {
public static void main(String []args) {
Scanner in = new Scanner(System.in); //用指定的输入流构造Scanner对象,System.in是标准输入流
System.out.print(“What’s your name?”);
String name = in.nextLine();
System.out.print(“How old are you?”);
int age = in.nextInt();
System.out.println(“Hello,”+name+"!Next year you’ll be “+(age+1)+” years old!");
}
}
3.格式化输出
类似C语言,使用printf函数:
public class OnputTest {
public static void main(String []args) {
double x = 1000.0 / 3.0;
char name = ‘sxjy’;
int age = 27;
System.out.printf("%8.2f",x); //注意是printf,使用8个字符和小数点后两个字符的精度
}
}