1 Welcome to java
public class ch01Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); //运行的快捷键为crtl + F11
}
}
2 从控制台输入
package cn.jxufe.java;
import java.util.Scanner;
public class ch02ConsoleInput {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("enter a number for radius: ");
double radius = input.nextDouble();
double area = radius * radius * 3.14159;
System.out.println("the area for the circle of radius " + radius + " is " + area);
}
}
输入三个数,计算三个数的平均值:
package cn.jxufe.java;
import java.util.Scanner;
import jdk.nashorn.internal.objects.NativeUint16Array;
public class ch03ComputerAverage {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print(" Enter three numbers : ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double average = (number1 + number2 + number3) / 3;
System.out.println("the average of " + number1 + " " + number2 + " " + number3 + " is " + average);
}
}
3 产生随机数
package cn.jxufe.java;
import java.util.Scanner;
public class ch04RanomNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
if (number1 < number2) {
int temp = number1;
number1 = number2;
number2 = temp;
}
System.out.println("what is " + number1 + " - " + number2 + " ?");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
if (number1 - number2 == answer)
System.out.println("you are right");
else {
System.out.println("you are wrong");
System.out.println(number1 + " - " + number2 + " should be " + (number1 - number2));
}
}
}
4 数学函数
4.1 三角函数
4.2 指数函数
4.3 取整方法
4.4 min、max和abs方法
4.5 random方法
5 String类型
5.1 求字符串长度、从字符串中获取字符、连接字符串和字符串大小写转换
package cn.jxufe.java;
public class ch05StringType {
public static void main(String[] args) {
// TODO Auto-generated method stub
String message = "Welcome to Java";
System.out.println("the length of " + message + " is " + message.length()); // 求字符串长度
System.out.println(message.charAt(0)); // 从字符串中获取字符
System.out.println(message.concat(" and python")); // 连接字符串
System.out.println(message + " and python");
System.out.println(message.toLowerCase()); // 字符串大小写转换
System.out.println(message.toUpperCase());
}
}
5.2 从控制台读取字符串
package cn.jxufe.java;
import java.util.Scanner;
public class ch05StringType {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("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);
System.out.println("enter a line: ");
Scanner input2 = new Scanner(System.in);
String s= input2.nextLine();
System.out.println("the line entered is " + s);
}
}
5.3 从控制台读取字符
package cn.jxufe.java;
import java.util.Scanner;
public class ch06ReadChar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("enter a character: ");
String s = input.nextLine();
char ch = s.charAt(0);
System.out.println("the character entered is " + ch);
}
}
5.4 字符串比较
package cn.jxufe.java;
public class ch07StringCompare {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Welcome to java ";
String s2 = "Welcome to java ";
String s3 = "Welcome to python ";
System.out.println(s1.equals(s2)); // 内容相等
System.out.println(s1.equals(s3));
System.out.println(s1.compareTo(s2)); // 从左至右第一个不同字符之间的距离
System.out.println(s1.compareTo(s3));
System.out.println(s1.startsWith("Wel")); // 字符串检测
System.out.println(s1.endsWith("va"));
System.out.println(s1.contains("to"));
}
}
5.5 获取字符串中的字符或者子串
package cn.jxufe.java;
import sun.print.resources.serviceui;
public class ch08subString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Welcome to java";
System.out.println(s.indexOf('W'));
System.out.println(s.indexOf('o'));
System.out.println(s.indexOf("to"));
System.out.println(s.indexOf("To"));
System.out.println();
System.out.println(s.lastIndexOf('W'));
System.out.println(s.lastIndexOf('o'));
System.out.println(s.lastIndexOf("to"));
System.out.println(s.lastIndexOf("To"));
}
}
6 方法重载
package cn.jxufe.java;
import com.sun.org.apache.regexp.internal.recompile;
public class ch09OverLoading {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("the maximum of 3 and 4 is " + max(3, 4));
System.out.println("the maximum of 3.0 and 5.4 is " + max(3, 5.4));
System.out.println("the maximum of 3.0, 5.4 and 10.14 is " + max(3, 5.4, 10.14));
}
public static int max(int num1, int num2) {
return Math.max(num1, num2);
}
public static double max(double num1, double num2) {
return Math.max(num1, num2);
}
public static double max(double num1, double num2, double num3) {
return max(max(num1, num2), num3);
}
}
7 foreach循环
package cn.jxufe.java;
public class ch10ForEach {
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] myList = { 1.9, 2.9, 3.4, 3.5 };
for (double e : myList) {
System.out.print(e + " ");
}
}
}
8 应用一维数组分析数字
找出一些数中大于平均值的个数。让用户给出数据的个数和输入的数据。
package cn.jxufe.java;
import java.util.Scanner;
public class ch11AnalyzeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("enter the number of items: ");
int n = input.nextInt();
double[] numbers = new double[n];
double sum = 0;
System.out.println("enter the numbers: ");
for (int i = 0; i < n; i++) {
numbers[i] = input.nextDouble();
sum += numbers[i];
}
double average = sum / n;
int count = 0;
for (double e : numbers) {
if (e > average)
count++;
}
System.out.println("average is " + average);
System.out.println("number of elements above the average is " + count);
}
}
9 一维数组的复制
package cn.jxufe.java;
public class ch12Arraycopy {
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] myList = { 1.9, 2.9, 3.4, 3.5 };
double[] newList = new double[myList.length];
System.arraycopy(myList, 0, newList, 0, myList.length);
for(double e:newList)
System.out.print(e + " ");
}
}
10 将一维数组传递给方法
package cn.jxufe.java;
public class ch13ArrayTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 1;
int[] y = new int[10];
m(x, y);
System.out.println("x is " + x);
System.out.println("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers) {
number = 1001;
numbers[0] = 5555;
}
}
11 可变长参数列表
package cn.jxufe.java;
public class ch14VarArgsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
printMax(34, 3, 3, 2, 56.5);
printMax(new double[] { 1, 2, 3 }); // 匿名数组
}
public static void printMax(double... numbers) {
if (numbers.length == 0) {
System.out.println("no argument passed ");
return;
}
double result = numbers[0];
for (double e : numbers) {
if (e > result)
result = e;
}
System.out.println("the max value is " + result);
}
}
12 Arrays类
package cn.jxufe.java;
import java.util.Arrays;
public class ch15ArraysDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] numbers = { 6.0, 4.4, 1.9, 2.9, 3.4, 3.5 };
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // 打印一维数组的简单方法
int[] list = { 2, 4, 7, 10, 11, 45 };
System.out.println(" 11 index is " + Arrays.binarySearch(list, 11));
int[] list2 = { 2, 3, 5, 1 };
Arrays.fill(list2, 5);
System.out.println(Arrays.toString(list2));
}
}
13 将二维数组传递给方法
package cn.jxufe.java;
import java.util.Arrays;
import java.util.Scanner;
public class ch16TowDimensionArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] m = getArray();
System.out.println(Arrays.deepToString(m)); // 打印二维数组的简单方法
System.out.println("Sumof all element is " + sum(m));
}
public static int[][] getArray() {
Scanner input = new Scanner(System.in);
int[][] m = new int[3][4];
System.out.println("enter " + m.length + " rows and " + m[0].length + " columns: ");
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++)
m[i][j] = input.nextInt();
}
return m;
}
public static int sum(int[][] m) {
int total = 0;
for (int row = 0; row < m.length; row++) {
for (int column = 0; column < m[row].length; column++) {
total += m[row][column];
}
}
return total;
}
}