第一题
index.wxml
<view class='con'>
<view wx:for="{{[1,2,3,4,5,6,7,8,9]}}" wx:for-item="i">
<view style='display:inline-block;width:35px'
wx:for="{{[9,8,7,6,5,4,3,2,1]}}" wx:for-item="j">
<view wx:if="{{i<=j}}">
{{i}}*{{j}}={{i*j}}
</view>
</view>
</view>
</view>
index.wxss
.con{
font-size: 10px
}
效果图
第二题
index.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);
},
})
index.wxlm
<view class="App">
<view class="app">
<text wx:for="{{message}}" wx:key="*this">{{item}}</text>
</view>
</view>
效果图
第三题
index.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});}
});
index.json
{
"navigationBarBackgroundColor": "#000000",
"navigationBarTitleText": "水仙花数总共有",
"navigationBarTextStyle": "white",
"backgroundTextStyle": "dark"
}
index.wxml
<view class="container">
<view class="row">水仙花数共有:
<view wx:for="{{narcissisticNumbers}}" wx:key="{{index}}">
<text>{{item}} </text>
</view>
</view>
</view>
index.wxss
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.row {
display: flex;
flex-direction: row;
}
.container text {
margin: 2px;
}
效果图
第四题
index.js
Page({
data: {
diamondLines: []
},
onLoad: function () {
const diamondLines = this.generateDiamond(10);
this.setData({
diamondLines: diamondLines
});
},
generateDiamond: function (height) {
const lines = [];
for (let i = 1; i <= height; i++) {
let line = '';
for (let j = 1; j <= height - i; j++) {
line += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += '*';
}
lines.push(line);
}
for (let i = height - 1; i >= 1; i--) {
let line = '';
for (let j = 1; j <= height - i; j++) {
line += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += '*';
}
lines.push(line);
}
return lines;
}
});
index.json
{
"navigationBarTitleText": "菱形"
}
index.wxml
<view class="container">
<view class="diamond">
<view wx:for="{{diamondLines}}" wx:key="{{index}}">
<text>{{item}} </text>
</view>
</view>
</view>
index.wxss
.container {
display: flex;
vertical-align: middle;
text-align: center;
}
.diamond {
margin-top: 20px;
}
.container text {
margin: 0px;
}
效果图