- java学习笔记10
- 知识点
- 1.获取字符串长度
- 2.自定义快捷输入
- 3.类创建的流程
- 任务
- 课程1 比较和设置条件
- 任务1 两个数字中的最小值
- 任务2 四个数字中的最大值
- 任务3 对三个数字进行排序
- 任务4 珍或珍?
- 任务5 18 岁以上
- 任务6 保安政策
- 任务7 三个数字
- 任务8 目标已锁定!
- 课程2 boolean 类型
- 任务1 标签和数字
- 任务2 描述数字
- 任务3 正数
- 任务4 正数和负数
- 课程3 while循环
- 任务1 1 到 10
- 任务2 从 10 到 1
- 任务3 好东西也不能索取无度
- 任务4 看看你将在未来拥有的美元
- 任务5 乘法表
- 课程4 for循环
- 任务1 偶数
- 任务2 画矩形
- 任务3 由 8 组成的三角形
- 任务4 画线
- 任务5 连锁信
- 课程5 练习课
- 任务1 可观的收入
- 任务2 设法达到均衡
- 任务3 相加
- 任务4 相加
- 课程6 练习创建对象
- 任务1 创建猫
- 任务2 实现 fight 方法
字符串名.length();
2.自定义快捷输入在IDEA中,通过File → Settings → Editor → Live Templates
例如:
只要输入buff ,就会直接输入BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
只要输入ssrl ,就会输入String s$END$ = reader.readLine(); ,并且输入光标停留在$END$处。
类基本上是按以下流程创建的:
-
程序员确定需要哪些其他对象。
-
程序员根据对象要执行的操作将其分为不同的类型。
-
程序员为每种类型编写单独的类。
-
在类中,程序员声明所需的方法和变量。
-
在每种方法中,程序员编写命令以使该方法执行所需的操作。
-
类已完成。现在你可以创建该类的对象。
package zh.codegym.task.task04.task0418;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
if(a < b){
System.out.println(a);
}else{
System.out.print(b);
}
//在此编写你的代码
}
}
任务2 四个数字中的最大值
package zh.codegym.task.task04.task0419;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
String sD = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
int d = Integer.parseInt(sD);
int max = 0 ;
if (a > b) {
max = a;
} else {
max = b;
}
if (c > max) {
max = c;
}
if (d > max) {
max = d;
}
System.out.println(max);
//在此编写你的代码
}
}
任务3 对三个数字进行排序
使用键盘输入三个数字,然后按降序显示它们。
所显示的数字必须用空格分隔。
package zh.codegym.task.task04.task0420;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
int tmp = 0;
if (a > b) {
if (c > a) {
tmp = a;
a = c;
c = tmp;
tmp = b;
b = c;
c = tmp;
} else if (c > b) {
tmp = b;
b = c;
c = tmp;
}
} else {
tmp = b;
b = a;
a = tmp;
if (c > a) {
tmp = a;
a = c;
c = tmp;
tmp = b;
b = c;
c = tmp;
} else if (c > b) {
tmp = b;
b = c;
c = tmp;
}
}
System.out.println(a + " " + b + " " + c);
}
}
任务4 珍或珍?
使用键盘输入两个名字。如果这两个名字相同,则显示“名字相同”。
如果这两个名字不同,但它们的长度相同,则显示“名字长度相同”。
如果这两个名字和名字长度都不相同,则不显示任何内容。
package zh.codegym.task.task04.task0421;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sName1 = reader.readLine();
String sName2 = reader.readLine();
if (sName1.equals(sName2)) {
System.out.println("名字相同");
} else if (sName1.length() == sName2.length()) {
System.out.println("名字长度相同");
}
//在此编写你的代码
}
}
任务5 18 岁以上
使用键盘输入名字和年龄。如果年龄小于 18,则显示“再长大一点”。
package zh.codegym.task.task04.task0422;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String sAge = reader.readLine();
int age = Integer.parseInt(sAge);
if (age < 18) {
System.out.println("再长大一点");
}
//在此编写你的代码
}
}
任务6 保安政策
package zh.codegym.task.task04.task0423;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String sAge = reader.readLine();
int age = Integer.parseInt(sAge);
if (age > 20) {
System.out.println("18 岁就够大了");
//在此编写你的代码
}
}
}
任务7 三个数字
使用键盘输入三个整数。其中一个数字是唯一的。另外两个数字相同。显示不同于其他两个的数字的序号。
package zh.codegym.task.task04.task0424;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
if (a == b && b != c) {
System.out.println(3);
} else if (a == c && b != c) {
System.out.println(2);
} else if (b == c && a != b) {
System.out.println(1);
}
//在此编写你的代码
}
}
任务8 目标已锁定!
使用键盘输入两个整数,以表示不在坐标轴上的点的坐标。
显示包含给定点的象限的编号。
package zh.codegym.task.task04.task0425;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sX = reader.readLine();
String sY = reader.readLine();
int x = Integer.parseInt(sX);
int y = Integer.parseInt(sY);
if (x > 0 && y > 0) {
System.out.println(1);
} else if (x < 0 && y > 0) {
System.out.println(2);
} else if (x < 0 && y < 0) {
System.out.println(3);
} else if (x > 0 && y < 0){
System.out.println(4);
}
//在此编写你的代码
}
}
课程2 boolean 类型
任务1 标签和数字
使用键盘输入一个整数。按如下所示显示字符串描述:
“负偶数”- 如果数字同时为负数和偶数,
“负奇数”- 如果数字同时为负数和奇数,
“零”- 如果数字为 0,
“正偶数”- 如果数字同时为正数和偶数,
“正奇数”- 如果数字同时为正数和奇数。
package zh.codegym.task.task04.task0426;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
int a = Integer.parseInt(sA);
if (a == 0) {
System.out.println("零");
} else if (a > 0 && a % 2 == 0) {
System.out.println("正偶数");
} else if (a > 0 && a % 2 == 1) {
System.out.println("正奇数");
} else if (a < 0 && a % 2 == 0) {
System.out.println("负偶数");
} else {
System.out.println("负奇数");
}
//在此编写你的代码
}
}
任务2 描述数字
从键盘输入 1 - 999 之间的整数。按如下所示显示字符串描述:
“一位偶数”- 如果数字为偶数且包含一位数字,
“一位奇数”- 如果数字为奇数且包含一位数字,
“两位偶数”- 如果数字为偶数且包含两位数字,
“两位奇数”- 如果数字为奇数且包含两位数字,
“三位偶数”- 如果数字为偶数且包含三位数字,
“三位奇数”- 如果数字为奇数且包含三位数字。
如果输入的数字不在 1 - 999 之间,则不显示任何内容。
package zh.codegym.task.task04.task0427;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
int num = Integer.parseInt(sNum);
if ((num > 0 && num < 10) && num % 2 == 0) {
System.out.println("一位偶数");
} else if ((num > 0 && num < 10) && num % 2 == 1) {
System.out.println("一位奇数");
} else if ((num >= 10 && num < 100) && num % 2 == 0) {
System.out.println("两位偶数");
} else if ((num >= 10 && num < 100) && num % 2 == 1) {
System.out.println("两位奇数");
} else if ((num >= 100 && num < 1000) && num % 2 == 0) {
System.out.println("三位偶数");
} else if ((num >= 100 && num < 1000) && num % 2 == 1) {
System.out.println("三位奇数");
}
}
}
任务3 正数
使用键盘输入三个整数。显示原始集中正数的个数。
package zh.codegym.task.task04.task0428;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
if (a > 0 && (b > 0 && c > 0)) {
System.out.println(3);
} else if ((a > 0 && (b > 0 || c > 0)) || (b > 0 && c > 0)) {
System.out.println(2);
} else if ((a > 0 || b > 0) || c > 0) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
任务4 正数和负数
使用键盘输入三个整数。显示原始集中正数和负数的个数,格式如下:
“负数个数:a”和“正数个数:b”,其中 a 和 b 是未知的。
package zh.codegym.task.task04.task0429;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
int pos = 0;
int neg = 0;
if (a > 0) {
pos++;
} else if (a < 0) {
neg++;
}
if (b > 0) {
pos++;
} else if (b < 0) {
neg++;
}
if (c > 0) {
pos++;
} else if (c < 0) {
neg++;
}
System.out.println("负数个数:" + neg);
System.out.println("正数个数:" + pos);
}
}
课程3 while循环
任务1 1 到 10
使用 while 循环从 1 到 10 显示数字。每行显示一个值。
package zh.codegym.task.task04.task0430;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
int i =1;
while(i <= 10){
System.out.println(i++);
}
//在此编写你的代码
}
}
任务2 从 10 到 1
使用 while 循环从 10 到 1 显示数字。每行显示一个值。
package zh.codegym.task.task04.task0431;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
int i = 10;
while (i > 0) {
System.out.println(i--);
}
}
}
任务3 好东西也不能索取无度
使用键盘输入字符串和大于 0 的数字 N。
使用 while 循环将字符串显示 N 次。每行显示一个值。
package zh.codegym.task.task04.task0432;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
String sNum = reader.readLine();
int num = Integer.parseInt(sNum);
int i = 1;
while (i <= num) {
System.out.println(s);
i++;
}
}
}
任务4 看看你将在未来拥有的美元
使用 while 循环显示美元符号的 10x10 平方。
在每行中不要将符号分开。
package zh.codegym.task.task04.task0433;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
int i = 1;
while (i <= 10) {
System.out.println("$$$$$$$$$$");
i++;
}
}
}
任务5 乘法表
使用 while 循环显示 10x10 乘法表。
使用空格将数字分隔。
package zh.codegym.task.task04.task0434;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
int i = 1;
while (i <= 10) {
int j = 1;
while (j <= 10) {
System.out.print(i * j + " ");
j++;
}
System.out.println();
i++;
}
}
}
课程4 for循环
任务1 偶数
使用 for 循环显示 1 到 100(含 1 和 100)之间的偶数。
每行显示一个值。
package zh.codegym.task.task04.task0435;
public class Solution {
public static void main(String[] args) throws Exception {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
任务2 画矩形
使用键盘输入两个数字 m 和 n。
使用 for 循环显示一个由 8 组成的 n x m 矩形。
package zh.codegym.task.task04.task0436;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
System.out.print(8);
}
System.out.println();
}
//在此编写你的代码
}
}
任务3 由 8 组成的三角形
使用 for 循环显示由 8 组成的直角三角形,底边为 10,高为 10。
package zh.codegym.task.task04.task0437;
public class Solution {
public static void main(String[] args) throws Exception {
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < i; j++) {
System.out.print(8);
}
System.out.println();
}
//在此编写你的代码
}
}
任务4 画线
使用 for 循环显示:
- 由 10 个 8 组成的水平线
- 由 10 个 8 组成的垂直线(在此垂直线中,不将水平线上的任何一个 8 计算在内)。
package zh.codegym.task.task04.task0438;
public class Solution {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
System.out.print(8);
}
for (int i = 0; i < 10 ; i++) {
System.out.println(8);
}
}
}
任务5 连锁信
从键盘输入一个名字,并使用 for 循环显示以下短语 10 次:<名字>爱我。
package zh.codegym.task.task04.task0439;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sName = reader.readLine();
for (int i = 0; i < 10; i++) {
System.out.println(sName + "爱我。");
}
}
}
课程5 练习课
任务1 可观的收入
使用循环将以下短语显示一百次:
“我永远不会为了一点微薄的薪水而工作。阿米戈”
每行显示一个值。
package zh.codegym.task.task04.task0440;
public class Solution {
public static void main(String[] args) {
for (int i = 0; i < 100 ; i++) {
System.out.println("我永远不会为了一点微薄的薪水而工作。阿米戈");
}
//在此编写你的代码
}
}
任务2 设法达到均衡
使用键盘输入三个数字,然后显示中间数。
换言之,不是最大数,也不是最小数。
如果所有数字都相等,则显示其中任一数字。
package zh.codegym.task.task04.task0441;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
if ((a >= b && b >= c) || (c >= b && b >= a)) {
System.out.println(b);
} else if ((b >= a && a >= c) || (c >= a && a >= b)) {
System.out.println(a);
} else {
System.out.println(c);
}
}
}
任务3 相加
使用键盘输入数字。
如果用户输入 -1,则显示所有输入数字的总和并结束程序。
-1 应计入总和中。
package zh.codegym.task.task04.task0442;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int sum = 0;
while (true) {
String sNum = reader.readLine();
int num = Integer.parseInt(sNum);
sum += num;
if (num == -1) {
System.out.println(sum);
break;
}
}
//在此编写你的代码
}
}
任务4 相加
名字就是名字
使用键盘输入名字。
使用键盘输入出生日期(三个数字):年、月、日。
显示以下内容:
“我的名字叫名字。
我出生于月/日/年”
示例输出:
我的名字叫凯文。
我出生于 2/15/1988
package zh.codegym.task.task04.task0443;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sName = reader.readLine();
String sYear = reader.readLine();
String sMonth = reader.readLine();
String sDay = reader.readLine();
System.out.println("我的名字叫" + sName + "。");
System.out.println("我出生于 " + sMonth + "/" + sDay + "/" + sYear);
}
}
课程6 练习创建对象
任务1 创建猫
创建 Cat 类。Cat 类必须包含 String 变量 name、int 变量 age、int 变量 weight 以及 int 变量 strength。
package zh.codegym.task.task05.task0501;
public class Cat {
String name;
int age;
int weight;
int strength;
public static void main(String[] args) {
}
}
任务2 实现 fight 方法
实现 boolean fight(Cat anotherCat) 方法:
基于猫的体重、年龄和力量实现一种猫咪作战机制。
你可以自行决定猫的性格如何影响战斗。
方法应确定我们 (this) 是否赢得战斗,即,如果我们赢了,则返回 true,否则将返回 false。
package zh.codegym.task.task05.task0502;
public class Cat {
public int age;
public int weight;
public int strength;
public Cat() {
}
public boolean fight(Cat anotherCat) {
if ((this.strength + this.age + this.weight) > (anotherCat.strength + anotherCat.weight + anotherCat.age)) {
return true;
} else {
return false;
}
//在此编写你的代码
}
public static void main(String[] args) {
}
}



