java学习笔记10
知识点
1.获取字符串长度
字符串名.length();
2.自定义快捷输入
在IDEA中,通过File → Settings → Editor → Live Templates
例如:
只要输入buff ,就会直接输入BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
只要输入ssrl ,就会输入String s$END$ = reader.readLine(); ,并且输入光标停留在$END$处。
3.类创建的流程
类基本上是按以下流程创建的:
-
程序员确定需要哪些其他对象。
-
程序员根据对象要执行的操作将其分为不同的类型。
-
程序员为每种类型编写单独的类。
-
在类中,程序员声明所需的方法和变量。
-
在每种方法中,程序员编写命令以使该方法执行所需的操作。
-
类已完成。现在你可以创建该类的对象。
任务
课程1 比较和设置条件
任务1 两个数字中的最小值
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.