作者Sanbeishuu (三杯鼠)
看板java
標題[蟲?] 請問這個原始碼哪裡有問題?
時間Fri Nov 2 00:53:50 2007
原始碼
import java.util.Scanner;//class uses class Scanner
public class test
{
//main method begins
public static void main( String args[] )
{
//create scanner to obtain input
Scanner n;
n = new Scanner( System.in );
int n;//正方形邊長
System.out.print( "請輸入一個介於1到70的數值:" );//prompt
n = input.nextInt();//讀取使用者輸入的數值寫到變數n
if ( 1<=n<=70 )//如果變數n介於1到70
i = 1;//將1寫到變數i
while ( i<=n )//i小於n使用while loop
{
if ( i==1 || i==n )//如果i等於1或n
j = 1;//將1寫到變數j
while ( j<=n )
{
System.out.print( "─" );//輸出─
j = j + 1;//變數j加1
}
System.out.print( "\n" );//跳行
i = i + 1;
else
System.out.print( "│" );
j = 1;//將1寫到變數j
while ( j<=n-2 )
{
System.out.print( " " );
j = j + 1;//變數j加1
}
System.out.print( "│\n" );
i = i + 1;
}
else
System.out.print( "錯誤!! 輸入的數值未介於1到70" );
}
}
Microsoft Windows XP [版本 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>d:
D:\>javac test.java
test.java:40: 'else' without 'if'
else
^
test.java:41: <identifier> expected
System.out.print( "│" );
^
test.java:41: illegal start of type
System.out.print( "│" );
^
test.java:43: <identifier> expected
j = 1;//將1寫到變數j
^
test.java:45: illegal start of type
while ( j<=n-2 )
^
test.java:45: <identifier> expected
while ( j<=n-2 )
^
test.java:45: <identifier> expected
while ( j<=n-2 )
^
test.java:52: <identifier> expected
System.out.print( "│\n" );
^
test.java:52: illegal start of type
System.out.print( "│\n" );
^
test.java:54: <identifier> expected
i = i + 1;
^
test.java:57: class, interface, or enum expected
else
^
test.java:59: class, interface, or enum expected
}
^
12 errors
D:\>
compiler過不去 可是我也看不出來哪裡有問題T.T
麻煩各位大大幫我看看
感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.128.194.182
推 TuTu522:if else 請加括號: if{...} else{...} 才不易搞錯 11/02 00:56
推 shaldrem:if else使用上的問題吧 結構上就有問題了 11/02 01:01
推 Sanbeishuu:可是我剛剛把每個IF跟ELSE都加上 結果error更多了@@ 11/02 01:03
→ Sanbeishuu:請問S大說得結構上就有問題是指? 11/02 01:03
推 ton60316:if ( 1<=n<=70 ) 不是這樣寫XD 11/02 01:13
推 ton60316:結構上也有問題... 11/02 01:16
推 Sanbeishuu:啊...不可以這樣寫嗎? 那那個if怎麼寫呢? 11/02 01:19
推 Sanbeishuu:結構上? 我把{}算的好好的 有20error 少打一個} 11/02 01:21
→ Sanbeishuu:剩下一個error.... 這是啥咪情形.... 11/02 01:22
推 buganini:找個好一點的editor看吧 11/02 02:32
→ buganini:notepad++ or pspad 11/02 02:33
推 icespeech:應該寫if(n >= 1 && n <= 70) 11/02 04:00
推 Sanbeishuu:嗯嗯 OK了 編譯完成 運作也沒有問題了 感謝各位大大 11/02 10:58
→ Sanbeishuu:整個問題出在 前面沒有定義變數i j.... 11/02 10:58
→ Sanbeishuu:至於b大說的 教授規定先用記事本 恩 希望可以早點用 11/02 10:59
→ Sanbeishuu:eclipse 不然整個就頭很大XD 11/02 11:00
推 MonyemLi:notepad++ or pspad 也是記事本的一種,提供語法高亮度 11/02 21:55
推 slalala:雙n?????????? 11/04 12:28
推 PRAM:用記事本沒啥不好啊 =__= 11/04 16:20
推 Egriawei:emediter editplus也不錯啊,關鍵字有顏色差別的ide就夠了 11/04 21:48
→ Egriawei:我倒覺得簡單點的ide比較方便 11/04 21:49
推 devc:Scanner物件怎麼宣告為n卻又用input來呼叫方法? 11/14 00:18
→ devc:i跟j也都沒有宣告,怎麼使用? 11/14 00:20