나는 현재이 프로젝트에서 어려움을 겪고 있습니다. 온라인에서 다양한 검색을 통해 어디로 가는지 알지 못하기 때문에 다른 답변을 얻으므로 현재 가지고있는 코드가 거의 정확하지 않습니다. 곱셈표는 1-10이고 곱셈 답은 최대 5 개까지 표시됩니다. (1x1 = 1, 1x2 = 2, 의미가있는 경우 최대 5x10까지) 올바른 코드 예제가 필요합니다.
<!DOCTYPE html>
<html>
<head>
<title>Multiplication Tables</title>
</head>
<body>
<p>Enter a number below to see its multiplication table.</p>
<input type="text" name="txtNumber" id="txtNumber" placeholder="Enter a number" />
<br />
<br />
<button onclick="DisplayTable();">Click Me</button>
<div id="multiTable"></div>
<script language="javascript">
function DisplayTable(){
for (var i = 1; i <= 10; i++){
for (var i = 1, var j = 0; (i+j) <= 10; i++, j += i) {
System.out.print("\t"+i*j);
System.out.println();
}
}
}
</script>
</body>
</html>
1) 자바 스크립트 삽입 / 포함시 , 후자는 표준 코드가 아니므로 type="text/javascript"
대신 사용하십시오 language="javascript"
.
2) 이미 주석에서 언급했듯이 var i = 1
두 번째 for 루프 ( )에서 첫 번째 for 루프 ( ) 의 카운터를 재정의하려고합니다 var i = 1, var j = 0
. i
외부 for
루프와 j
내부 / 두 번째 for
루프 에만 카운터 를 사용하는 것이 좋습니다 . ( i
카운터는 이미 두 루프 모두에서 사용할 수 있으며 여기서 재설정하지 않으려 고합니다.)
3) System.out.print
및 System.out.printLn
자바 스크립트 기능하지 않습니다. Javascript에 해당하는 것은 document.write("Some text");
. 그러나이 함수는 페이지가로드 된 후 호출 할 때 모든 HTML을 지우므로 권장되지 않습니다 (이 경우). 따라서 대신 Javascript 함수에서 생성 된 HTML 코드를 저장하는 문자열 변수를 사용하십시오. 그런 다음 Javascript 함수가 루프의 모든 반복에서 새 테이블 행 / 셀을 추가하도록합니다 for
.
4) HTML로 표를 만들 때를 사용하지 말고 \t
대신 <table>
표 행 ( <tr>
) 및 표 셀 ( <td>
) 과 함께 적절한 태그 를 사용하십시오 .
5) 하나의 input
필드로 무엇을하고 싶은지 불분명합니다 . input
사용자가 곱셈 (행과 열)으로 시작하고 끝낼 위치를 선택할 수 있도록 's 를 사용하고 싶다고 생각했습니다 . 이 경우 4 개의 input
필드 (고유 ID 포함)가 필요합니다. 이 4 개의 값은 자바 스크립트 함수의 시작 부분에서 가져 와서 for
루프의 시작점과 끝점으로 사용합니다 .
예:
function DisplayTable(){
/* first, get the values of the input fields and parse them as integers */
var colStart = parseInt(document.getElementById("startCol").value);
var colEnd = parseInt(document.getElementById("endCol").value);
var rowStart = parseInt(document.getElementById("startRow").value);
var rowEnd = parseInt(document.getElementById("endRow").value);
/* create a buffer variable for the new html code & put a opening table tag in it (with a border for clearity) */
var htmlBuffer = "<table border='1'>";
for (var i = rowStart; i <= rowEnd; i++){
/* add a opening table row tag to the buffer */
htmlBuffer += "<tr>";
for (var j = colStart; j <= colEnd; j++) {
/* add a table cell with the multiplication inside it to the buffer */
htmlBuffer += "<td>"+ (i*j) + "</td>";
}
/* add a closing table row tag to the buffer */
htmlBuffer += "</tr>";
}
/* add a closing table tag to the buffer */
htmlBuffer += "</table>";
/* print/put generated html code inside the DIV element */
document.getElementById("multiTable").innerHTML = htmlBuffer;
}
<p>Fill in the numbers below and click "Generate table" to see their multiplication table.</p>
Start columns with:<br />
<input type="text" name="startCol" id="startCol" placeholder="Enter a number" value="1" /><br />
End columns with:<br />
<input type="text" name="endCol" id="endCol" placeholder="Enter a number" value="20" /><br />
<br />
Start rows with:<br />
<input type="text" name="startRow" id="startRow" placeholder="Enter a number" value="1" /><br />
End rows with:<br />
<input type="text" name="endRow" id="endRow" placeholder="Enter a number" value="10" /><br />
<br />
<br />
<button onclick="DisplayTable();">Generate table</button>
<br />
<div id="multiTable"></div>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다