第一题
利用wx:if及wx:for数据绑定来实现输出乘法口诀表的编程。代码如下:
.js
Page({
})
.json
{
}
.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: 10px;
}
.row .col{
width: 40px;
}
![](https://i-blog.csdnimg.cn/blog_migrate/b3c0ad4feab2874826f02c15357a72b1.png)
第二题
编写程序,在Console控制台输出水仙花数(水仙花数是指一个3位数的各位上的数字的3次幂之和等于它本身。代码如下:
.js
Page({
data: {
result: ''
},
inputChange(e) {
this.setData({
number: e.detail.value
})
},
checkNumber() {
const number = this.data.number;
if (number.length !== 3) {
this.setData({
result: '请输入一个三位数'
})
return;
}
const digit1 = parseInt(number.charAt(0));
const digit2 = parseInt(number.charAt(1));
const digit3 = parseInt(number.charAt(2));
const sum = Math.pow(digit1, 3) + Math.pow(digit2, 3) + Math.pow(digit3, 3);
if (sum === parseInt(number)) {
this.setData({
result: '是水仙花数'
})
} else {
this.setData({
result: '不是水仙花数'
})
}
}
})
.json
{
"navigationBarBackgroundColor": "#000000",
"navigationBarTitleText": "水仙花数",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
.wxml
<view class="container">
<view class="title">水仙花数判断</view>
<input class="input" placeholder="请输入一个三位数" bindinput="inputChange" />
<button class="button" bindtap="checkNumber">验证</button>
<view class="result">{{result}}</view>
</view>
.wxss 空
![](https://i-blog.csdnimg.cn/blog_migrate/6b11aadd459eb86c2e859580532704eb.png)
第三题
编写程序,在页面中输出水仙花数。代码如下:
.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": "#0000ff",
"navigationBarTitleText": "水仙花数总共有",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
.wxss
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.narcissistic-numbers {
margin-top: 22px;
}
.narcissistic-numbers text {
display: block;
margin-bottom: 12px;
}
.wxml
<view class="container">水仙花数共有:
<view class="narcissistic-numbers">
<text wx:for="{{narcissisticNumbers}}" wx:key="*this">{{item}},</text></view>
</view>
![](https://i-blog.csdnimg.cn/blog_migrate/3f98785a62219242174197a69de36811.png)
第四题
编写程序,在页面中输出菱形图案。
.js
Page({
})
.json
{
}
.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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6be8452df1cdbf583ab87b6727f4e9c6.png)