1. 利用 wx:if 及 wx:for 数据绑定来实现输出乘法口诀表。
*json代码
{
"navigationBarBackgroundColor": "#000000",
"navigationBarTitleText": "九九乘法表",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
*wxml代码
<view class='con'>
<view wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="i"><!--对应行-->
<view class='inline' wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="j"><!--对应列-->
<view wx:if="{{j<=i}}">
{{i}}×{{j}}={{i*j}}
</view>
</view>
</view>
</view>
*wxss代码
.con {
font-size: 14px; /** 设置字体大小 **/
margin: 10px; /** 设置外边距 **/
}
.inline {
display: inline-block; /** 设置布局,一行显示,**/
width: 65px; /** 设置宽度 block的**/
}
运行结果:
2.编写程序,在Console控制台输出水仙花数(水仙花数是指一个3位数的各位上的数字的3次幂之和等于它本身。)
*js代码
Page({
data:{
message: []
},
onLoad: function () {
this.findmessage();
},
findmessage: function (){
const num =[];
for (let i=100;i<1000;i++){
const a = Math.floor(i / 100);
const b = Math.floor((i % 100) / 10); const c=i% 10;
if (a ** 3 + b** 3 +c**3===i){
num.push(i);}}
this.setData({
message: num});
console.log("水仙花数:"+num);
},
})
*json代码
{
"navigationBarBackgroundColor": "#000000",
"navigationBarTitleText": "水仙花",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
*wxml代码
<view class="App">
<view class="app">
<text wx:for="{{message}}" wx:key="*this">{{item}}</text>
</view>
</view>
运行结果
3. 编写程序,在页面中输出水仙花花数。
*js代码
Page({
data:{
narcissisticNumbers: []
},
onLoad: function () {
this.findNarcissisticNumbers();
},
findNarcissisticNumbers: function (){
const numbers =[];
for (let i=100;i<1000;i++){
const a = Math.floor(i / 100);
const b = Math.floor((i % 100) / 10); const c=i% 10;
if (a ** 3 + b** 3 +c**3===i){
numbers.push(i);}}
this.setData({
narcissisticNumbers: numbers});}
});
*json代码
{
"navigationBarBackgroundColor": "#000000",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
*wxml代码
<view class="one">
<view class="two">水仙花数</view>
<view class="three">
<text class="three">水仙花数共有:153,370,371,407</text>
</view>
</view>
*wxss代码
/* pages/news3/news3.wxss */
.two{
padding-top: 65px;
text-align: center;
margin-bottom: 50px;
font-weight: bold;
}
.three{
text-align: center;
padding-bottom: 280px;
display: block;
}
4.编写程序,在页面中输出菱形图案。
*json代码
{
"navigationBarTextStyle": "black",
"navigationBarTitleText": "菱形",
"navigationBarBackgroundColor": "#ffffff"
}
*wxml代码
<view class="diamond">
<view> *</view>
<view> ***</view>
<view> *****</view>
<view> *******</view>
<view>*********</view>
<view> *******</view>
<view> *****</view>
<view> ***</view>
<view> *</view>
</view>
*wxss代码
.diamond {
text-align: center;
margin-top: 20px;
font-size: 20px;
}