Day08
package com.qf.work;
public class Work01 {
/**
* 1.定义一个函数,获取某个数组中的最小值
*/
public static void main(String[] args) {
int[] arr = {4,6,5,1,8,9,7,3};
int min = getMin(arr);
System.out.println("最小值为:" + min);
}
public static int getMin(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
int element = arr[i];
if(min > element){
min = element;
}
}
return min;
}
}
package com.qf.work;
public class Work02 {
/**
* 2.定义一个数组,数组成员10个,找出数组中最大数连同下标一起输出
*/
public static void main(String[] args) {
int[] arr = {4,6,5,10,1,8,9,7,3,2};
int max = arr[0];
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
int element = arr[i];
if(max < element){
max = element;
maxIndex = i;
}
}
System.out.println("最大值为:" + max);
System.out.println("最大值的下标为:" + maxIndex);
}
}
package com.qf.work;
import java.util.Arrays;
public class Work03 {
/**
* 3.B哥去参加青年歌手大奖赛,有10个评委打分,(去掉一个最高一个最低)求平均分?
*/
public static void main(String[] args) {
int[] arr = {4,6,5,10,1,8,9,7,3,2};
Arrays.sort(arr);
int sum = 0;
for (int i = 1; i < arr.length-1; i++) {
sum += arr[i];
}
double avg = sum/8.0;
System.out.println("平均分为:" + avg);
}
}
package com.qf.work;
public class Work04 {
/**
* 4.定义数组,存放5个学生的成绩【成绩值自己设定】,将成绩从大到小排序,获得成绩之和,平均成绩,最小成绩,最大成绩。
*/
public static void main(String[] args) {
int[] arr = {8,9,7,3,2};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j]<arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
int max = arr[0];
int min = arr[arr.length-1];
int sum = 0;
for (int element : arr) {
sum += element;
}
double avg = sum/5.0;
System.out.println("成绩之和:" + sum);
System.out.println("平均分为:" + avg);
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
}
}
package com.qf.work;
import java.util.Arrays;
public class Work05 {
/**
* 5.提取一个方法,将指定数组中的数组元素进行反转
* 例如:{10,23,2,45,6}--->{6,45,2,23,10}
*
* 解题思路:将原数组直接反转
*/
public static void main(String[] args) {
int[] arr = {10,23,2,45,6};
reverse(arr);
System.out.println(Arrays.toString(arr));
}
public static void reverse(int[] arr){
int start = 0;
int end = arr.length-1;
while(start < end){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
package com.qf.work;
import java.util.Arrays;
public class Work06 {
/**
* 5.提取一个方法,将指定数组中的数组元素进行反转
* 例如:{10,23,2,45,6}--->{6,45,2,23,10}
*
* 解题思路:新建一个数组反转
*/
public static void main(String[] args) {
int[] arr = {10,23,2,45,6};
int[] reverse = reverse(arr);
System.out.println(Arrays.toString(reverse));
}
public static int[] reverse(int[] arr){
int[] newArr = new int[arr.length];
int index = newArr.length-1;
for (int element : arr) {
newArr[index--] = element;
}
return newArr;
}
}
package com.qf.work;
import java.util.Arrays;
public class Work07 {
/**
* 6.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组
*/
public static void main(String[] args) {
int[] arr = {2,10,23,45,6};
//找到最大值及下标
int max = arr[0];
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
if(max < arr[i]){
max = arr[i];
maxIndex = i;
}
}
//最大的与第一个元素交换
arr[maxIndex] = arr[0];
arr[0] = max;
//找到最大值及下标
int min = arr[0];
int minIndex = 0;
for (int i = 1; i < arr.length; i++) {
if(min > arr[i]){
min = arr[i];
minIndex = i;
}
}
//最小的与最后一个元素交换
arr[minIndex] = arr[arr.length-1];
arr[arr.length-1] = min;
System.out.println(Arrays.toString(arr));//45,10,23,6,2
}
}
package com.qf.test;
import java.util.Scanner;
public class Test {
/**
* 知识点:五子棋
*/
public static void main(String[] args) {
//棋盘长度
int length = 20;
//棋盘容器
String[][] gobang = new String[length][length];
//棋盘坐标
String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
//棋盘符号
String add = "┼";
String black = "●";
String white = "○";
//初始化棋盘
for (int i = 0; i < gobang.length; i++) {
for (int j = 0; j < gobang[i].length; j++) {
if(j == length-1){//每行的一列,设置行数
gobang[i][j] = nums[i];
}else if(i == length-1){//最后一行,设置列数
gobang[i][j] = nums[j];
}else{
gobang[i][j] = add;
}
}
}
//打印棋盘
for (String[] strings : gobang) {
for (String str : strings) {
System.out.print(str);
}
System.out.println();
}
Scanner scan = new Scanner(System.in);
boolean flag = true;//true-黑子 false-白子
while(true){
//输入坐标
System.out.println("请" + ((flag)?"黑":"白") + "子输入坐标:");
int x = scan.nextInt() - 1;//-1是因为用户看到的界面是坐标(从1开始),数组是下标(从0开始)
int y = scan.nextInt() - 1;//-1是因为用户看到的界面是坐标(从1开始),数组是下标(从0开始)
//判断下标是否在棋盘范围内
if(x<0 || x>length-2 || y<0 || y>length-2){
System.out.println("坐标错误 - 坐标超出棋盘范围,请重新输入");
continue;
}
//判断下标上是否有棋子
if(!gobang[x][y].equals(add)){
System.out.println("坐标错误 - 坐标上已有棋子,请重新输入");
continue;
}
//落子
String piece = (flag)?black:white;
gobang[x][y] = piece;
//打印棋盘
for (String[] strings : gobang) {
for (String str : strings) {
System.out.print(str);
}
System.out.println();
}
//判断输赢
boolean leftAndRight = leftAndRight(x, y, piece, gobang, length);
if(leftAndRight){
System.out.println(((flag)?"黑":"白") + "子赢");
break;
}
//置反
flag = !flag;
}
}
public static boolean leftAndRight(int x,int y,String piece,String[][] gobang,int length){
int count = 1;
//计算左边连续棋子
int index = y;
while(index > 0){
index--;
if(gobang[x][index].equals(piece)){
count++;
}else{
break;
}
}
//计算右边连续棋子
index = y;
while(index < length-2){
index++;
if(gobang[x][index].equals(piece)){
count++;
}else{
break;
}
}
System.out.println(count);
if(count >= 5){
return true;
}
return false;
}
}
package com.qf.test01;
public class Test01 {
/**
* 知识点:初识面向对象
*
* 需求:创建人类的对象
* 分析:
* 1.创建人类(属性:name、sex、age,方法:eat、sleep、method)
* 2.创建对象
* 3.操作对象(设置属性、获取属性、调用方法)
*/
public static void main(String[] args) {
//创建对象
Person p = new Person();
//设置属性
p.name = "侯小康";
p.sex = '男';
p.age = 23;
//获取属性
System.out.println(p.name);
System.out.println(p.sex);
System.out.println(p.age);
//调用成员方法
p.eat();
p.sleep();
//调用静态方法
Person.method();
}
}
package com.qf.test01;
public class Person {
String name;
char sex;
int age;
public void eat(){
System.out.println("吃饭饭");
}
public void sleep() {
System.out.println("睡觉觉");
}
public static void method() {
System.out.println("静态方法");
}
}
package com.qf.test02;
public class Test01 {
/**
* 知识点:初识面向对象
*
* 需求:创建人类的多个对象
*/
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "侯小康";
p1.sex = '男';
p1.age = 23;
System.out.println(p1.name);
System.out.println(p1.sex);
System.out.println(p1.age);
p1.eat();
p1.sleep();
System.out.println("-----------------------------");
Person p2 = new Person();
p2.name = "波多野结衣";
p2.sex = '女';
p2.age = 18;
System.out.println(p2.name);
System.out.println(p2.sex);
System.out.println(p2.age);
p2.eat();
p2.sleep();
}
}
package com.qf.test02;
public class Person {
String name;
char sex;
int age;
public void eat(){
System.out.println(this.name + "吃饭饭");
}
public void sleep(){
System.out.println(this.name + "睡觉觉");
}
}
1.五子棋
2.初识面向对象
作业:
1.完全掌握五子棋
2.复习:之前所有的内容
3.预习:面向对象 – 构造方法、this、private、封装、static、继承、super、重写、访问修饰符、抽象类及抽象方法、接口、多态、对象转型、内部类