php打印输出棋盘的实现方法
这篇文章主要介绍了php打印输出棋盘的实现方法,以实例形式分析了两种不同的实现方法,具有一定的参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php打印输出棋盘的两种实现方法。分享给大家供大家参考。具体实现方法如下:
例子1,代码如下:
复制代码 代码如下:
/**
* 隔行隔列换色
* string fun_table(int $rows=9,int $cols=9)
* $rows 表示行数 必须为整数 并且必须在1-20之间
* $cols 表示列数 必须为整数 并且必须在1-20之间
*/
function fun_table($rows=9,$cols=9){
if ($rows20){
return "必须为整数 并且必须在1-20之间";
}
if ($cols20){
return "必须为整数 并且必须在1-20之间";
}
if($rows!=(int)($rows)){
return '行数 必须为整数';
}
if($cols!=(int)($cols)){
return '列数 必须为整数';
}
$str="";
$str.= "
"; }else{ $str.= " |
"; |
return $str;
}
echo fun_table();
?>
例子2 简单实现棋盘-for循环
实现这个棋盘首先我们想想棋盘是怎么样的,是有很多个方格组成,然后由黑色和白色的相间的方格组成,首先我们先把方格画出来,代码如下:
复制代码 代码如下:
echo "
54im | ";
?>
看到上面棋盘后,考虑下黑白格排放位置,有个规律可以发现,横排和竖排上白色格子都是基数,黑色的都是偶数,我们可以用取余的方法来判断这个格子该显示什么颜色,基数单元格我让他显示白色,偶数单元格显示黑色,基数+偶数=偶数,所以偶数单元格(黑色)我们很好找出来了,剩余的就是基数格(白色),代码如下:
复制代码 代码如下:
/**
通过for循环和html实现棋盘
**/
echo "
"; }else{ echo " |
"; |
?>
希望本文所述对大家的php程序设计有所帮助。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
