目录
10.2 定义一个整型变量并赋任意五位正整数作为初始值,输出各位数字之和 (例如:12345 各位之和是:1+2+3+4+5 。也就是 15)
10.3 定义整型变量 a、b,写出将 a、b 两个变量值进行互换的程序 (要求不能使用第三个变量)
10.5 人工智能的概念刚兴起时,网上流传了一段价值一个亿的代码
1. Java 发展史和其特点特性
- Java程序设计语言
- 各硬件平台(操作系统)的Java虚拟机实现
- Class文件格式
- Java类库API
- 第三方类库(来自商业或开源社区)
2. 掌握环境搭建(JDK 与 eclipse 下载安装)
已经不需要配置CLASSPATH了
windows环境注意Path里用分号
macOS环境注意Path用冒号,export PATH=$JAVA_HOME/bin:$PATH:.
3. 学习如何编写代码
掌握好语法是关键:
java 是严格区分大小写的;
java 是一种自由格式的语言;
代码分为结构定义语句和功能执行语句;
功能执行语句的最后必须用分号结束;
面向对象的概念:封装、继承、类;
4. 了解 Java 特性,JDK 和 JRE 等概念
Java还会自动地收集内存垃圾,使得内存管理变得更为简单。
Java还提供了丰富的类库、API文档以及第三方开发包,还有大量Java的开源项目。
面向对象性:java语言提倡“万物皆对象”,语法中不能在类外面定义单独的数据和函数
分布性:⑴操作分布:即在多个不同的主机上不知相关操作。
⑵数据分布:将数据分别存放在不同的主机上,这些主机是网络中的不同成员
可移植性:Java的类库也提供了针对不同平台的接口,所有这些类库也可以被移植。
JDK是开发工具包,JRE是运行时环境包
5.Java基本数据类型:
八种基本数据类型:数值型和布尔型
数值型:整型,浮点型,字符型
引用数据类型:类,接口,数组
6.定义变量:变量类型 变量名称
7.使用变量:
按所属的数据类型划分:1)基本数据类型变量 2)引用数据类型变量
按被声明的位置划分:
1)局部变量:方法或语句块内部定义的变量
2)成员变量:方法外部、类的内部定义的变量
8.运算符:
算术运算符:+ - * / %(取模或求余) ++(自增) --(自减)
赋值运算符:= += -= *= /=(除等于) %=(模等于)
关系运算符:== != < > <= >=
逻辑运算符:& | ^ ! && ||
字符串连接运算符:String str = "jia" + "you", 结果:jiayou
三目运算符:(x)?y:z
9.整型变量互换
9.1 利用第三方变量
public class ThirdVariable {
/*
* 利用第三方变量
*/
public static void main(String[] args) {
int x=9, y=5;
System.out.println("x="+x+", y="+y);
int thirdVar;
thirdVar = x;
x = y;
y = thirdVar;
System.out.println("x="+x+", y="+y);
}
}
9.2 不使用第三方变量
public class NoThirdVariable {
/*
* 不利用第三方变量
*/
public static void main(String[] args) {
int x = 5, y = 9;
System.out.println("x="+x+",y="+y);
y=y-x; // 4=9-5
x=x+y; // 9=5+4
y=x-y; // 5=9-4
System.out.println("x="+x+",y="+y);
}
}
9.3 利用异或逻辑关系
public class XorLogic {
/*
* 利用异或的逻辑关系
*/
public static void main(String[] args) {
int x=9,y=5;
System.out.println("x="+x+",y="+y);
x=x^y;
y=x^y; //y=(x^y)^y
x=x^y; //x=(x^y)^x
System.out.println("x="+x+",y="+y);
}
}
10.通过一些例子掌握基本语法、流程控制、数组
10.1 回文
package com.company;
public class PalindromeMain {
public static void main(String[] args) {
// write your code here
int x = 45654;
System.out.printf("%d is palindrome? %s", 45654, String.valueOf(isPalindrome(45654)));
System.out.println();
System.out.printf("%d is palindrome? %s", 89098, String.valueOf(isPalindrome(89098)));
System.out.println();
System.out.printf("%d is palindrome? %s", 68268, String.valueOf(isPalindrome(68268)));
System.out.println();
}
static boolean isPalindrome(int one5Int) {
boolean result = false;
if(one5Int < 0 || (one5Int % 10 == 0 && one5Int != 0)) {
return false;
}
int num = 0;
while (one5Int > num) {
num = num * 10 + one5Int % 10;
one5Int/=10;
}
return one5Int == num || one5Int == num/10;
}
}
10.2 定义一个整型变量并赋任意五位正整数作为初始值,输出各位数字之和 (例如:12345 各位之和是:1+2+3+4+5 。也就是 15)
package com.company;
public class FiveNumberSumMain {
public static void main(String[] args) {
// write your code here
int x = 45654;
System.out.printf("%d is palindrome? %s", 45654, String.valueOf(numberSum(x)));
System.out.println();
System.out.printf("%d is palindrome? %s", 89098, String.valueOf(numberSum(89098)));
System.out.println();
System.out.printf("%d is palindrome? %s", 68268, String.valueOf(numberSum(68268)));
System.out.println();
}
static int numberSum(int one5Int) {
int sum = 0;
if(one5Int < 0) {
return -1;
}
while (one5Int > 0) {
sum = sum + one5Int % 10;
one5Int/=10;
}
return sum;
}
}
10.3 定义整型变量 a、b,写出将 a、b 两个变量值进行互换的程序 (要求不能使用第三个变量)
package com.company;
public class ExchangeNumMain {
public static void main(String[] args) {
// write your code here
NumClass data = new NumClass(8, 9);
System.out.printf("Before exchange: %d and %d\n", data.x, data.y);
exchangeNum(data);
System.out.printf("After exchange: %d and %d\n", data.x, data.y);
System.out.println();
data = new NumClass(45, 29);
System.out.printf("Before exchange: %d and %d\n", data.x, data.y);
exchangeNum(data);
System.out.printf("After exchange: %d and %d\n", data.x, data.y);
System.out.println();
data = new NumClass(127, 238);
System.out.printf("Before exchange: %d and %d\n", data.x, data.y);
exchangeNum(data);
System.out.printf("After exchange: %d and %d\n", data.x, data.y);
System.out.println();
}
static NumClass exchangeNum(NumClass data) {
data.x = data.x + data.y;
data.y = data.x - data.y;
data.x = data.x - data.y;
return data;
}
public static class NumClass {
int x;
int y;
public NumClass(int x, int y) {
this.x = x;
this.y = y;
}
}
}
10.4 Hello World
package com.company;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!!!");
}
}
10.5 人工智能的概念刚兴起时,网上流传了一段价值一个亿的代码
package com.company;
import java.util.Scanner;
public class AIMain {
public static void main(String[] args) {
// 通过 Scanner 类来获取用户的输入
Scanner scanner = new Scanner(System.in);
String question;
while (true) {
// 从键盘接收数据
question = scanner.next();
// 替换字符"吗"为空字符
question = question.replace("吗", "");
// 替换字符"我"为"我也"
question = question.replace("我", "我也");
// 替换字符"?"为"!"
question = question.replace("?", "!");
// 替换中文标点符号"?"为"!"
question = question.replace("?", "!");
System.out.println(question);
}
}
}
10.6 计算应缴金额
package com.company;
import java.util.Scanner;
public class Discount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String money;
double result = 0;
money = scanner.next();
Double dMoney = Double.valueOf(money);
if (dMoney > 0 && dMoney < 2000) {
result = dMoney * 0.9;
} else if (dMoney > 2000 && dMoney < 4000) {
result = dMoney * 0.8;
} else if (dMoney > 4000 && dMoney < 8000) {
result = dMoney * 0.75;
} else if (dMoney > 8000) {
result = dMoney * 0.7;
}
System.out.printf("Input original price is %S, Discount pricie is %s\n", money.toString(), String.valueOf(result));
System.out.println();
}
}
10.7 计算该年该月天数
package com.company;
import java.util.Scanner;
public class GetYearDays {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your YEAR:");int year = scanner.nextInt();
System.out.println("Please input your MONTH:");int month = scanner.nextInt();int num = 0;int day = 0;
switch (month){
case 12:
day=31;
break;
case 11:
day=30;
break;
case 10:
day=31;
break;
case 9:
day=30;
break;
case 8:
day=31;
break;
case 7:
day=31;
break;
case 6:
day=30;
break;
case 5:
day=31;
break;
case 4:
day=30;
break;
case 3:
day=31;
break;
case 2:
day=(isLeap(year))?29:28;
break;
case 1:
day=31;
break;
}
System.out.printf("The year %d is %b leap year and the month %d has %d days", year, isLeap(year), month, day);
System.out.println();
}
static boolean isLeap(int year) {
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
} else {
return false;
}
}
}
10.8 图形打印任务
图形一
*
**
***
****
*****
图形二
*****
****
***
**
*
图形三
*
***
*****
*******
*********
package com.company;
public class TriangleStars {
public static void main(String[] args) {
//画图形一
System.out.println("图形一");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
System.out.println("图形二");
//画图形二
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--)
System.out.print("*");
System.out.println();
}
System.out.println();
System.out.println("图形三");
//画图形三
for (int i = 1; i <= 5; i++) {
for (int j = 5; i <= j; j--)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int j = 1; j < i; j++)
System.out.print("*");
System.out.println();
}
}
}
10.9 打印九九乘法表
打印9x9乘法表:
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
package com.company;
public class MultiplicationTable {
public static void main(String[] args) {
System.out.println("打印9x9乘法表:");
printTable();
}
// 打印九九乘法表
static void printTable() {
// 外层循环控制纵向打印多少次,从1到9
for (int i = 1; i <= 9; i++) {
// 内层循环控制横向打印多少次,从1到i
for (int j = 1; j <= i; j++) {
System.out.print(j + "x" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}// printTable
}
10.10 打印三位数中的所有水仙花数
package com.company;
public class ArmstrongNumber {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int firstNum = i / 100;
int secondNum = i / 10 % 10;
int thirdNum = i % 10;
if (firstNum * firstNum * firstNum + secondNum * secondNum * secondNum + thirdNum * thirdNum * thirdNum == i) {
System.out.println("三位数的水仙花数为:" + i);
}
}
}
}
10.11 选队长:
package com.company;
import java.util.Scanner;
public class CaptainSelection {
public static void main(String[] args) {
System.out.println("请输入队伍人数:");
Scanner input = new Scanner(System.in);
int people = input.nextInt();
int[] team = new int[people];
int num = team.length;//数组长度,用于判断是否剩余最后一个人
int count = 0;//报数
int index = 0;//数组下标
int removeCount = 0;
int remains = 0;
while (num > 1) {
//默认数组值为0,这个即计算最后没有被设置为-1(没报3)的个数
if (team[index] == 0) {
++count;
++remains;
}
if (count == 3) {
team[index] = -1;
//num--;
removeCount++;
count = 0;
}
if (remains == num) {//重新报数
index = 0;
num-=removeCount;
removeCount = 0;
remains = 0;
} else {
index++;
}
}
for (int i = 0; i < team.length; i++) {
if (team[i] == 0) {
System.out.println("队长是原来第" + (i + 1) + "位同学");
}
}
}
}
10.12 查找某个整数
package com.company;
import java.util.Scanner;
public class FindIntIndex {
public static void main(String[] args) {
int result = -1;
int[] array = new int[10];
for (int i = 0; i < array.length; ) {
Scanner input = new Scanner(System.in);
System.out.println("请输入第" + (i + 1) + "个数:");
if (input.hasNextInt()) {//判断控制台得到的是否是整数
int num = input.nextInt();
array[i] = num;
i++;//当时int类型的数才+1
} else {
System.out.println("请输入一个整数!");
}
}
System.out.println("请输入要查找的数:");
Scanner find = new Scanner(System.in);
int findNum = find.nextInt();
int max = array[0];
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (findNum == array[i]) {
result = i;
}
}
if (result == -1) {
System.out.printf("查找数%d的下标为:%d", findNum, -1);
}
System.out.printf("查找数%d的下标为:%d", findNum, result);
System.out.println();
}
}
10.13 找出数据最值
package com.company;
import java.util.Scanner;
public class FindMaxInt {
public static void main(String[] args) {
int[] array = new int[10];
for (int i = 0; i < array.length; ) {
Scanner input = new Scanner(System.in);
System.out.println("请输入第" + (i + 1) + "个数:");
if (input.hasNextInt()) {//判断控制台得到的是否是整数
int num = input.nextInt();
array[i] = num;
i++;//当时int类型的数才+1
} else {
System.out.println("请输入一个整数!");
}
}
int max = array[0];
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
}
if (min > array[i]) {
min = array[i];
}
}
System.out.println("最大值为:" + max + ",最小值为:" + min);
}
}
10.14 两数之和
package com.company;
import java.util.HashMap;
import java.util.Scanner;
public class Find2NumberSum {
public static void Find_Num(int[] arr, int key) {
int count = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
int num = key - arr[i];
if (map.containsKey(num)) {
System.out.println(map.get(num) + " " + i);
count++;
}
map.put(arr[i], i);
}
if (count == 0) {
System.out.println("-1 -1");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Input the Target sum:");
int key = input.nextInt();
System.out.println("Input the length of the array:");
int length = input.nextInt();
int[] arr = new int[length];
System.out.println("Input the elements of the array:");
for (int i = 0; i < length; i++)
arr[i] = input.nextInt();
Find_Num(arr, key);
}
}
10.15 排序查找
package com.company;
public class BinarySearch {
public static void main(String[] args) {
//排序并查找
int[] a = {1, 3, 9, 5, 6, 7, 15, 4, 8};
//储存最大值
int temp;
//用冒泡排序对输入的数字进行排序处理
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
int f = 6;
//利用二分查找
//最小边界下标
int Minindex = a[0];
//最大边界下标
int Maxindex = a.length - 1;
//中间下标
int centerindex = (Maxindex + Minindex) / 2;
while (true) {
if (a[centerindex] > f) {
//如果中间数据较大,最大范围下标=centerindex-1
Maxindex = centerindex - 1;
} else if (a[centerindex] < f) {
//如果中间数据较小,最小范围下标=centerindex+1
Minindex = centerindex + 1;
} else {
//如果中间数据相等,输出中间下标
break;
}
if (Minindex > Maxindex) {
centerindex = -1;
System.out.println("你查找的数不存在");
}
//当边界更新时,需要更新中间下标
centerindex = (Maxindex + Minindex) / 2;
}
System.out.println("6的下标为" + centerindex);
}
}
10.16 移动零
package com.company;
public class NotMove {
public static void main(String[] args) {
int[] nums = {0, 1, 0, 3, 12};
int temp;
//外层循环控制的时,比较的轮数
//外层循环次数:lenth-1
for (int i = 0; i < nums.length - 1; i++) {
//内层循环控制的是,每轮比较的次数
//第i轮(i从0开始计算),所以比较次数为:lenth-i-1
for (int j = 0; j < nums.length - i - 1; j++) {
if (nums[j] == 0) {
//当j下标值为0时,则一直交换到最右边
//交换
temp = nums[j + 1];
nums[j + 1] = nums[j];
nums[j] = temp;
//这里打印看一下这一次交换后的元素排序,更好理解
for (int n = 0; n < nums.length; n++) {
System.out.print(nums[n] + " ");
}
//换行
System.out.println();
}
}
}
System.out.println("最终交换结果为:");
for (int j = 0; j < nums.length; j++) {
System.out.print(nums[j] + " ");
}
System.out.println();
}
}