什么是数组?

数组的定义:是用统一的名字代表这批数据,用序号来区分各个数据。数组是无序的数据元素按有序的下标组成的集合,分配固定空间大小的一种容器。

如何理解:其实就是一个同时放很多数据的变量。

a=1;

a=2;

a=3;

这成了反复赋值,最后a=3;

a怎么能同时放下1,2,3......?

如 int a0;int a1; int a2; 同时具备3个变量才能存储。

一定让a存下3个或更多数据呢?

如果是同样的数据类型,这时用一个变量名 a 来放这些数据,这就是数组。

如下:

a0=1;

a1=2;

a2=3;

a3=4;

a4=5;

a5=6;

这样变量名字还是变了,我们用 a[下标] 来表示数组中的某个元素

归纳一下:如何赋值一个数组呢? 确定数组的数据类型 int a[20] 下标从0开始。//for(int i=0;// orc guai[100];

a[0]=1;

a[1]=2;

a[2]=3;

.........a[19];

存储固定大小同类元素的容器。

两种定义方法:

一种:

dataType[] arrayName = new dataType[arraySize];

int [] arr=new int[10];

arr[0]=22;

arr[1]=18;

........

arr[9]=321;

另一种:

dataType[] arrayName = {value0, value1, ..., valuek};

int [] arr={238,23,21,34,87,2,3,19,198,28};


使用一位数组解决 1 1 2 3 5 8 13 数列问题 斐波纳契数列 Fibonacci

//一维数组 输出30个Fibonacci数列
#include <iostream>
using namespace std; int main(){
//定义一个一维数组
int arr[]={,};
//造fabonacci数组
for(int i=;i<;i++) {
arr[i]=arr[i-]+arr[i-];
}
//遍历一下
for(int i=;i<;i++){
cout<<arr[i]<<endl;
} return ;
}

分析?

1 1 2 3 5 8 13 这个数列的下一个是什么?得到什么规律?如何用一维数组实现?

把C++的程序编程Java的。


一维数组的排序,把数组中的元素按照从小到大的顺序输出?

两种方法选择法与冒泡法

选择法的思路?

思路:用一个数组元素和所有其他的所有元素PK,谁小谁放第一个,确定一个位置再确定下一个位置,一次类推

把下边c++的代码换成Java的

#include <iostream>
using namespace std; int main(){
//声明一维数组?整型的一维数组,就是数组中所有的元素都是int
int arr[]={,,,,,,,,,}; for(int i=;i<;i++){
cout<<"一维数组的第 "<<i<<" 个值是 "<<arr[i]<<endl;
} //请你给刚才的数组的进行排序,要求从小到大输出 (选择法)
// for(int i=0;i<10-1;i++){
// for(int j=i+1;i<10;j++){
// if(array[i]>array[j]){
// int temp;
// temp=array[i];
// array[i]=array[j];
// array[j]=temp;
// }
// }
// }
for(int i=;i<-;i++) {
for(int j=i+;j<;j++) {
if(arr[i]>arr[j]) {
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//排序后再遍历字符一下数组
for(int i=;i<;i++){
cout<<"遍历后一维数组的第 "<<i<<" 个值是 "<<arr[i]<<endl;
}
return ;
}

选择法的思路?

思路:

数组中相邻的两个元素两两比较,逐步把小的放在前面,一轮过后最大的数值如石头一样沉入水底,而相对较小的数值如气泡逐渐在水中上浮,经过n轮的两两比较冒泡排序

把下边c++的代码换成Java的

#include <iostream>
using namespace std; int main(){
//声明一维数组?整型的一维数组,就是数组中所有的元素都是int
int arr[]={,,,,,,,,,}; for(int i=;i<;i++){
cout<<"一维数组的第 "<<i<<" 个值是 "<<arr[i]<<endl;
} for(int i=;i<;i++) {
for(int j=;j<--i;j++) {
if(arr[j]>arr[j+]) {
int temp;
temp=arr[j];
arr[j]=arr[j+];
arr[j+]=temp;
}
}
}
//排序后再遍历字符一下数组
for(int i=;i<;i++){
cout<<"遍历后一维数组的第 "<<i<<" 个值是 "<<arr[i]<<endl;
}
return ;
}

什么是对象数组?

人类对象数组——一堆人,实例如下:

package com.swift;

public class PersonArray
{
public static void main(String args[])
{
person per[]=new person[3];
per[0]=new person("张三",20);
per[1]=new person("李四",22);
per[2]=new person("王五",23);
for (int x=0;x<per.length;x++)
{
per[x].getinfo();
}
}
}
class person
{
private String name;
private int age;
public person(String name,int age)
{
this.name=name;
this.age=age;
}
public void getinfo()
{
System.out.println("姓名: "+this.name+" 年龄: "+this.age);
}
}

最后使用一维数组来完成吃金币游戏中,成堆的金币,很多炸弹的布置。

实现游戏中生成8个金币精灵和3个炸弹精灵,每个精灵有自己的精灵序号,和在游戏窗口中的横纵坐标

游戏代码如下:

package com.swift;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent; import com.rupeng.game.GameCore;
/**
* @author swift
* @version 2.0
* @category 新增炸弹功能,精灵如果吃到炸弹立即死亡,游戏结束;
* @category 新增游戏倒计时功能,倒计时60秒
* @category 新增游戏胜利失败功能,吃完金币则胜利,吃到炸弹失败
*/
public class Coin2 implements Runnable { public static void main(String[] args) {
GameCore.start(new Coin2());
} @Override
public void run() {
//设置窗体大小、标题、背景
GameCore.setGameSize(800, 345);
GameCore.setGameTitle("用键盘操控精灵移动的小游戏");
GameCore.loadBgView("bg.jpg");
// 设置女精灵出现及出场位置
int spriteGirl = 0;
GameCore.createSprite(spriteGirl, "guizi");
GameCore.playSpriteAnimate(spriteGirl, "run", true);
GameCore.setSpritePosition(spriteGirl, 140, 190);
// 使用数组放置8个金币的数量、横坐标、纵坐标
int[] coinNum = { 1, 2, 3, 4, 5, 6, 7, 8 };
int[] coinXDate = { 100, 200, 300, 400, 500, 600, 700, 730 };
int[] coinYDate = { 140, 180, 150, 190, 140, 170, 160, 140 };
// 使用数组放置3个炸弹的数量、横坐标、纵坐标
int[] bombNum = { 9, 10, 11 };
int[] bombXDate = { 250, 450, 650 };
int[] bombYDate = { 160, 160, 190 };
//boolean[] coinIsDead = new boolean[8]; //设置右上角显示的金币图片和吃到的金币数量
GameCore.createImage(0);
GameCore.setImageSource(0, "bigCoin.png");
GameCore.setImagePosition(0, 710, 20);
GameCore.createText(0, "NUM");
GameCore.setTextColor(0, Color.WHITE);
GameCore.setTextPosition(0, 650, 25);
GameCore.createText(1, "0");
GameCore.setTextPosition(1, 760, 25);
GameCore.setTextColor(1, Color.WHITE); //设置左上角显示的时间倒计时
GameCore.createText(3, "Time-Left");
GameCore.setTextColor(3, Color.WHITE);
GameCore.setTextFontSize(3, 20);
GameCore.setTextPosition(3, 25, 5);
GameCore.createText(4, "60");
GameCore.setTextPosition(4,60, 25);
GameCore.setTextColor(4, Color.WHITE);
GameCore.setTextFontSize(4, 28); //设置游戏倒计时的时间开始点
long timeBegin=System.currentTimeMillis(); //设置吃掉金币前金币依然活着 另一种初始化数组方法 boolean[]
boolean[] coinIsDead={false,false,false,false,false,false,false,false};
//金币精灵死亡数
int textNum = 0;
//根据横纵坐标设置金币精灵在游戏中的出现
for (int i = 0; i < coinNum.length; i++) {
GameCore.createSprite(coinNum[i], "coin");
GameCore.setSpritePosition(coinNum[i], coinXDate[i], coinYDate[i]);
GameCore.playSpriteAnimate(coinNum[i], "rotate", true);
}
//根据横纵坐标设置炸弹精灵在游戏中的出现
for (int i = 0; i < bombNum.length; i++) {
GameCore.createSprite(bombNum[i], "bomb");
GameCore.playSpriteAnimate(bombNum[i], "laser", true);
GameCore.setSpritePosition(bombNum[i], bombXDate[i], bombYDate[i]);
}
//创建新的爆炸精灵,等待触发
int newBombNum=12;
GameCore.createSprite(newBombNum, "bomb"); //创建获胜或失败时的文本,等待触发
int newTextNum=2; //无限循环获取时时键盘信息,改变女精灵的位置
for (;;) {
int codeNum = GameCore.getPressedKeyCode();
Point position = GameCore.getSpritePosition(spriteGirl);
//如果按向上或W键则向上移动 并且设置上边界为135
if (codeNum == KeyEvent.VK_UP || codeNum == KeyEvent.VK_W) {
if (position.y > 135) {
GameCore.setSpritePosition(spriteGirl, position.x, --position.y);
GameCore.pause(1);
}
}
//如果按向下或S键则向下移动 并且设置下边界为190
if (codeNum == KeyEvent.VK_DOWN || codeNum == KeyEvent.VK_S) {
if (position.y < 190) {
GameCore.setSpritePosition(spriteGirl, position.x, ++position.y);
GameCore.pause(1);
}
}
//如果按向左或A键则向左移动 并且设置左边界为0
if (codeNum == KeyEvent.VK_LEFT || codeNum == KeyEvent.VK_A) {
if (position.x > 0) {
GameCore.setSpriteFlipX(spriteGirl, true);
GameCore.setSpritePosition(spriteGirl, --position.x, position.y);
GameCore.pause(2);
}
}
//如果按向右或D键则向右移动 并且设置右边界为730
if (codeNum == KeyEvent.VK_RIGHT || codeNum == KeyEvent.VK_D) {
if (position.x < 730) {
GameCore.setSpriteFlipX(spriteGirl, false);
GameCore.setSpritePosition(spriteGirl, ++position.x, position.y);
GameCore.pause(1);
}
}
//得到当前女精灵的位置
Point pGirl = GameCore.getSpritePosition(spriteGirl);
//判断女精灵和任意金币的距离,足够接近金币就消失,并设置该金币已死
for (int i = 0; i < coinNum.length; i++) {
if(coinIsDead[i]) {
continue;
}
Point pCoin = GameCore.getSpritePosition(coinNum[i]); double distance = Math.sqrt((Math.pow((pCoin.x - pGirl.x), 2) + Math.pow((pCoin.y - pGirl.y), 2)));
if (distance < 30) {
GameCore.hideSprite(coinNum[i]);
coinIsDead[i] = true;
textNum = 0;//这句含义很大,无限循环每次到这清零,不然数字无限疯长
for(int j=0;j<coinNum.length;j++) {
if(coinIsDead[j]==true){//我把这写成了i,所以只要吃掉一个,就是8了,应该是j
textNum++;
}
}
GameCore.setText(1, Integer.toString(textNum));//这句放在循环外边,不然
}
if(textNum==8) {
//输出文本
GameCore.createText(newTextNum, "You Win");
GameCore.setTextPosition(newTextNum, 300, 150);
GameCore.setTextColor(newTextNum, Color.RED);
GameCore.setTextFontSize(newTextNum, 88);
GameCore.pause(3000);
GameCore.exit(); }
}
//判断女精灵和任意炸弹的距离,足够接近炸弹就爆炸,游戏失败
for (int i = 0; i < bombNum.length; i++) {
Point pBomb = GameCore.getSpritePosition(bombNum[i]); double distance = Math.sqrt((Math.pow((pBomb.x - pGirl.x), 2) + Math.pow((pBomb.y - pGirl.y), 2)));
if (distance < 20) {
//隐藏原炸弹精灵
GameCore.hideSprite(bombNum[i]);
//出现新爆炸精灵
GameCore.playSpriteAnimate(newBombNum, "fire", true);
//在原来炸弹的位置出现新炸弹
GameCore.setSpritePosition(newBombNum, pBomb.x, pBomb.y);
GameCore.pause(3000);
//输出文本
GameCore.createText(newTextNum, "You Lose");
GameCore.setTextPosition(newTextNum, 300, 150);
GameCore.setTextColor(newTextNum, Color.RED);
GameCore.setTextFontSize(newTextNum, 88);
GameCore.pause(2500);
GameCore.exit();
}
}
//设置游戏倒计时的时间结束点
long timeEnd=System.currentTimeMillis();
int timeText=(int)(60-(timeEnd-timeBegin)/1000);
GameCore.setText(4, Integer.toString(timeText));
if(timeText==0) {
GameCore.alert("游戏通关时间已到,即将结束。");
GameCore.exit();
}
} }
}
(1)新增炸弹功能,精灵如果吃到炸弹立即死亡,游戏结束;
(2)新增游戏倒计时功能,倒计时60秒
(3)新增游戏胜利失败功能,吃完金币则胜利,吃到炸弹失败 游戏包及素材下载地址:
https://pan.baidu.com/s/1jHI54Po

最新文章

  1. Compiler Error Message: CS0016: Could not write to output file 回绝访问
  2. AC日记——合法C标识符 openjudge 1.7 06
  3. LabVIEW如何将脚本插入Quick Drop
  4. jeecms获取栏目标题图
  5. python爬虫学习(1)__抓取煎蛋图片
  6. 第三天关于网页sip的学习。平台win7 64位 freeSwitch jssip架构web网络电话
  7. CSS Clip剪切元素实例
  8. 理解JMS规范中消息的传输模式和消息持久化
  9. windows 下搭建 apache + php52 + postgreSQL7/8/9环境
  10. 华为JAVA(面试问题及答案节)
  11. li点击弹出序号
  12. Java 基础知识总结
  13. python网络爬虫之使用scrapy自动爬取多个网页
  14. 网卡驱动引起openstack的mtu问题
  15. 面试题之(js实现当年剩余时间倒计时程序)
  16. Existing lock /var/run/yum.pid: another copy is running as pid 解决办法
  17. java读取jar包中的文件
  18. node api 之:Error
  19. Azure ARM (15) 根据现有VHD文件,创建ARM VM
  20. delphi 属性 参数 新注释

热门文章

  1. OPENGL_单位长度对应屏幕像素
  2. 51nod1076(tarjan)
  3. ASPNET-ASPNETCORE 认证
  4. (转)cookie和session的区别
  5. Markdown - 如何给文本加下划线
  6. [题解]数学期望_luogu_P1850_换教室
  7. urllib库的基本使用
  8. hibernate Day2 案例代码
  9. 一图秒懂http与https的区别
  10. e​c​s​h​o​p​调​用​商​品​简​单​描​述