3.20学习进度
技术学习
3.20 学习进度
java学习
//游戏启动类public class GameLauncher {public static void main(String args[]){Game game_one = new Game();game_one.gameStart();}}//游戏类public class Game {Player p1;Player p2;Player p3;public void gameStart(){p1 = new Player();p2 = new Player();p3 = new Player();int numberp1 = 0;int numberp2 = 0;int numberp3 = 0;boolean p1Result = false;boolean p2Result = false;boolean p3Result = false;int targetNumber = (int) (Math.random()*20);System.out.println("The number is betwenn 0 - 19\n");while(true){System.out.println("The number is "+targetNumber);p1.guess();p2.guess();p3.guess();numberp1 = p1.number;numberp2 = p2.number;numberp3 = p3.number;if(numberp1==targetNumber){p1Result = true;}if(numberp2==targetNumber){p2Result = true;}if(numberp3==targetNumber){p3Result = true;}if(p1Result||p2Result||p3Result){System.out.println("let's check the answer");System.out.println("Player 1 answer is"+numberp1);if(p1Result){System.out.println("\n### P1 wins!!! ###\n");}System.out.println("Player 2 answer is"+numberp2);if(p2Result){System.out.println("\n### P2 wins!!! ###\n");}System.out.println("Player 3 answer is"+numberp3);if(p3Result){System.out.println("\n### P3 wins!!! ###\n");}System.out.println("\n GAME OVER!!! \n");break;}else{System.out.println("\nsorry,Let's start a new game\n");}}}}//玩家类public class Player {int number = 0;public void guess(){number = (int)(Math.random()*20);System.out.println("I guess "+number);}}
安卓
C语言和数据结构
#include<stdio.h>#include<stdlib.h>#define LEN sizeof(Node)#define MAX 64 //为结构体Node和枚举类型Status更改名字typedef struct Node{int data;struct Node *next;}Node,*ptr_Node;typedef enum Status{SUCCESS,ERROR}Status;ptr_Node pt; //将链表头节点作为全局变量int exist=0; //将链表是否存在作为全局变量void select(); //函数声明ptr_Node create(int *arr,int n);void destroy(ptr_Node head);Status insert(ptr_Node head,ptr_Node node,int index);Status delete(ptr_Node head,int index,int *data);int search(ptr_Node head,int data);Status edit(ptr_Node head,int index,int *data);int main(void) //主函数处理输入输出{printf("请输入任意你想存放至链表的数字(除0),输入任意字符停止\n");printf("当前支持最大输入的数据为%d个\n",MAX);printf("需要修改请把LinkLiset.c文件中#define MAX %d 改成你所需的数字\n\n",MAX);select();system("pause");}ptr_Node create(int *arr,int n) //创建单链表函数{if(n<=0) //判断传入数组的长度return NULL; ptr_Node p1,p2,head; //变量定义和初始化p1=p2=(ptr_Node)malloc(LEN);head =NULL;p1->data=*arr; int j=0;while(*(arr+j)!='\0') //循环使上一个节点的next指向下一个节点的data数据的地址{j=j+1; if(j==1)head=p1; elsep2->next = p1; p2=p1;p1=(ptr_Node)malloc(LEN);p1->data=*(arr+j);} p2->next=NULL; //防止野指针free(p1); //释放循环结前申请的无用空间p1=NULL; //防止野指针return(head);}void destory(ptr_Node head) //销毁链表函数{if(head==NULL)printf("ERROR");else{ptr_Node p1=NULL,p2=NULL;p1=head;p2=p1->next;while(p2!=NULL){p1->next=NULL;free(p1);p1=p2;p2=p1->next;}p1=NULL;p2=NULL;head=NULL;}}void print(ptr_Node head) //输出链表函数{ptr_Node p=NULL;if(head==NULL)printf("\nERROR,the head is empty\n");else{p=head;printf("what we get is:\n\t\t(head)%d",p->data);p=p->next;while(p!=NULL) //当指针不为空的时候输出{printf("---->%d",p->data);p=p->next;}printf("(end)\n\nThat‘s all.\n");}}Status insert(ptr_Node head,ptr_Node node,int index)//插入节点{ptr_Node p1=head;ptr_Node p2=NULL;ptr_Node p3=node;int j,n=index-1;if(index==1) //当插入的节点是要插到头节点时候{p3->next=p1;pt=p3;return SUCCESS; }for(j=0;j<index;j++) //for循环找到插入的位置{if(j==index-1) //if条件中执行插入操作{p2=p1->next;p1->next=p3;p3->next=p2;}p2=p1->next;p1=p2;}return SUCCESS;}Status delete(ptr_Node head,int index,int *data) //删除函数{ptr_Node p1=head;ptr_Node p2=NULL;int j;int *p=data; for(j=0;j<=index-2;j++) //找到待删除的节点的位置{p2=p1->next;p1=p2;}p2=p1->next;p1->next=p2->next; //将p1指针指向待删除的节点的下一个节点*p=p2->data;printf("the data deleted is:%d\n",p2->data);free(p2); printf("\n!!!链表已根据您的需求删除节点!!!\n");printf("查看请输入任意数字,不查看输入任意字符\n");if(scanf("%d",&j))print(pt); //输出链表elsereturn SUCCESS;return SUCCESS;}int search(ptr_Node head,int data) //查找节点函数{ptr_Node p1=head;ptr_Node p2=head; for(int i=0;p2!=NULL;++i) //执行查找操作,找到返回节点的位置{if(p2->data == data)return i;elsep2=p2->next;}return -1; //未找到返回-1}Status edit(ptr_Node head,int index,int *data) //修改节点值函数{ptr_Node p1=head;ptr_Node p2=head;for(int i=0;i<index;++i){p1 = p1->next;}int temp = p1->data;p1->data = *data;*data = temp; return SUCCESS;}void select(){int a[MAX],data[5],length,t,select=1,index=1;//a待存数组,data待存数组,length链表长度,select为控制页面变量int number; //用于search函数中查找int data_num=0;for(length=0;scanf("%d",a+length);length++);*(a+length)='\0'; //自定义输入链表中的数据system("cls");printf("已接收数据\n接下来请选择你所需操作\n\n");while(select) //接下来使用一个swirch处理事件{printf("\n1.根据输入数组生成链表");printf("\n2.销毁链表链表");printf("\n3.根据输入的值在链表中插入");printf("\n4.根据输入的值在链表中 删除");printf("\n5.根据输入的值在链表中 查找");printf("\n6.根据data中的值在链表中修改");printf("\n7.输出链表");printf("\n8.输出data数组");printf("\n9.修改data数组中第一个值");printf("\n0.退出");printf("\n若输入非1-9,0数字,则默认第一个选项\n");printf("\n请选择你的操作:");getchar();scanf("%d",&select);switch(select){case 1:if(exist){printf("\n已存在链表,自动销毁链表中\n");destory(pt);printf("\n链表销毁完毕\n");printf("将根据你最开始输入的原始数据重新生成链表\n");} pt = create(a,length); //调用create方法生成单链表exist=1; //链表生成printf("\n链表生成完毕\n");print(pt);getchar();printf("输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 2:if(exist) //检测链表是否已存在{destory(pt);printf("\n链表销毁完毕\n\n");exist=0; //链表已销毁}elseprintf("链表未生成,无法销毁,请退出至菜单运行1\n"); printf("输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 3:if(exist) //检验链表是否生成printf("链表已存在,可以插入\n"); else{pt = create(a,length);printf("\n链表生成完毕\n");exist=1;}ptr_Node node; //生成一个新的节点node = (ptr_Node)malloc(LEN);node->next=NULL;node->data=0;printf("请输入你想插入的第几个节点和插入节点的数据\n");A:scanf("%d %d",&index,&t); //检查用户使用数据是否越界if(index<=0||index>length){printf("数据越界,请重新输入\n");goto A;}node->data=t;insert(pt,node,index);printf("修改之后的链表为:\n");print(pt);length++;printf("输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 4:if(exist) //检验链表是否生成printf("链表已存在,可以删除\n"); else{pt = create(a,length);printf("\n链表生成完毕\n");exist=1;}printf("\n请输入你想删除的第几个节点之后的节点:");B:scanf("%d",&index); //检查用户使用数据是否越界if(index<=0||index>=length){printf("数据越界,请重新输入\n");goto B;}delete(pt,index,data);data_num = (length-index); //检查data数组个数length--; //修改链表当前长度printf("输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 5:if(!exist) //检验链表是否生成{printf("链表未生成\n");printf("将根据你最开始输入的原始数据重新生成链表\n");pt = create(a,length);printf("\n链表生成完毕\n");exist=1;}elseprintf("链表存在,可进行查找\n");printf("\n请输入你需要查找的值:");scanf("%d",&number);int temp=search(pt,number); //检验是否找到节点if(~temp) //按位取反检验是否找到节点,是则输出节点位置,否则说明失败printf("在第%d个节点存在与data相同的值\n\n",temp);elseprintf("未能找到所需的data值\n\n");printf("\n输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 6: if(data_num) //检测data数组中是否有值{if(!exist){printf("链表未生成\n");printf("将根据你最开始输入的原始数据重新生成链表\n");pt = create(a,length);printf("\n链表生成完毕\n");exist=1;}elseprintf("链表存在,可进行修改\n"); printf("\n请输入你想修改的第几个节点之后的节点:");C:scanf("%d",&index); //检查用户使用数据是否越界if(index<=0||index>=length){printf("数据越界,请重新输入\n");goto C;} Status b=edit(pt,index,data);b==SUCCESS?printf("\n修改成功"):printf("\n修改失败");//输出修改的结果是否成功}elseprintf("\ndata中无值,无法进行修改操作,请退出至菜单运行6或9\n"); printf("\n输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 7:if(exist) //检验链表是否存在print(pt);else{printf("链表未生成,无法输出,请退出至菜单运行1\n\n");}printf("输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 8: if(data_num) //检测data数组是否有值,有则输出,无则要求用户输入{printf("\ndata数组为:");for(int k=0;k<data_num;k++)printf("\t%d",data[k]);}elseprintf("data数组中未有值,请退出至菜单运行4\n");printf("\n输入一个整数清屏并退出至菜单,输入字符退出程序\n");if(scanf("%d",&t))system("cls");else{select=0;break;}break; case 9:if(data_num) //检验data数组中是否有值,有则无需输入,无则要求用户输入printf("已经有数据了,无需输入\n");else{printf("\n请输入一个整数\n");F:if(scanf("%d",&data[0])) //防止用户输入错误的数据类型{printf("\n输入成功\n");data_num=1;}else{printf("\n输入失败,请退出至菜单重新输入\n");}}printf("\n输入一个整数清屏并退出至菜单,输入字符退出程序\n");getchar();if(scanf("%d",&t))system("cls");else{select=0;break;}break; default:break;} }}