你可以使用一个简单的映射关系来简化你的代码。首先,你可以创建一个对象,将天干和对应的索引值进行映射,然后使用这些索引值来获取相应的十神。 下面是一个简化的示例代码: [JavaScript] 纯文本查看 复制代码 var shishen = ["比肩", "劫财", "食神", "伤官", "偏财", "正财", "七杀", "正官", "偏印", "正印"]; var tg = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]; function qshishen(rizhu, tiangan) { var rizhuIndex = tg.indexOf(rizhu); var tianganIndex = tg.indexOf(tiangan); if (rizhuIndex !== -1 && tianganIndex !== -1) { var shishenIndex = (rizhuIndex + tianganIndex) % 10; return shishen[shishenIndex]; } else { return "提交参数错误"; } } // 示例用法 var rizhu = "甲"; var tiangan = "丙"; var result = qshishen(rizhu, tiangan); console.log(result); // 输出:食神 这样,你只需要使用两个索引值进行计算,而不需要逐个判断每个组合。注意,这个示例代码只处理了甲到癸的情况,如果需要处理更多的组合,你可以根据这个思路进行扩展。 |
在 湘伦 这个朋友回答时 说由文心大模型生成,得到启发,在用讯飞星火模型 一点点的问,终于解决了 我需要的答案,看来这个AI模型是未来的疑难杂症的重要帮手,深有感触,因为我不太懂JS代码,写了个简单的,放在AI模型去优化代码。又学到了更精简的代码,看来以后有问题 不是找啥搜索引擎,而是先问AI模型。首选讯飞星火。至此 这个帖子结束 7天后 申请解除回答。顺便把解决后的代码 贴上 留待以后有需要的人参考 var shishen = ["比肩", "劫财", "食神", "伤官", "偏财", "正财", "七杀", "正官", "偏印", "正印"]; var tg = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]; function qshishen (rizhu, tiangan) { var rizhuIndex = tg.indexOf (rizhu); var tianganIndex = tg.indexOf (tiangan); if (rizhuIndex === -1 || tianganIndex === -1) { return document.write ("提交参数错误 "); } const shishenIndex = `${rizhuIndex}${tianganIndex}`; if ([ ' 00', '11', '22', '33', '44', '55', '66', '77', '88', '99'].includes(shishenIndex)) { //return document.write (shishen[0] + " "); return document.write (rizhu+tiangan+" "+shishen[0] + " "); } else if ([ ' 01', '10', '23', '32', '45', '54', '67', '76', '89', '98'].includes(shishenIndex)) { //return document.write (shishen[1] + " "); return document.write (rizhu+tiangan+" "+shishen[1] + " "); } else if ([ ' 02', '13', '24', '35', '46', '57', '68', '79', '80', '91'].includes(shishenIndex)) { //return document.write (shishen[2] + " "); return document.write (rizhu+tiangan+" "+shishen[2] + " "); } else if ([ ' 03', '12', '25', '34', '47', '56', '69', '78', '81', '90'].includes(shishenIndex)) { //return document.write (shishen[3] + " "); return document.write (rizhu+tiangan+" "+shishen[3] + " "); } else if ([ ' 04', '15', '26', '37', '48', '59', '60', '71', '82', '93'].includes(shishenIndex)) { //return document.write (shishen[4] + " "); return document.write (rizhu+tiangan+" "+shishen[4] + " "); } else if ([ ' 05', '14', '27', '36', '49', '58', '61', '70', '83', '92'].includes(shishenIndex)) { //return document.write (shishen[5] + " "); return document.write (rizhu+tiangan+" "+shishen[5] + " "); } else if ([ ' 06', '17', '28', '39', '40', '51', '62', '73', '84', '95'].includes(shishenIndex)) { //return document.write (shishen[6] + " "); return document.write (rizhu+tiangan+" "+shishen[6] + " "); } else if ([ ' 07', '16', '29', '38', '41', '50', '63', '72', '85', '94'].includes(shishenIndex)) { //return document.write (shishen[7] + " "); return document.write (rizhu+tiangan+" "+shishen[7] + " "); } else if ([ ' 08', '19', '20', '31', '42', '53', '64', '75', '86', '97'].includes(shishenIndex)) { // return document.write (shishen[8] + " "); return document.write (rizhu+tiangan+" "+shishen[8] + " "); } else if ([ ' 09', '18', '21', '30', '43', '52', '65', '74', '87', '96'].includes(shishenIndex)) { //return document.write (shishen[9] + " "); return document.write (rizhu+tiangan+" "+shishen[9] + " "); } } |
[JavaScript] 纯文本查看 复制代码 var shishen=["比肩","劫财","食神","伤官","偏财","正财","七杀","正官","偏印","正印"]; var tg=["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"]; var dz=["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"]; function qshishen(rizhu,tiangan) { if (rizhu.includes("甲") && tiangan.includes("甲") || rizhu.includes("乙") && tiangan.includes("乙") || rizhu.includes("丙") && tiangan.includes("丙") || rizhu.includes("丁") && tiangan.includes("丁") || rizhu.includes("戊") && tiangan.includes("戊") || rizhu.includes("己") && tiangan.includes("己") || rizhu.includes("庚") && tiangan.includes("庚") || rizhu.includes("辛") && tiangan.includes("辛") || rizhu.includes("壬") && tiangan.includes("壬") || rizhu.includes("癸") && tiangan.includes("癸") ){ return document.write(shishen[0]+ "<br>") //return dinren[yuefen1]; } else if (rizhu.includes("甲") && tiangan.includes("乙") || rizhu.includes("乙") && tiangan.includes("甲") || rizhu.includes("丙") && tiangan.includes("丁") //后面按照这个格式写就行 ){ return document.write(shishen[1]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("丙")){ return document.write(shishen[2]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("丁")){ return document.write(shishen[3]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("戊")){ return document.write(shishen[4]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("己")){ return document.write(shishen[5]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("庚")){ return document.write(shishen[6]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("辛")){ return document.write(shishen[7]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("壬")){ return document.write(shishen[8]+ "<br>") } else if (rizhu.includes("甲") && tiangan.includes("癸")){ return document.write(shishen[9]+ "<br>") } // 这样后面还有9个天干对比 这么写肯定不对,只是我不会写,如何用简单的代码 表达出来 else if (rizhu.includes("乙") && tiangan.includes("甲")){ return document.write(shishen[1]+ "<br>") } else{ return document.write("提交参数错误<br>") } } |
你的代码可以通过使用数组索引来简化,从而避免大量的 else if 语句。首先,你可以将天干和地支转换为数组索引,然后使用这些索引来访问 shishen 数组中的相应元素。以下是一个简化的版本: [JavaScript] 纯文本查看 复制代码 var shishen = ["比肩", "劫财", "食神", "伤官", "偏财", "正财", "七杀", "正官", "偏印", "正印"]; var tg = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]; function qshishen(rizhu, tiangan) { var rizhuIndex = tg.indexOf(rizhu); var tianganIndex = tg.indexOf(tiangan); if (rizhuIndex === -1 || tianganIndex === -1) { return document.write("提交参数错误<br>"); } var shishenIndex = (rizhuIndex + tianganIndex) % 10; // 使用模运算确保索引在0-9之间 return document.write(shishen[shishenIndex] + "<br>"); } 这个代码首先查找 rizhu 和 tiangan 在 tg 数组中的索引。如果它们不在数组中(即索引为 -1),则输出错误消息。否则,它将两个索引相加,并使用模运算确保结果在 0 到 9 之间,然后用这个索引来从 shishen 数组中获取相应的元素。 由文心大模型3.5生成 请自测 |
sitemap|
易语言源码|
易语言教程|
易语言论坛|
易语言模块|
手机版|
广告投放|
精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:[email protected]
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司
( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173