我正在寻找一些更好的解决方案。我正在为我的学校项目做一个匹配的单词游戏,但我有一些问题。而不是这种代码风格,我希望我的值从sql数据库中随机显示,但不重复相同的单词。我尝试了一些方法,但是不起作用,所以我将感谢你的任何帮助。
这是我的小游戏的截图:when i click start words will be open, and the game will start...
我的代码是:
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%! String[] wordsleft ={"Belgrade","Zagreb","Sarajevo", "Washington", "Paris" };
%>
<%! String[] wordsright ={"Serbia","Bosnia","Croatia","France","USA"};
%>
<%!
String printleft(){
Random rand = new Random();
int a = rand.nextInt(4 - 0 + 1) + 0;
return String.valueOf(wordsleft[a]);
} %>
<%!
String printright(){
Random rand = new Random();
int a = rand.nextInt(4 - 0 + 1) + 0;
return String.valueOf(wordsright[a]);
} %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="cssstyle.css" rel="stylesheet">
<title>Matching Words</title>
</head>
<body>
<h1 id="welcome">Welcome!</h1>
<p class="tekst">Spojnica je igra sa rečima. <br><br>U dve kolone je prikazano po 5 reči.
Svaka reč u levoj koloni ima svoj par u desnoj koloni. Za svaki spojen par dobijate 3 poena.
</p>
<div id="container">
<div class="containerlevo">
<input type="submit" class="leftword" name="prvo" id="firstleft" value="?" />
<input type="submit" class="leftword" name="drugo" id="secondleft" value="?" />
<input type="submit" class="leftword" name="trece" id="thirdleft" value="?" />
<input type="submit" class="leftword" name="trece" id="fourthleft" value="?" />
<input type="submit" class="leftword" name="trece" id="fifthleft" value="?" />
</div>
<div class="containerdesno">
<input type="submit" class="rightword" name="prvo" id="firstright" value="?" />
<input type="submit" class="rightword" name="drugo" id="secondright" value="?" />
<input type="submit" class="rightword" name="trece" id="thirdright" value="?" />
<input type="submit" class="rightword" name="drugo" id="fourthright" value="?" />
<input type="submit" class="rightword" name="trece" id="fifthright" value="?" />
</div>
<form action="Rezultat" method="post" >
<input class="button start" type="button" id="start" onclick = "printright(); printleft();" value="START" name="start"/>
<input class="button" type="submit" id="end" value="END" name="end"/>
</form>
</div>
</body>
</html>
<script>
function printleft(){
document.getElementById("firstleft").value = "<%= printleft() %>";
document.getElementById("secondleft").value = "<%= printleft() %>";
document.getElementById("thirdleft").value = "<%= printleft() %>";
document.getElementById("fourthleft").value = "<%= printleft() %>";
document.getElementById("fifthleft").value = "<%= printleft() %>";
document.getElementById("start").disabled = true;
}
function printright(){
document.getElementById("firstright").value = "<%= printright() %>";
document.getElementById("secondright").value = "<%= printright() %>";
document.getElementById("thirdright").value = "<%= printright() %>";
document.getElementById("fourthright").value = "<%= printright() %>";
document.getElementById("fifthright").value = "<%= printright() %>";
document.getElementById("start").disabled = true;
}
发布于 2016-01-08 09:21:12
在名字上使用数据库需要添加和学习很多东西。也许你想从名字开始--文件--这似乎更容易开始。
不管怎样。首先,您需要创建一个数据库,根据所使用的jdbc驱动程序,您将创建到数据库的jdbc连接字符串(这里是mysql)。
String connurl = "jdbc:mysql://localhost:3306/riddle?profileSQL=true";然后,您需要在应用程序中打开需要加载驱动程序类一次,通常是在init()中,甚至更好地作为上下文侦听器。由于您正在使用JDBC,因此最好在应用程序部署描述符中定义一个数据源。
描述这方面的所有细节显然超出了这里的范围,而且肯定在其他地方写得更好。您可以在这里找到许多示例: Netbeans在https://netbeans.org/kb/docs/web/mysql-webapp.html中提供了一个教程来说明这一点
然后,您应该从一个提供单词列表的类开始,即一个模型类。模型类将隐藏从JSP页面获取的所有数据。
这就是您应该从现在开始的地方:从JSP中删除所有函数,并将数据处理放在一个模型类中。然后在JSP中使用该模型类来提供数组。
完成后,您只需将模型更改为从文件读取,而不是从常量数组读取。如果你能正常工作,那就去处理数据库吧。
https://stackoverflow.com/questions/34667805
复制相似问题