一.利用wx:if及wx:for数据绑定来实现数据输出乘法口诀表的编程
1.打开微信开发者工具,新建一个项目
2.在index.wxml输入以下代码
<!--index.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>
3.在index.wxss输入以下代码
/** index.wxss **/
.con {
font-size: 10px;
margin: 10px;
}
.inline {
display: inline-block;
width: 38px;
}
4.保存后然后编译,结果如下
二.编写程序,在 Console 控制台输出水仙花数(水仙花数是指一个3位数的各位上的数字的3次暴之和等于它本身。例如,1³+5³+3³=153)
1.js代码
// index.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>
2.运行结果
三.编写程序,在页面中输出水仙花数
1.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",
"navigationBarTitleText": "水仙花数总共有",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
wxml代码
<view class="container">
<view class="narcissistic-numbers">
<text wx:for="{{narcissisticNumbers}}" wx:key="*this">{{item}}</text></view>
</view>
wxss代码
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.narcissistic-numbers {
margin-top: 20px;
}
.narcissistic-numbers text {
display: block;
margin-bottom: 10px;
}
2.结果如图
四.编写程序,在页面中输出菱形图案
1.wxml代码
<view class="diama">
<view> *</view>
<view> ***</view>
<view> *****</view>
<view> *******</view>
<view>*********</view>
<view> *******</view>
<view> *****</view>
<view> ***</view>
<view> *</view>
</view>
2.wxss代码
.daima {
text-align: center;
margin-top: 20px;
font-size: 20px;
}
3.运行结果