1
我被一些作業卡住了,我似乎無法找到我正在尋找的答案或將會工作的答案在我的情況。我試着做一個簡單的數學測驗,點擊提交後會檢查答案。我開始嘗試用jstl來完成這個循環,但似乎無法掌握如何使其工作。我目前的代碼是一團糟,我知道可以做得更好,但我正試圖在此時獲得一個可用的產品。任何幫助是極大的讚賞。JSP/HTML:嘗試將html輸入傳遞給具有相同變量名稱的多個對象以供使用
我現在正在嘗試將我的對象鏈接到我希望它們使用的輸入框,但是因爲我的變量名稱相同,所有其他名稱都被覆蓋。我試圖讓這個工作成功的時候,隔離了兩個數學問題。以下代碼非常糟糕,但是是我目前使用java和html的技能水平。
的Java bean:
public class MathGen {
private int a;
private int b;
private int c;
private String problem;
private String check;
public MathGen(){
//Generate random numbers a and b
int randA = (int) Math.round(Math.random()*100);
int randB = (int) Math.round(Math.random()*100);
if (randA < randB){
a = randB;
b = randA;
}
else {
a = randA;
b = randB;
}
this.problem = (a + " - " + b + " = ");
}
public String getProblem() {
return problem;
}
/**
* @param c the c to set
*/
public void setC(int c) {
this.c = c;
}
/**
* @return the check
*/
public String getCheck() {
if ((a - b) != c){
this.check = c + " is INCORRECT";
}
else {
this.check = c + " is CORRECT";
}
return check;
}
}
JSP頁面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<form action="answercheck.jsp" >
<jsp:useBean id="problem" scope="session" class="MyClass" />
<jsp:getProperty name="problem" property="problem"/>
<input type="text" name="c[]" value="" id="problem" size="2"/><br>
<jsp:useBean id="problem1" scope="session" class="MyClass" />
<jsp:getProperty name="problem1" property="problem"/>
<input type="text" name="c[]" value="" id="problem1" size="2"/><br>
<input type="submit" />
</form>
謝謝。我覺得很愚蠢,我錯過了它,現在意識到「參數」標籤對用戶輸入排序的重要性。 – Guardian452
我明白,如果您有更多的問題,請不要忘記回來並提出這個問題。 –