1.利用wx:if及wx:for数据绑定来实现输出乘法口诀表(图2-21)的编程。
wxml
<view class='row' wx:for="{{[1,2,3,4,5,6,7,8,9]}}"
wx:for-item="row">
<view class='col' wx:for="{{[1,2,3,4,5,6,7,8,9]}}"
wx:for-item="col" wx:if="{{col<=row}}">
{{col}}*{{row}}={{col*row}}
</view>
</view>
wxss
.row{
display: flex;
justify-content: flex-start;
font-size: 9px;
}
.row .col{
width: 40px;
}
运行结果
2.编写程序,在Console控制台输出水仙花数(水仙花数是指一个3位数的各位上的字的3次幂之和等于它本身。例如,1’+5’+3’=153)
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);
},
})
wxml
<view class="App">
<view class="app">
<text wx:for="{{message}}" wx:key="*this">{{item}}</text>
</view>
</view>
运行结果![](https://i-blog.csdnimg.cn/blog_migrate/05f9e153a12cd362bea78c245ea573f5.png)
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});
},
})
wxml
<view class="App">
<view class="app">
<text wx:for="{{message}}" wx:key="*this">{{item}}</text>
</view>
</view>
运行结果
4.编写程序,在页面中输出菱形图案
js
Page({
data: {
demo: []
},
onLoad: function() {
var demoString = '';
for (var a = 0; a < 7; a++) {
for (var b = 0; b < 6-a; b++) {
demoString += ' ';
}
for (var c = 0; c< 2 * a + 1; c++) {
demoString += '*';
}
demoString += '\n';
}
for (var a = 5; a >= 0; a--) {
for (var b = 0; b < 4; b++) {
demoString += ' ';
}
for (var c = 0; c < 2 * a + 1; c++) {
demoString += '*';
}
demoString += '\n';
}
this.setData({
demo: demoString
});
}
})
wxml
<view class="container">
<text>{{demo}}</text>
</view>
wxss
.container {
text-align: center;
}
运行结果