我這代碼是哪里錯了?
<script?type="text/javascript"> function?tcon(x,y){??//定義函數 ????var?x,y; ????if(x>y){ ????????document.write("x大于y"); ????????else?if(x=y){ ????????????document.write("x等于y"); ????????} ????????else{ ????????????document.write("x小于y"); ????????} ????} //函數體,判斷兩個整數比較的三種情況 } //調用函數,實現下面兩組數中,返回較大值。 ??document.write("?5?和?4?的較大值是:"+tcon(5,4)+"<br>"); ??document.write("?6?和?3?的較大值是:"+tcon(6,3)?);? </script>
2016-11-23
if(x>y){
????????document.write("x大于y");
這段代碼后面少了一個結尾大括號; ?最底下多了一個結尾大括號;
另 ?比較的結果不能以document.write()輸出, 這里要用返回值 ?return ?,因為你下面用了document.write()輸出;
如果上面你用document.write()輸出; ?下面就直接調用方法傳遞參數就好了,如:tcon(5,4);
<script type="text/javascript">
function tcon(x,y){ ?//定義函數
? ? var x,y;
? ? if(x>y){
? ? ? ? return "x大于y";
}
? ? ? ? else if(x=y){
? ? ? ? ? ? return "x等于y";
? ? ? ? }
? ? ? ? else{
? ? ? ? ? ? return "x小于y";
? ? ? ? }
? ? }
?
//函數體,判斷兩個整數比較的三種情況
?
?
?
//調用函數,實現下面兩組數中,返回較大值。
? document.write(" 5 和 4 的較大值是:"+tcon(5,4)+"<br>");
? document.write(" 6 和 3 的較大值是:"+tcon(6,3) );?
?
?
?
?
</script>
2016-11-23
if...else 格式錯了,正確如下
if(x>y){
????????document.write("x大于y");
? }else?if(x=y){
????????????document.write("x等于y");
? }else{
????????????document.write("x小于y");
?}