目录
0 eclipse快捷键
- Alt + / 代码提示
- Ctrl + / 注释
1 基本输入输出
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in); //创建输入对象,接收键盘输入
System.out.println(in.nextLine()); //将输入的内容显示在终端
}
2 用变量做计算
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
final int price = 10; //定义一个常量
int money;
money = in.nextInt(); //读入一个整数
System.out.println("找零:"+(money-price));
}
3 浮点数运算
public static void main(String[] args) {
// TODO Auto-generated method stub
int foot;
double inch; //定义一个浮点数
Scanner in = new Scanner(System.in);
foot = in.nextInt();
inch = in.nextDouble();
//浮点数运算,将结果强制转换为int类型
System.out.println("身高为:"+(int)((foot + inch/12)*0.3048*100) + "cm");
}
4 浮点数判等
public static void main(String[] args) {
// TODO Auto-generated method stub
double a = 0.3;
double b = 0.1 + 0.1 + 0.1;
System.out.println("a="+a + ", b="+b);
System.out.println(a == b); //false
//比较两浮点数的差值的绝对值是否小于无穷小
System.out.println(Math.abs(a-b) < 1e-6); //true
}
5 if条件判断语句
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int age;
System.out.print("请输入你的年龄:");
age = in.nextInt();
if(age>=0 && age<12) {
System.out.println("这个年龄段,尽情地玩耍吧~");
}else if(age>=12 && age<35) {
System.out.println("不要在最能吃苦的年纪选择了安逸~");
}else if(age>=35 && age<60) {
System.out.println("要保持对梦想的激情,肆意地挥洒热血吧~");
}else if(age>=60 && age<=100){
System.out.println("余生是用来享受生活的,去周游世界吧~");
}else {
System.out.println("年龄不合法!");
}
}
tips:推荐的代码风格(方便进行注释)
6 循环语句
6.1 输入一个整数,判断其位数
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num = in.nextInt();
int cnt = 0;
int tmp = num;
while(tmp!=0) {
tmp = tmp/10;
cnt++;
}
System.out.println(num + "是一个" + cnt + "位数");
}
6.2 猜数游戏
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int num = (int)(1+Math.random()*100); //产生1~100之间的随机数
int ans; //用户猜数
int cnt = 0; //猜数次数
do {
System.out.println("请猜数(1~100):");
ans = in.nextInt();
cnt++;
if(ans > num)
{
System.out.println("猜大了,继续猜:");
}
else if(ans < num)
{
System.out.println("猜小了,继续猜:");
}
else
{
System.out.println("恭喜你猜对了,一共猜了"+cnt+"次");
}
}while(ans != num);
}
6.3 输入一个整数,将其倒序输出,并求各位数之和
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("输入一个整数:");
int num = in.nextInt();
int sum = 0;
int res = 0;
while(num != 0)
{
sum += num%10; //求和
res = res*10 + num%10; //数字逆序
num /= 10;
}
System.out.println("倒序输出为"+ res + ",各位求和为"+ sum);
}
6.4 计算阶乘
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int res = 1;
int t = n;
for(; t!=0; t--)
{
res *= t;
}
System.out.println(n+"! = " + res);
}
6.5 输出100以内的素数
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean isPrime = true;
for(int n=2; n<=100; n++)
{
for(int i=2; i<=Math.sqrt(n); i++)
{
if(n%i == 0)
{
isPrime = false;
}
}
if(isPrime)
{
System.out.print(n + " ");
}
isPrime = true;
}
}
6.6 输出前50个素数
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean isPrime = true;
int cnt = 0;
for(int n=2; cnt<50; n++)
{
for(int i=2; i<=Math.sqrt(n); i++)
{
if(n%i == 0)
{
isPrime = false;
}
}
if(isPrime)
{
cnt++;
System.out.println("第"+cnt+"个素数: "+n);
}
isPrime = true;
}
}
6.7 跳出多重循环的方法
public static void main(String[] args) {
// TODO Auto-generated method stub
OUT:
for(int i=1; i<10; i++)
{
for(int j=1; j<10; j++)
{
for(int k=1; k<10; k++)
{
if(i+j+k==10)
{
System.out.println(i + "," + j + "," + k);
break OUT; //跳出多重循环
}
}
}
}
}
6.8 计算1 + 1/2 + 1/3 +……+ 1/n
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double sum = 0;
for(int i=1; i<=n; i++)
{
sum += 1.0/i;
}
System.out.printf("%.2f",sum);
}
6.9 计算1 - 1/2 + 1/3 -……+ 1/n
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double sum = 0;
for(int i=1; i<=n; i++)
{
if(i%2 == 0)
{
sum += -1.0/i;
}
else
{
sum += 1.0/i;
}
}
System.out.printf("%.2f",sum);
}
6.10 计算两个数的最大公约数
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int oa = a;
int ob = b;
int r = 0;
while(b!=0)
{
r = a % b;
a = b;
b = r;
}
System.out.println(oa + "和" + ob + "的最大公约数是" + a);
}
6.11 for-each循环的应用
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] data = {1, 2, 3, 4, 5, 6};
for(int i : data) //将data中的每一个数赋值给i
{
System.out.println(i);
}
}
总结:for-each循环适合遍历数组时取数
7 数组
7.1 思考程序运行结果
结果:
2
3
4
5
6
总结:
7.2 二维数组:tic-tac-toe游戏
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int numOfX = 0;
int numOfO = 0;
final int SIZE = 3;
boolean gotResult = false;
int[][] board = new int[SIZE][SIZE];
//读入矩阵
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
{
board[i][j] = in.nextInt();
}
}
//检查行
for(int i=0; i<SIZE; i++)
{
numOfX = 0;
numOfO = 0;
for(int j=0; j<SIZE; j++)
{
if(board[i][j] == 1)
{
numOfX++;
}
else
{
numOfO++;
}
}
if(numOfX == SIZE || numOfO == SIZE)
{
gotResult = true;
break;
}
}
//检查列
if(!gotResult) {
for(int i=0; i<SIZE; i++)
{
numOfX = 0;
numOfO = 0;
for(int j=0; j<SIZE; j++)
{
if(board[j][i] == 1)
{
numOfX++;
}
else
{
numOfO++;
}
}
if(numOfX == SIZE || numOfO == SIZE)
{
gotResult = true;
break;
}
}
}
//检查主对角线
if(!gotResult)
{
numOfX = 0;
numOfO = 0;
for(int i=0; i<SIZE; i++)
{
if(board[i][i] == 1)
{
numOfX++;
}
else
{
numOfO++;
}
}
if(numOfX == SIZE || numOfO == SIZE)
{
gotResult = true;
}
}
//检查副对角线
if(!gotResult)
{
numOfX = 0;
numOfO = 0;
for(int i=0; i<SIZE; i++)
{
if(board[i][SIZE-i-1] == 1)
{
numOfX++;
}
else
{
numOfO++;
}
}
if(numOfX == SIZE || numOfO == SIZE)
{
gotResult = true;
}
}
//判断结果
if(numOfX == SIZE)
{
System.out.println("X");
}else if(numOfO == SIZE)
{
System.out.println("O");
}
else
{
System.out.println("No result!");
}
}
8 包裹类型
9 字符串
9.1 输入字符串
9.2 字符串内容判等
9.3 字符串大小比较
9.4 访问字符串的字符
9.5 截取字符串的子串
9.6 在字符串中查找字符/字符串
9.7 其他操作
9.8 在switch-case中使用字符串
10 Math类
- abs() 计算绝对值
- pow() 计算幂次
- random() 生成随机数
- round() 四舍五入
11 函数
11.1 计算m与n之间有多少个素数,并求和
//素数判别
public static boolean isPrime(int x) {
boolean isPrime = true;
for(int i=2; i<=Math.sqrt(x); i++)
{
if(x % i == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
if(m == 1) m = 2; //1不是素数,直接排除在外
int cnt = 0;
int sum = 0;
for(int i=m; i<=n; i++)
{
if(isPrime(i))
{
cnt++;
sum += i;
}
}
System.out.println("在"+m+"与"+n+"之间有"+cnt+"个素数,和为"+sum);
}
11.2 求出1~10、20~30、35~45的三个和
public static void sum(int a, int b)
{
int sum = 0;
for(int i=a; i<=b; i++)
{
sum += i;
}
System.out.println(a+"到"+b+"的和是"+sum);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
sum(1,10);
sum(20,30);
sum(35,45);
}
12 参数传递
- Java在调用函数时,只能传值给函数
public static void swap(int a, int b)
{
int t = a;
a = b;
b = t;
System.out.println("a="+a+",b="+b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 10;
int b = 20;
swap(a, b);
System.out.println("a="+a+",b="+b);
}
PS:这种方式无法交换两个变量的值