java九宫格代码_Java实现九宫格的简单实例

Java实现九宫格的简单实例

九宫格:共有三行三列九个格子,从1到9共九个数字不重复地填入这九个格子中,条件是每行、每列、两个对角线上三个数字的和相等。

下面用Java实现九宫格:

public class NineTable {

public static void main(String[] args) {

int arr[][] = new int[3][3];

int a = 2;

int b = 3 / 2;

for (int i = 1; i <= 9; i++) {

arr[a++][b++] = i;

if (0 == i % 3) {

a = a - 2;

b = b - 1;

}

else {

a = a % 3;

b = b % 3;

}

}

System.out.println("output:");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(arr[i][j] + " ");

}

System.out.print("\n");

}

}

}

在Linux上运行:

$javac NineTable.java

$java NineTable

output:

4 9 2

3 5 7

8 1 6

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Vue实现九宫格抽奖程序的步骤: 1.创建一个Vue实例 ``` new Vue({ el: '#app', data: { prizes: [ {text: '一等奖', color: '#FF6B6B'}, {text: '二等奖', color: '#FFE66D'}, {text: '三等奖', color: '#6BCBFF'}, {text: '谢谢参与', color: '#C4C4C4'} ], selectedPrize: null, isRolling: false }, methods: { roll() { this.isRolling = true const index = Math.floor(Math.random() * this.prizes.length) this.selectedPrize = this.prizes[index] setTimeout(() => { this.isRolling = false }, 3000) } } }) ``` 2.创建九宫格组件 ``` <template> <div class="grid"> <div class="cell" v-for="(prize, index) in prizes" :key="index" @click="!isRolling && roll"> <p :style="{color: prize.color}">{{prize.text}}</p> <div class="icon"> <i class="fas fa-gift"></i> </div> </div> </div> </template> <script> export default { props: ['prizes', 'isRolling', 'roll'] } </script> <style scoped> .grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 10px; } .cell { position: relative; display: flex; flex-direction: column; justify-content: center; align-items: center; background-color: #fff; border-radius: 10px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1); cursor: pointer; } .cell:hover { transform: translateY(-5px); box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2); } .icon { position: absolute; top: -30px; left: 50%; transform: translateX(-50%); background-color: #fff; border-radius: 50%; padding: 5px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1); } .fa-gift { color: #FF6B6B; font-size: 24px; } </style> ``` 3.在HTML中引用九宫格组件和Vue实例 ``` <div id="app"> <prize-grid :prizes="prizes" :isRolling="isRolling" :roll="roll"></prize-grid> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://kit.fontawesome.com/eb83b1af77.js"></script> <script src="./prize-grid.js"></script> ``` 4.运行程序 现在你可以在浏览器中运行程序,点击九宫格中的格子,就可以进行抽奖了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值