用C#编写猜数、九九乘法表‘、迷宫
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
//数字9 代表墙
//数字0 代表通路
//数字1 代表入口 只有一个
//数字2 代表出口 只有一个
//数字3 代表已走过
//数字8 代表死胡同
//最终按照3代表的路径走出迷宫
int[,] mg = new int[10, 10]
{
{9,9,9,9,9,9,9,9,9,9},
{9,1,0,9,0,9,0,9,0,9},
{9,0,9,9,0,0,0,0,0,9},
{9,0,0,0,0,9,9,0,0,9},
{9,0,9,0,9,0,0,0,0,9},
{9,0,0,0,9,0,0,9,9,9},
{9,0,9,0,0,9,0,0,0,9},
{9,0,9,9,9,0,0,9,0,9},
{9,9,0,0,0,0,0,9,2,9},
{9,9,9,9,9,9,9,9,9,9}
};
static void Main(string[] args)
{
//*********九九乘法表********
////for (int i = 1; i <10; i++)
////{
//// for (int j =1; j <10; j++)
//// {
//// if (j <= i)
//// {
//// Console.Write("{0}*{1}={2}\t", i, j, i * j);
//// }
//// }
//// Console.WriteLine();
////}
////Console.ReadKey();
//Helloworld tet = new Helloworld();
//tet.wrie();
//*********九九乘法表********
//*********猜数游戏********
//bool temp = true;
//Random rd = new Random();
//int i = rd.Next(1, 100), j;
//Console.WriteLine("欢迎来玩猜数游戏");
//while(temp == true)
//{
// Console.Write("开始游戏,请输入数字:");
// j = Convert.ToInt32(Console.ReadLine());
// if(j > i)
// {
// Console.WriteLine("你输入的数字太大了,请再来一次!");
// continue;
// }
// else if (j < i)
// {
// Console.WriteLine("你输入的数字太小了,请重新输入:");
// continue;
// }
// Console.WriteLine("恭喜你猜对了!");
// temp = false;
//}
//*********猜数游戏********
//*********加减乘除计算器
// while (true)
// {
// calcu();
// Console.WriteLine("是否继续执行循环?Y/N");
// string s = Console.ReadLine();
// if(s.ToUpper() == "Y")
// {
// calcu();
// continue;
// }
// break;
// }
// }
//static void calcu()
// {
// Console.WriteLine("请输入第一个数字:");
// float num1 = 0;
// float num2 = 0;
// string fuhao = "";
// while (true)
// {
// try
// {
// num1 = Convert.ToSingle(Console.ReadLine());
// break;
// }
// catch (Exception)
// {
// num1 = 0;
// Console.WriteLine("输入错误,请重新输入第一个数字:");
// }
// }
// Console.WriteLine("请输入第二个数字:");
// while (true)
// {
// try
// {
// num2 = Convert.ToSingle(Console.ReadLine());
// break;
// }
// catch (Exception)
// {
// num1 = 0;
// Console.WriteLine("输入错误,请重新输入第二个数字:");
// }
// }
// Console.WriteLine("请输入运算符(+ - * /):");
// while (true)
// {
// try
// {
// fuhao = Console.ReadLine();
// if (fuhao == "+" || fuhao == "-" || fuhao == "*" || fuhao == "/")
// break;
// fuhao = "";
// Console.WriteLine("运算符输入错误,请重新输入:");
// }
// catch (Exception)
// {
// fuhao = "";
// Console.WriteLine("运算符输入错误,请重新驶入");
// }
// }
// float num = 0;
// switch (fuhao)
// {
// case "+": num = num1 + num2; break;
// case "-": num = num1 - num2; break;
// case "*": num = num1 * num2; break;
// case "/": num = num1 / num2; break;
// }
// Console.WriteLine("运算结果出炉:" + num1 + fuhao + num2 + "=" + num);
//*********加减乘除计算器
//*******迷宫游戏
Program pm = new Program();
Console.WriteLine("原始迷宫:");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(pm.mg[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
//走出迷宫
bool bl = pm.getEntrance();
if (bl == false)
{
Console.Write("未指明迷宫入口! ");
return;
}
point cpt = pm.stk.Pop();
pm.stk.Push(cpt);
while (cpt.var != 2)
{
if (pm.scanUp(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
if (pm.scanDown(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
if (pm.scanLeft(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
if (pm.scanRight(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
pm.mg[cpt.row, cpt.cul] = 8;
cpt = pm.stk.Pop();
}
Console.WriteLine("走出迷宫(数字3 代表出迷宫路径):");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(pm.mg[i, j] + " ");
}
Console.WriteLine();
}
Console.Write("END");
}
public struct point
{
public int row;
public int cul;
public int var;
}
//路径栈
Stack<point> stk = new Stack<point>();
//得到入口 从入口进入
public bool getEntrance()
{
point ent = new point();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (mg[i, j] == 1)
{
ent.row = i;
ent.cul = j;
mg[i, j] = 3;
ent.var = mg[i, j];
stk.Push(ent);
return true;
}
}
}
return false;
}
//向上扫描
public bool scanUp(point cpt)
{
int cr = cpt.row - 1;
int cc = cpt.cul;
if (cr < 0 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//向下扫描
public bool scanDown(point cpt)
{
int cr = cpt.row + 1;
int cc = cpt.cul;
if (cr > 10 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//向左扫描
public bool scanLeft(point cpt)
{
int cr = cpt.row;
int cc = cpt.cul - 1;
if (cc < 0 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//向右扫描
public bool scanRight(point cpt)
{
int cr = cpt.row;
int cc = cpt.cul + 1;
if (cc > 10 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//**********迷宫游戏
}
}
//*********九九乘法表********
//public class Helloworld
//{
// public void wrie()
// {
// for (int i = 1; i < 10; i++)
// {
// for (int j = 1; j < 10; j++)
// {
// if (j <= i)
// {
// Console.Write("{0}*{1}={2}\t", i, j, i * j);
// }
// }
// Console.WriteLine();
// }
// Console.ReadKey();
// }
//}
//*********九九乘法表********
用C#编写猜数、九九乘法表‘、迷宫的更多相关文章
- python编写九九乘法表代码
打印九九乘法表 代码: #!/usr/bin/env python # -*- coding: UTF-8 -*- # 项目二: # 1.要求:编写九九乘法表 # 2.分析: # 根据九九乘法表的样式 ...
- 用Python编写九九乘法表考虑print自动换行问题
编写了一个简单的小程序九九乘法表,代码如下: for i in range(1,10): for j in range(1,i+1): print(" %d*%d=%d" % (j ...
- Python学习笔记系列——九九乘法表&猜大小
再重新捡起Python,数据库短时间之内已经没啥看的了,不知道今年结束之前能不能Python入门,一直认为自己是没有编程思想的... 1.九九乘法表 #九九乘法表实现的一种方式之一 def Multi ...
- 使用VS2017进行Python代码的编写并打印出九九乘法表
我们来盘一盘怎么使用VS2017进行python代码的编写并打印出九九乘法表. 使用Visual Studio 2017进行Python编程不需要太复杂的工作,只需要vs2017安装好对Python的 ...
- 使用 JavaScript 用循环嵌套输出乘法表。外循环控制行数,内循环控制当前行要输出的乘法表达式,在页面上输出九九乘法表
查看本章节 查看作业目录 需求说明: 在页面上输出九九乘法表,实现效果如图所示 实现思路: 创建HTML页面 在页面中嵌入 <script type="text/javascript& ...
- 编写Java程序,使用循环结构打印出九九乘法表
编写Java程序,使用循环结构打印出九九乘法表 效果如下: 实现代码: public class Multiplication99 { public static void main(String[] ...
- 编写一个jsp页面,输出九九乘法表。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- 九九乘法表,全js编写,放入table表格带入页面渲染出来
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用lambda编写九九乘法表
Java 8 出来有一段时间了,支持lambda表达式 非常的赞. lambda表达式 即匿名方法,属于一种轻量级的封装 lambda表达式的语法由参数列表.箭头符号->和函数体组成.函数体既可 ...
随机推荐
- nginx源代码分析--nginx模块解析
nginx的模块很之多.能够觉得全部代码都是以模块的形式组织.这包含核心模块和功能模块,针对不同的应用场合.并不是全部的功能模块都要被用到,附录A给出的是默认configure(即简单的httpser ...
- 以css为例谈设计模式
什么是设计模式? 曾有人调侃,设计模式是工程师用于跟别人显摆的,显得高大上:也曾有人这么说,不是设计模式没用,是你还没有到能懂它,会用它的时候. 先来看一下比较官方的解释:"设计模式(Des ...
- git忽略已经被提交的文件,以及如何恢复追踪
问题描述 之前在提交代码时,.gitignore 没有填写完整,导致idea编辑器的配置文件夹.idea被提交了 然后每次运行本地项目,都会在.idea文件夹下生成一堆文件,这时发现问题,将.idea ...
- 第一百六十八节,jQuery,表单选择器
jQuery,表单选择器 学习要点: 1.常规选择器 2.表单选择器 3.表单过滤器 表单作为 HTML 中一种特殊的元素,操作方法较为多样性和特殊性,开发者不但可以 使用之前的常规选择器或过滤器,也 ...
- Spring MVC列表多选框
以下示例显示如何在使用Spring Web MVC框架的表单中使用列表框(Listbox).首先使用Eclipse IDE来创建一个WEB工程,实现一个让用户可选择自己所善长的技术(多选)的功能.并按 ...
- yii2 刷新缓存(刷新模型缓存)
Yii2开启表结构缓存,因为当运用模型(model)时,AR的一些公共属性都会从DB中获取,这样会导致服务器负担一些额外的资源开销,实际上对于成品来说,服务器这些开始销是多余的,故应该阻止这种默认行为 ...
- HTML5新增的语义标签和IE版本低的兼容性问题
<!DOCTYPE html><html> <head> <!-- HTML5中浏览器兼容(较低版本的IE浏览器不支持H5的布局):需要在<head&g ...
- hdu 5471(状压DP or 容斥)
想了最复杂的思路,用了最纠结的方法,花了最长的时间,蒙了一种规律然后莫名其妙的过了. MD 我也太淼了. 后面想了下用状压好像还是挺好写的,而且复杂度也不高.推出的这个容斥的规律也没完全想透我就CAO ...
- (六)通过solr7的API实现商品的列表查询
(六)通过solr7的API实现商品的列表查询 工具类: 获取 HttpSolrClient public class Constant { public static HttpSolrClient ...
- tomcat8热部署配置--maven自动发布项目到tomcat8(如何支持远程访问部署)
1.tomcat8实现热部署的配置 我们实现热部署后,自然就可以通过maven操作tomcat了,所以就需要maven取得操作tomcat的权限,现在这一步就是配置tomcat的可操作权限. #进入 ...