“忙”是至於一切矯情的良藥
Hello.java
package ExercisesToPractice;//包名
/**
* 這個代碼是用來輸出helloworld
* 【塊註釋】
*/
public class Hello {//類名
//main函數是程序的入口【行註釋】
public static void main(String[] args) {
System.out.println("Hello");//打印語句
System.out.println("World");
}
}
MultiplicationTables.java
package ExercisesToPractice;
//簡單打印乘法表
public class MultiplicationTables {
/**
* @Description:乘法表
* @Author: cc雪影
*/
public static void main(String[] args) {
System.out.println("1*1=1");
System.out.println("1*2=2 2*2=4");
System.out.println("1*3=3 2*3=6 3*3=9");
}
}
輸出:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
Variable.java
package ExercisesToPractice;
public class Variable {
/**
* @Description:變量
* @Author: cc雪影
*/
public static void main(String[] args) {
//1.聲明變量
// 聲明變量 學校名稱
String schoolname;
// 聲明變量 學校地址
String schooladress;
// 聲明變量 學校類型
String type;
// 聲明變量 學校校齡
int schoolage;
//2.賦值
schoolname = "鄭州大學";
schooladress = "鄭州市";
type = "university";
schoolage = 100;
}
}
TheOperator.java
package ExercisesToPractice;
public class TheOperator {
/**
* @Description:操作符
* @Author: cc雪影
*/
public static void main(String[] args) {
System.out.println("| "+"姓名"+" | "+"專業"+" | "+"年級"+" | "+"分數"+" |");
String name;
String professional;
String grade;
int score;
int sum = 0,count=0;
name = "張三";
professional ="軟工";
grade = "大一";
score =99;
count++;
sum = sum + score;
System.out.println("| "+name+" | "+professional+" | "+grade+" | "+score+" |");
name = "李四";
professional ="計科";
grade = "大二";
score =93;
count++;
sum = sum + score;
System.out.println("| "+name+" | "+professional+" | "+grade+" | "+score+" |");
name = "王五";
grade = "大一";
score =90;
System.out.println("| "+name+" | "+professional+" | "+grade+" | "+score+" |");
count++;
sum = sum + score;
System.out.println("平均成績:"+sum/count);
}
}
輸出:
| 姓名 | 專業 | 年級 | 分數 |
| 張三 | 軟工 | 大一 | 99 |
| 李四 | 計科 | 大二 | 93 |
| 王五 | 計科 | 大一 | 90 |
平均成績:94
Function.java
package ExercisesToPractice;
public class Function {
/**
* @Description:方法(函數)
* @Author: cc雪影
*/
public static void main(String[] args) {//main方法
double r1 = Math.random();//調用Math類的random方法
System.out.println("隨機生成一個[0,1)的數是:" + r1);
int r2 = (int) (Math.random() * 10);
System.out.println("隨機生成一個[0,10)的數是:" + r2);
int n1 = (int) (Math.random() * 10);
int n2 = (int) (Math.random() * 10);
int n3 = (int) (Math.random() * 10);
int n4 = (int) (Math.random() * 10);
int n5 = (int) (Math.random() * 10);
int n6 = (int) (Math.random() * 10);
System.out.println("驗證碼1: " + n1 + " " + n2 + " " + n3 + " " + n4 + " " + n5 + " " + n6);
double r3 = Math.random() + 1;
int num = (int) (r3 * 100000);
Integer i1, i2, i3, i4, i5, i6;
String code;
i1 = num % 10;
num = num / 10;
i2 = num % 10;
num = num / 10;
i3 = num % 10;
num = num / 10;
i4 = num % 10;
num = num / 10;
i5 = num % 10;
num = num / 10;
i6 = num % 10;
num = num / 10;
code = i1.toString()+i2.toString()+i3.toString()+i4.toString()+i5.toString()+i6.toString();//調用Integer類中的toString方法
System.out.println("驗證碼2: "+code);
}
}
輸出:
隨機生成一個[0,1)的數是:0.8540207047284485
隨機生成一個[0,10)的數是:2
驗證碼1: 9 7 3 0 0 6
驗證碼2: 804061
CustomMethod.java
package ExercisesToPractice;
public class CustomMethod {
/**
* @Description:自定義方法
* @Author: cc雪影
*/
// 執行排隊的方法
public static void lineUp() {//小駝峯命名,首字母小寫
}
// 執行點菜的方法
public static void order() {
}
//創建一個名字爲play的方法
public static void play(){
}
//創建兩個方法,一個用來介紹公司,一個用來聯繫公司
public static void showCompany(){
}
public static void contactCompany(){
}
//定義一個產生四位驗證碼的驗證碼生成器code
public static void code(){
String code="";
double r = Math.random();
int num = (int) (r * 10000);
Integer i1, i2, i3, i4;
i1 = num % 10;
num = num / 10;
i2 = num % 10;
num = num / 10;
i3 = num % 10;
num = num / 10;
i4 = num % 10;
num = num / 10;
code = i1.toString() + i2.toString() + i3.toString() + i4.toString();
System.out.println(code);
}
//定義一個產生指定位數的驗證碼生成器customCode
public static void customCode(int length){
String code="";
int len = 1;
double r = Math.random();
Integer i1, i2, i3, i4,integer;
if(length<1){
System.out.println("非法:customCode方法只能接受比1大的整數");
return;
}
for(int j=1;j<=length;j++){
len = len * 10;
}
int num = (int) (r * len);
for(int i=1;i<=length;i++){
integer = num % 10;
num = num / 10;
code = integer.toString() +code;
}
System.out.println(code);
}
//產生指定位數的驗證碼生成器的改進customCode1
public static void customCode1(int length){
if(length<1){
System.out.println("非法:customCode方法只能接受比1大的整數");
return;
}
String code = "";
for(int i=1;i<=length;i++){
Integer num = new Integer((int)(Math.random()*10));
code = code + num.toString();
}
System.out.println(code);
}
//產生指定位數的驗證碼生成器的改進customCode2
public static void customCode2(int length){
if(length<1){
System.out.println("非法:customCode方法只能接受比1大的整數");
return;
}
int len = 1;
for(int j=1;j<length;j++){
len = len * 10;
}
int code = (int) ((Math.random() * 9 + 1) * len);
System.out.println(code);
}
public static void customCode2(int length,String name){
if(length<1){
System.out.println("尊敬的 "+name+" ,您的操作非法:customCode方法只能接受比1大的整數");
return;
}
int len = 1;
for(int j=1;j<length;j++){
len = len * 10;
}
int code = (int) ((Math.random() * 9 + 1) * len);
System.out.println("尊敬的用戶 "+name+" ,您本次的驗證碼是:"+code);
}
public static void main(String[] args) {
//方法的調用邏輯語句
code();
customCode(0);
customCode1(4);
customCode2(6);
customCode2(4,"老王");
}
}
輸出:
7018
非法:customCode方法只能接受比1大的整數
1136
919133
尊敬的用戶 老王 ,您本次的驗證碼是:1756
LogicStatements.java
package ExercisesToPractice;
public class LogicStatements {
/**
* @Description:邏輯語句
* @Author: cc雪影
*/
public static void report(String s, int num) {
System.out.print(s + "本次考試 " + num + " 分 ");
if (num >= 60) {//邏輯判斷 if語句
System.out.println("恭喜您及格了");
}
if (num < 60) {
System.out.println("同志仍需努力");
}
}
public static void message(int num) {
if (num <= 25) {
System.out.println("人人都應該編程,現在加入吧");
}
if (num > 60) {
System.out.println("人生就應該及時行樂,港澳臺三日遊來否?");
}
}
public static void number(int n) {
if (n % 2 == 0) {
System.out.println(n + "是偶數");
} else {//選擇性執行
System.out.println(n + "是奇數");
}
}
public static void student(String name, int grade) {
if (grade >= 60) {
System.out.println(name + "恭喜你及格啦!");
} else {
System.out.println(name + "繼續加油吧少年!");
}
}
public static void studentReport(String name, int grade) {
if (grade >= 90) {
System.out.println(name + ",你本次的成績爲優秀");
} else if (grade >= 80) {//條件鏈 else if
System.out.println(name + ",你本次的成績爲良好");
} else if (grade >= 60) {
System.out.println(name + ",你本次的成績爲及格");
} else {
System.out.println(name + ",你本次的成績爲不通過,需要補考");
}
}
public static void schedule(double time) {
if (time > 0) {//嵌套條件
System.out.print("Hello! ");
if (time == 7.0) {
System.out.println("起牀啦");
}
if (time == 13.3) {
System.out.println("午睡時間");
}
if (time == 23.0) {
System.out.println("睡覺啦,晚安");
}
}
}
public static void inform(int level, int grade) {
System.out.print(level + " " + grade+" ");
if (grade > 425) {//嵌套條件
if (level == 4) {
System.out.println("恭喜你通過英語四級考試");
}
if (level == 6) {
System.out.println("恭喜你通過英語六級考試");
}
}
}
public static void main(String[] args) {
report("老王", 99);
report("老王", 59);
message(18);
message(70);
number(2);
number(3);
student("Nick", 59);
student("Nick", 99);
studentReport("Nick", 65);
studentReport("Nick", 89);
schedule(7);
schedule(13.3);
schedule(23);
inform(4, 500);
}
}
輸出:
老王本次考試 99 分 恭喜您及格了
老王本次考試 59 分 同志仍需努力
人人都應該編程,現在加入吧
人生就應該及時行樂,港澳臺三日遊來否?
2是偶數
3是奇數
Nick繼續加油吧少年!
Nick恭喜你及格啦!
Nick,你本次的成績爲及格
Nick,你本次的成績爲良好
Hello! 起牀啦
Hello! 午睡時間
Hello! 睡覺啦,晚安
4 500 恭喜你通過英語四級考試
ReturnStatement.java
package ExercisesToPractice;
public class ReturnStatement {
/**
* @Description:返回語句
* @Author: cc雪影
*/
public static void main(String[] args) {
plan(37);
plan(39);
}
public static void plan(double temperature) {
System.out.println("準備出門去學校");
if (temperature > 38) {
//判斷爲發熱,不適宜做運動
return;//返回語句,來完成程序的中斷
}
System.out.println("去操場踢球");
}
}
輸出:
準備出門去學校
去操場踢球
準備出門去學校
Recursive.java
package ExercisesToPractice;
public class Recursive {
/**
* @Description:調用方法
* @Author: cc雪影
*/
public static int count = 0;
public static void main(String[] args) {
cutDown(5);
coding(30);
}
public static void cutDown(int number) {
System.out.print(number + " ");
number--;
if (number == 0) {
System.out.println("Go!");
return;
}
cutDown(number);//遞歸
}
public static void coding(int hairamount) {
count++;
System.out.print(hairamount + "撮頭髮,敲了一會代碼,掉了一撮,");
hairamount--;
System.out.print(hairamount + "撮頭髮在頭上; ");
if (count % 2 == 0) {
if (hairamount == 0) {
} else {
System.out.println();
}
}
if (hairamount == 0) {
System.out.println();
System.out.print("頭已光!沒有頭髮可以掉!!!");
return;
}
coding(hairamount);//遞歸
}
}
輸出:
5 4 3 2 1 Go!
30撮頭髮,敲了一會代碼,掉了一撮,29撮頭髮在頭上; 29撮頭髮,敲了一會代碼,掉了一撮,28撮頭髮在頭上;
28撮頭髮,敲了一會代碼,掉了一撮,27撮頭髮在頭上; 27撮頭髮,敲了一會代碼,掉了一撮,26撮頭髮在頭上;
26撮頭髮,敲了一會代碼,掉了一撮,25撮頭髮在頭上; 25撮頭髮,敲了一會代碼,掉了一撮,24撮頭髮在頭上;
24撮頭髮,敲了一會代碼,掉了一撮,23撮頭髮在頭上; 23撮頭髮,敲了一會代碼,掉了一撮,22撮頭髮在頭上;
22撮頭髮,敲了一會代碼,掉了一撮,21撮頭髮在頭上; 21撮頭髮,敲了一會代碼,掉了一撮,20撮頭髮在頭上;
20撮頭髮,敲了一會代碼,掉了一撮,19撮頭髮在頭上; 19撮頭髮,敲了一會代碼,掉了一撮,18撮頭髮在頭上;
18撮頭髮,敲了一會代碼,掉了一撮,17撮頭髮在頭上; 17撮頭髮,敲了一會代碼,掉了一撮,16撮頭髮在頭上;
16撮頭髮,敲了一會代碼,掉了一撮,15撮頭髮在頭上; 15撮頭髮,敲了一會代碼,掉了一撮,14撮頭髮在頭上;
14撮頭髮,敲了一會代碼,掉了一撮,13撮頭髮在頭上; 13撮頭髮,敲了一會代碼,掉了一撮,12撮頭髮在頭上;
12撮頭髮,敲了一會代碼,掉了一撮,11撮頭髮在頭上; 11撮頭髮,敲了一會代碼,掉了一撮,10撮頭髮在頭上;
10撮頭髮,敲了一會代碼,掉了一撮,9撮頭髮在頭上; 9撮頭髮,敲了一會代碼,掉了一撮,8撮頭髮在頭上;
8撮頭髮,敲了一會代碼,掉了一撮,7撮頭髮在頭上; 7撮頭髮,敲了一會代碼,掉了一撮,6撮頭髮在頭上;
6撮頭髮,敲了一會代碼,掉了一撮,5撮頭髮在頭上; 5撮頭髮,敲了一會代碼,掉了一撮,4撮頭髮在頭上;
4撮頭髮,敲了一會代碼,掉了一撮,3撮頭髮在頭上; 3撮頭髮,敲了一會代碼,掉了一撮,2撮頭髮在頭上;
2撮頭髮,敲了一會代碼,掉了一撮,1撮頭髮在頭上; 1撮頭髮,敲了一會代碼,掉了一撮,0撮頭髮在頭上;
頭已光!沒有頭髮可以掉!!!
MethodOfValue.java
個人稅務計算器:
package ExercisesToPractice;
public class MethodOfValue {
/**
* @Description:有返回值的方法
* @Author: cc雪影
*/
public static void main(String[] args) {
System.out.println(code(100000));
double amount = ratepaying(20000, 1500, true, true, true);
System.out.println(amount);
}
public static int code(int len) {
/**
* @description:自定義位數驗證碼生成器
* @param len 長度(10的冪)
* @createDate: 2020/5/17 21:44
* @return: int
*/
// 得到隨機數結果
double val = Math.random();
// 隨機數結果*9+1 然後再乘以位數得到驗證碼
int result = (int) ((val * 9 + 1) * len);
return result;
}
public static double ratepaying(double income, double socialSecurity, boolean onlyChild, boolean hasChild, boolean hasParents) {
/**
* @description:個人稅務計算器
* @param income 工資收入
* @param socialSecurity 5險一金金額
* @param onlyChild 是否獨生子女
* @param hasChild 是否有小孩在讀書
* @param hasParents 是否有60歲以上的父母需要贍養
* @createDate: 2020/5/17 21:24
* @return: double
*/
double money = income;
money = money - socialSecurity;
if (hasChild) {
money = money - 1000;
}
if (hasParents) {
if (onlyChild) {
money = money - 2000;
}
}
if (money >= 3000 && money <= 12000) {
money = money * 0.1 + 210;
}
if (money > 12000 && money <= 25000) {
money = money * 0.2 + 1410;
}
if (money > 25000 && money <= 35000) {
money = money * 0.25 + 2660;
}
return money;
}
}
輸出:
456076
4510.0
Array.java
package ExercisesToPractice;
public class Array {
/**
* @Description:數組的使用
* @Author: cc雪影
* @Date:21:53 2020/5/17
*/
public static void main(String[] args) {
int[] arr = new int[4];//聲明數組arr,並申請空間
int size = arr.length;
System.out.println(size);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
for (int i=0;i<getArray(5).length;i++){
System.out.print(getArray(5)[i]+" ");
}
}
public static int[] getArray(int radix){
/**
* @description:根據底數獲取數組
* @param radix 底數
* @createDate: 2020/5/17 22:45
* @return: int[] 方法的返回類型可以是數組類型
*/
if(radix<0) {
int[] arr = {-1};
return arr;
}
int []array=new int[radix];
int res = 1;
for(int i=0;i<radix;i++){
res = res * radix;
array[i] = res;
}
return array;
}
}
輸出:
4
5 25 125 625 3125
String.java
package ExercisesToPractice;
/**
* @Author: cc雪影
* @Description:字符串方法及操作
* @Date: Create in 16:21 2020/5/21
*/
public class String {
public static void main(String[] args) {
String string = "今天我們來複習字符串";
//調用字符串的length方法來獲取字符串的長度
System.out.println("string 的長度: " + string.length());
boolean countpass = validatecount(string);
System.out.println("string 的長度是否在限制範圍內: " + countpass);
//取字符串第一個字符
System.out.println("string 的第一個字符是: " + string.charAt(0));
//取字符串第二個字符
System.out.println("string 的第二個字符是: " + string.charAt(1));
//去掉左右多餘的空格
String s = " 我好像是全世界最帥的人程序員了 ";
System.out.println(s.trim());
String str = "Java是一種廣泛使用的計算機編程語言,擁有跨平臺、面向對象、泛型編程的特性,廣泛應用於企業級Web應用開發和移動應用開發。任職於太陽微系統的詹姆斯·高斯林等人於1990年代初開發Java語言的雛形,最初被命名爲Oak,目標設置在家用電器等小型系統的編程語言,應用在電視機、電話、鬧鐘、烤麪包機等家用電器的控制和通信。由於這些智能化家電的市場需求沒有預期的高,Sun公司放棄了該項計劃。隨着1990年代互聯網的發展,Sun公司看見Oak在互聯網上應用的前景,於是改造了Oak,於1995年5月以Java的名稱正式發佈。Java伴隨着互聯網的迅猛發展而發展,逐漸成爲重要的網絡編程語言。";
f(str, "Java");
//substring方法進行字符串拼接
System.out.println(str.substring(4));
System.out.println(str.substring(4, 19));
judgeDocument("String.java");
String s1 = str.replace("Java", "Python");
System.out.println(s1);
String s2 = "Java是一種廣泛使用的計算機編程語言,於1995年5月以Java的名稱正式發佈。";
//打印第二個"Java"後的五個字符
int index = s2.indexOf("Java", s2.indexOf("Java") + 1);
System.out.println(index);
String s3 = s2.substring(index + 4, index + 4 + 5);
System.out.println(s3);
}
public static boolean validatecount(String message) {
/**
* @description:判斷字符串長度是否達到上限(默認去除空格)
* @param message
* @createDate: 2020/5/21 16:32
* @return: boolean
*/
message.trim();
if (message.length() > 140) {
return false;
}
return true;
}
public static void f(String str,String point){
/**
* @description:檢索字符串
* @param str
* @param point
* @createDate: 2020/5/21 17:45
* @return: void
*/
int index = str.indexOf(point);
if(index!=-1){
System.out.println("匹配到了Java,索引位置是:"+index);
}else{
System.out.println("沒有匹配到了Java");
}
}
public static void judgeDocument(String name){
/**
* @description:判斷文件是不是java文件
* @param name
* @createDate: 2020/5/21 18:01
* @return: void
*/
if (name.endsWith(".java")) {
System.out.println(name+" :是java文件。");
}
}
}
輸出:
string 的長度: 10
string 的長度是否在限制範圍內: true
string 的第一個字符是: 今
string 的第二個字符是: 天
我好像是全世界最帥的人程序員了
匹配到了Java,索引位置是:0
是一種廣泛使用的計算機編程語言,擁有跨平臺、面向對象、泛型編程的特性,廣泛應用於企業級Web應用開發和移動應用開發。任職於太陽微系統的詹姆斯·高斯林等人於1990年代初開發Java語言的雛形,最初被命名爲Oak,目標設置在家用電器等小型系統的編程語言,應用在電視機、電話、鬧鐘、烤麪包機等家用電器的控制和通信。由於這些智能化家電的市場需求沒有預期的高,Sun公司放棄了該項計劃。隨着1990年代互聯網的發展,Sun公司看見Oak在互聯網上應用的前景,於是改造了Oak,於1995年5月以Java的名稱正式發佈。Java伴隨着互聯網的迅猛發展而發展,逐漸成爲重要的網絡編程語言。
是一種廣泛使用的計算機編程語言
String.java :是java文件。
Python是一種廣泛使用的計算機編程語言,擁有跨平臺、面向對象、泛型編程的特性,廣泛應用於企業級Web應用開發和移動應用開發。任職於太陽微系統的詹姆斯·高斯林等人於1990年代初開發Python語言的雛形,最初被命名爲Oak,目標設置在家用電器等小型系統的編程語言,應用在電視機、電話、鬧鐘、烤麪包機等家用電器的控制和通信。由於這些智能化家電的市場需求沒有預期的高,Sun公司放棄了該項計劃。隨着1990年代互聯網的發展,Sun公司看見Oak在互聯網上應用的前景,於是改造了Oak,於1995年5月以Python的名稱正式發佈。Python伴隨着互聯網的迅猛發展而發展,逐漸成爲重要的網絡編程語言。
29
的名稱正式
Object.java
package ExercisesToPractice;
import java.io.File;
import java.time.LocalDate;//導包
import java.time.format.DateTimeFormatter;
/**
* @Author: cc雪影
* @Description:創建對象
* @Date: Create in 22:09 2020/5/21
*/
public class Object {
// 1、Java包管理器
public static void main(String[] args) {
//得到當前的時間
LocalDate now = LocalDate.now();
System.out.println(now);
//創建一個時間格式化方式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
//執行時間的格式化處理
String time = dateTimeFormatter.format(now);
System.out.println(time);
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int dayOfWeek = now.getDayOfWeek().getValue();
System.out.println("當前年:"+year+" 當前月:"+month+" 當前日:"+day);
System.out.println("當前時間所在的週數" +dayOfWeek);
//將字符串時間轉換成時間
String time1 = "2020-05-21";
LocalDate localDate1 = LocalDate.parse(time1);
System.out.println(dateTimeFormatter.format(localDate1));//格式化輸出
//其他時間日期的運算
System.out.println("加1天:" + now.plusDays(1));
System.out.println("加1周:" + now.plusWeeks(1));
System.out.println("加1月:" + now.plusMonths(1));
System.out.println("加1年:" + now.plusYears(1));
System.out.println("減法運算");
System.out.println("減1天:" + now.minusDays(1));
System.out.println("減1周:" + now.minusWeeks(1));
System.out.println("減1月:" + now.minusMonths(1));
System.out.println("減1年:" + now.minusYears(1));
//2、實例化對象
//計算mywork下得文件大小並轉換成kb
File myfile = new File("mywork");
long total = 0;
for(File file: myfile.listFiles()){
total = total+ file.length();
}
System.out.println(total/1024+"kb");
}
public static LocalDate getLeaveTime(String checkInTime, int days) {
/**
* @description:時間日期的計算
* @param checkInTime
* @param days
* @createDate: 2020/5/21 22:40
* @return: java.time.LocalDate
*/
// 把字符串轉化爲 LocalDate 類型
LocalDate time = LocalDate.parse(checkInTime);
// 使用 plusDays 添加天數,得到新的時間
LocalDate leaveTime = time.plusDays(days);
return leaveTime;
}
}
輸出:
2020-05-22
2020年05月22日
當前年:2020 當前月:5 當前日:22
當前時間所在的週數5
2020年05月21日
加1天:2020-05-23
加1周:2020-05-29
加1月:2020-06-22
加1年:2021-05-22
減法運算
減1天:2020-05-21
減1周:2020-05-15
減1月:2020-04-22
減1年:2019-05-22
0kb
成功或是平庸,只在於你是旭日裏努力還是夕陽下幻想
但我好像是熬夜肝兒