introduction
1.输入输出
(1)基本输入输出
package practice;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
//输入Scan后Alt+/可自动补全第3行内容;“in”可替换,实际上是一个标识符
String a = in.next(); //读取输入的字符串
double b = in.nextDouble(); //读取输入的浮点数,同理还有nextInt等
in.close(); //报错时的解决办法
int x = Integer.parseInt(a); //字符串转换成int类型
double y = Double.parseDouble(a); //字符串转换成double类型
long z = Long.parseLong(a); //字符串转换成double类型
System.out.printf("%05d", x); //格式化打印
System.out.print(x); //直接打印
System.out.println(x); //打印并换行
System.out.println(); //新一行
String output = String.format("My name is %s, I am %d years old", "Luo", 30);
//另一种格式化打印方法
}
}
(2)格式化打印控制符:
(3)输入函数
import java.util.*;
public class ScannerClassExample1 {
public static void main(String args[]){
String s = "Hello, This is JavaTpoint.";
//Create scanner Object and pass string in it
Scanner scan = new Scanner(s);
//Check if the scanner has a token
System.out.println("Boolean Result: " + scan.hasNext());
//Print the string
System.out.println("String: " + scan.nextLine());
scan.close();
System.out.println("||{Enter Your Details||{ ");
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.next();
System.out.println("Name: " + name);
System.out.print("Enter your age: ");
int i = in.nextInt();
System.out.println("Age: " + i);
System.out.print("Enter your salary: ");
double d = in.nextDouble();
System.out.println("Salary: " + d);
in.close();
}
}
2.Math库
使用示例:x = Math.abs(a);
3.for循环
for循环可以在括号内直接声明int i=1。
4.数组
(1)声明数组
double [] a; // 声明数组
double a1[]; //方括号也可以放在标识符后
a = new double[10]; // 创建数组
double [] b = new double[10] // 声明的同时创建数组
double [][] c = new double[m][n]; //声明二维数组
(2)数组初始化
double [] a = new double[n];
对于所有的数字原始类型,默认的初始值为0,而布尔类型为false。也可以在声明数组的同时完成初始化。
int[] a = {1,2,3,4,5};
(3)数组长度
a.length可以访问数组长度。
【例】print a random card name such as Queen of
Clubs, as follows.
String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
String[] RANKS = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
int i = (int) (Math.random() * RANKS.length);
int j = (int) (Math.random() * SUITS.length);
System.out.println(RANKS[i] + " of " + SUITS[j]);
(4)二维数组
(a)对于二维数组,a.length访问数组行数,a[i].length访问第i行元素的个数。而二维数组所有行的长度不一定相同。
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
【例】SelfAvoidingWalk
public class SelfAvoidingWalk {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]); // lattice size
int trials = Integer.parseInt(args[1]); // number of trials
int deadEnds = 0; // trials resulting in a dead end
// simulate trials self-avoiding walks
for (int t = 0; t < trials; t++) {
boolean[][] a = new boolean[n][n]; // intersections visited
int x = n/2, y = n/2; // current position
// repeatedly take a random step, unless you’ve already escaped
while (x > 0 && x < n-1 && y > 0 && y < n-1) {
// dead-end, so break out of loop
if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1]) {
deadEnds++;
break;
g
// mark (x, y) as visited
a[x][y] = true;
// take a random step to unvisited neighbor
double r = Math.random();
if (r < 0.25) {
if (!a[x+1][y])
x++;
}
else if (r < 0.50) {
if (!a[x-1][y])
x--;
}
else if (r < 0.75) {
if (!a[x][y+1])
y++;
}
else if (r < 1.00) {
if (!a[x][y-1])
y--;
}
}
}
System.out.println(100*deadEnds/trials + "% dead ends");
}
}
4.字符串
(1)java中字符串可理解为基本类型,用String表示。
(2)赋值:
字符串未赋值:String str1=null;
空字符串:String lcs=""。
(3)输入字符串:str1=in.next()。
(4)字符串长度:str.length()(返回值为int)。
(5)取第i+1个字符:str.charAt(i)(返回值为char)。
(6)a=“a”,b=“b”,c=a+b,c=“ab”。