IDEA中内容辅助键和快捷键
快速生成main方法:psvm 回车;
快速生成输出语句:sout 回车;
代码补全 ctrl+alt+space;
复制一行 Ctrl+D
单行注释:ctrl +/;
多行注释:ctrl + shift + / ;
格式化代码:ctrl +alt + L ;
\n 代表的时换行,\t代表的是tab键,往后缩进
Scanner 调用方法直接写sc.nextLine(),ctrl+alt+v;
命名指导规范:
变量名称:小驼峰 helloWorld
类名:大驼峰 HelloWorld
短路逻辑运算符:
&& 左边为false 右边就不用执行,同时为真结果为真
|| 左边为true,右边就不执行
遍历数组
public class helloworld {
public static void main(String[] args) {
// 定义数组
int[] arr = {1, 22, 33, 44, 55};
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);
}
}
}
数组中取最大值
public class helloworld {
public static void main(String[] args) {
int[] arr = {1, 22, 33, 44, 55};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println(max);
}
}
带参数方法定义和调用
public class helloworld {
public static void main(String[] args) {
// 常量调用
isEvenNumber(10);
// 变量调用
int number = 10;
isEvenNumber(number);
}
// 定义一个方法,该方法接收一个参数,判断该数据是否为偶数
public static void isEvenNumber(int number){
if (number%2==0){
System.out.println(true);
}else {
System.out.println(false);
}
}
}
带返回值方法的练习,两个数中取最大的
public class helloworld {
public static void main(String[] args) {
// 在main()方法中调用定义的方法并使用变量保存
// int result = getMax(10, 20)
// System.out.println(result);
System.out.println(getMax(10,20));
}
// 定义一个方法获取两个数中较大数
public static int getMax(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
}
遍历数组
public class helloworld {
public static void main(String[] args) {
// 定义一个数组,用静态初始化完成数组元组初始化
int[] arr= {11,22,33,44,55};
// 调用方法
printArray(arr);
}
// 定义一个方法,用数组遍历通用格式对数组进行遍历。
// 明确返回类型为void,参数int[] arr
public static void printArray(int[] arr){
for (int x=0;x<arr.length;x++){
System.out.println(arr[x]);
}
}
}
输出:
11
22
33
44
55
public class helloworld {
public static void main(String[] args) {
// 定义一个数组,用静态初始化完成数组元组初始化
int[] arr= {11,22,33,44,55};
// 调用方法
printArray(arr);
}
// 定义一个方法,用数组遍历通用格式对数组进行遍历。
// 明确返回类型为void,参数int[] arr
public static void printArray(int[] arr){
System.out.print("[");
for (int x=0;x<arr.length;x++){
if(x<arr.length-1){
System.out.print(arr[x]+",");
}else {
System.out.print(arr[x]);
}
}
System.out.print("]");
}
}
输出:
[11,22,33,44,55]
求数组中的最大值,通过调用方法
public class helloworld {
public static void main(String[] args) {
int[] arr = {1, 22, 7, 4, 6};
System.out.println(getMax(arr));
}
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
键盘输入版获取两个数中最大值
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in);
// 接收数据
System.out.println("请输入第一个数:");
int a = sc.nextInt();
System.out.println("请输入第二个数:");
int b = sc.nextInt();
// 调用方法
int max = getMax(a, b);
// 输出最大值
System.out.println("较大值是:" + max);
}
// 创建一个方法获取两个数的最大值
public static int getMax(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
}
百钱买百鸡
public class helloworld {
public static void main(String[] args) {
for (int x = 0; x <= 20; x++) {
for (int y = 0; y <= 33; y++) {
int z = 100 - x - y;
if (z % 3 == 0 && 5 * x + 3 * y + z / 3 == 100) {
System.out.println(x + "," + y + "," + z);
}
}
}
}
}
求满足条件的数组的和
public class helloworld {
public static void main(String[] args) {
int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};
int sum = 0;
// 遍历数组,获取到数组中的每一个元素
for (int x = 0; x < arr.length; x++) {
// 判断元素是否满足条件,满足条件就累加
// 求和的元素各位和十位都不能是7,并且只能是偶数
if (arr[x] % 10 != 7 && arr[x]/10 % 10!=7 && arr[x] % 2 == 0){
sum += arr[x];
}
}
// 输出求和变量
System.out.println("sum:"+sum);
}
}
判断两个数组,相同返回true,不同返回flase
public class helloworld {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 2, 3, 4, 5};
// 调用方法,用变量接收
boolean flag =compare(arr1,arr2);
// 输出结果
System.out.println(flag);
}
// 明确返回值类型:boolean;返回参数:int[] arr1,int[] arr2
public static boolean compare(int[] arr1, int[] arr2) {
// 首先比较数组长度,长度不相同,返回flase
if (arr1.length != arr2.length) {
return false;
}
// 其次遍历,比较两个数组中每个元素,不相同,返回flase
for (int x = 0; x < arr1.length; x++) {
if (arr1[x] != arr2[x]) {
return false;
}
}
return true;
}
}
数组反转
public class helloworld {
public static void main(String[] args) {
int[] arr = {19, 28, 37, 46, 50,1};
/*// 循环遍历数组,初始化语句定义两个索引变量,判断条件是开始索引小于结束索引
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}*/
reverse(arr);
printArray(arr);
}
// 遍历数组,明确返回类型void,参数int[] arr
public static void printArray(int[] arr) {
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);
}
}
public static void reverse(int[] arr){
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
}
评委打分,去掉一个最高分,去掉一个最低分,求平均分
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) {
int[] arr = new int[6];
Scanner sc = new Scanner(System.in);
// 键盘录入评委的打分
for (int x = 0; x < arr.length; x++) {
System.out.println("请输入第" + (x + 1) + "个评委的打分");
arr[x] = sc.nextInt();
}
// printArray(arr);
int max = getMax(arr);
int min = getMin(arr);
int sum = getSum(arr);
// 按照规则进行计算得到平均分
int avg = (sum - max - min) / (arr.length - 2);
System.out.println(avg);
}
// 遍历数组
public static void printArray(int[] arr) {
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);
}
}
// 定义一个方法获取数组中的最大值
// 返回值类型int,参数int[] arr
public static int getMax(int[] arr) {
int max = arr[0];
for (int x = 1; x < arr.length; x++) {
if (arr[x] > max) {
max = arr[x];
}
}
return max;
}
// 定义方法,获取数组中的最小值
public static int getMin(int[] arr) {
int min = arr[0];
for (int x = 1; x < arr.length; x++) {
if (arr[x] < min) {
min = arr[x];
}
}
return min;
}
// 定义求和的方法
public static int getSum(int[] arr) {
int sum = 0;
for (int x = 0; x < arr.length; x++) {
sum += arr[x];
}
return sum;
}
}
查找数组中的元素,并输出正确的索引
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) {
int[] arr = {19, 28, 3, 6, 5};
// 键盘录入需要查找的数据,用一个变量接收
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要查找的数据:");
int number = sc.nextInt();
// 定义一个索引变量初始值为-1
int index = -1;
// 遍历数组,获取到数组中的每一个元素
for (int x = 0; x < arr.length; x++) {
if (arr[x] == number) {
index = x;
break;
}
}
// 输出索引
System.out.println("index:" + index);
}
}
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) {
int[] arr = {19, 28, 3, 6, 5};
// 键盘录入需要查找的数据,用一个变量接收
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要查找的数据:");
int number = sc.nextInt();
int index = getIndex(arr, number);
// 输出索引
System.out.println("index:" + index);
}
public static int getIndex(int[] arr, int number) {
int index = -1;
for (int x = 0; x < arr.length; x++) {
if (arr[x] == number) {
index = x;
break;
}
}
return index;
}
}
对象的使用,在同一个包下,新建一个文件phone,和一个文件phoneDemo
public class phone {
String brand;
int price;
public void call() {
System.out.println("打电话");
}
public void sendMessage() {
System.out.println("发短信");
}
}
public class phoneDemo {
public static void main(String[] args) {
phone p = new phone();
System.out.println(p.brand);
System.out.println(p.price);
p.brand = "小米";
p.price = 1999;
System.out.println(p.brand);
System.out.println(p.price);
p.call();
}
}
private的使用,创建一个student类,创建一个studentdemo类
public class student {
// 成员变量
String name;
private int age;
// 提供get和set方法
public void setAge(int a) {
if (a < 0 || a > 120) {
System.out.println("你输入的年龄有误");
} else {
age = a;
}
}
public int getAge() {
return age;
}
// 成员方法
public void show() {
System.out.println(name + "," + age);
}
}
public class studentDemo {
public static void main(String[] args) {
// 创建对象
student s = new student();
s.name = "xiaoming";
s.setAge(-8);
s.show();
}
}
无参和有参构造方法
public class Student {
// 成员变量
private String name;
private int age;
// 构造方法
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 成员方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void show() {
System.out.println(name + "," + age);
}
}
public class StudentDemo {
public static void main(String[] args) {
// 无参方法创建对象后使用setxxx赋值
Student s1 = new Student();
s1.setName("xiaoming1");
s1.setAge(30);
s1.show();
// 使用带参数的方法直接创建带有属性的对象值
Student s2 = new Student("xiaoming2", 30);
s2.show();
}
}
String的构造方法
public class StringDemo {
public static void main(String[] args) {
// 创建一个空白字符串
String s1 = new String();
System.out.println("s1"+s1);
// 根据字符数组来创建字符串对象
char[] chs = {'a','b','c'};
String s2 = new String(chs);
System.out.println("s2"+s2);
// 根据字节内容
byte[] bys ={97,98,99};
String s3 = new String(bys);
System.out.println("s3"+s3);
// 直接复制的方式创建字符串对象
String s4 ="abc";
System.out.println("s4"+s4);
}
}
登录,判断用户名和密码是否相同,每个账户有三次登录机会
public class StringDemo {
public static void main(String[] args) {
String username = "wangrui";
String password = "mima123";
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入name");
String name = sc.nextLine();
System.out.println("请输入密码");
String pwd = sc.nextLine();
// 拿登陆的用户名和密码和已知的用户名和密码比较,给出相应的提示
if (name.equals(username) && password.equals(pwd)) {
System.out.println("sign in success");
break;
} else {
if (2 - i == 0) {
System.out.println("你的账户被锁定");
} else {
System.out.println("fail,还剩余" + (2 - i));
}
}
}
遍历字符串
public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
遍历字符串,能够获取到字符串中的每一个字符
// System.out.println(line.charAt(0));
// System.out.println(line.charAt(1));
// System.out.println(line.charAt(2));
for (int i = 0; i < line.length(); i++) {
System.out.println(line.charAt(i));
}
}
}
统计每种类型的字符出现的次数
public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
// System.out.println(line);
int countupper = 0;
int countlower = 0;
int countnumber = 0;
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
countupper += 1;
} else if (ch >= 'a' && ch <= 'z') {
countlower++;
} else if (ch >= '0' && ch <= '9') {
countnumber++;
}
}
System.out.println("大写字母出现" + countupper + "次");
System.out.println("小写字母出现" + countlower + "次");
System.out.println("数字出现" + countnumber + "次");
}
}
字符串反转
public class StringDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
// 调用方法用一个变量接收
String s = arraytostring(arr);
System.out.println("s" + s);
}
// 明确返回值为string,参数类型为 int[] arr
public static String arraytostring(int[] arr) {
String s = "";
s += "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
s += arr[i];
} else {
s += arr[i];
s += ",";
}
}
s += "]";
return s;
}
}
字符串反转
public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
// 调用一个方法,用一个变量接收
String s = reverse(line);
System.out.println("s:" + s);
}
public static String reverse(String s) {
String ss = "";
for (int i = s.length() - 1; i >= 0; i--) {
ss += s.charAt(i);
}
return ss;
}
}
public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
System.out.println(myreverse(line));
}
public static String myreverse(String s) {
// 在方法中用StringBuilder实现字符串反转,并把结果转成String返回
// String--StringBuilder--reverse--String
StringBuilder sb = new StringBuilder(s);
sb.reverse();
String ss = sb.toString();
return ss;
}
}
string和stringBulider的相互转换
public class StringDemo {
public static void main(String[] args) {
StringBuilder转换为String
// StringBuilder sb = new StringBuilder();
// sb.append("hello");
//
// String s = sb.toString();
// System.out.println(s);
// String转换为StringBuilder
String s = "hello";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb);
}
}
字符串拼接
public class StringDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String s = arraytoString(arr);
System.out.println(s);
}
public static String arraytoString(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(",");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
}