Java语言入门,你只需要看懂这几行代码
“忙”是至于一切矫情的良药
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
成功或是平庸,只在于你是旭日里努力还是夕阳下幻想
但我好像是熬夜肝儿