2014-10-10 47 views
0

所以基本上我正在做一個java教程,但爲了跟着我需要做一個類文件。一切都已經給了我,但它給了我一個錯誤。錯誤是:刪除else語句或沿着這些行的東西。但是,當我刪除它告訴我把另一個else語句在那裏。 代碼是:這是一個與日食問題或有問題的代碼?刪除else語句,但需要其他語句

import java.io.*; 
import java.text.*; 
public class In 
{ 
static InputStreamReader r=new InputStreamReader(System.in); 
static BufferedReader br=new BufferedReader(r); 
// Read a String from the standard system input 
public static String getString() 
{ 
try 
{ 
return br.readLine(); 
} 
catch(Exception e) 
{ 
return ""; 
} 
} 
// Read a number as a String from the standard system input 
// and return the number 
public static Number getNumber() 
{ 
String numberString = getString(); 
try 
{ 
numberString = numberString.trim().toUpperCase(); 
return NumberFormat.getInstance().parse(numberString); 
} 
catch(Exception e) 

{ 
// if any exception occurs, just return zero 
return new Integer(0); 
} 
} 
// Read an int from the standard system input 
public static int getInt() 
{ 
return getNumber().intValue(); 
} 
// Read a long from the standard system input 
public static long getLong() 
{ 
return getNumber().longValue(); 
} 
// Read a float from the standard system input 
public static float getFloat() 
{ 
return getNumber().floatValue(); 
} 
// Read a double from the standard system input 
public static double getDouble() 
{ 
return getNumber().doubleValue(); 
} 
// Read a char from the standard system input 
public static char getChar() 
{ 
String s = getString(); 
if (s.length() >= 1) 
return s.charAt(0); 
else 
return ’\n’; 
} 
} 
+6

您也有縮進問題。首先考慮關注該部分(這會讓你看到/防止其他可能的問題)。 – Pshemo 2014-10-10 17:24:10

+1

很確定這是代碼問題。 – 2014-10-10 17:24:38

+0

@Pshemo是什麼導致問題?因爲這只是從教科書中複製並粘貼的。...... – Liste1134 2014-10-10 17:25:04

回答

1

什麼是真正造成這裏的錯誤是,無論搞砸了標記你身邊\ n不撇號......我不知道它們是什麼。正是重寫代碼像你一樣,除了與撇號後(加上if和else語句中使用大括號,因爲我喜歡你這樣),沒有任何錯誤:

public static char getChar() 
{ 
    String s = getString(); 
    if (s.length() >= 1){ 
     return s.charAt(0); 
    }else{ 
     return '\n'; 
    } 
} 

請,在未來,使確保在您的問題中使用正確的縮進以使我們更容易閱讀。

+0

這是正確答案。 (也就是說,這正是我給出的答案:) :) – 2014-10-10 18:04:48

+0

但是OP提到了'\ n',它是* a ** char **,換行符是精確的。爲什麼你建議從** char ** s改爲'String's? – azurefrog 2014-10-10 19:09:31

+0

啊,我很抱歉。我沒有看到他在使用轉義字符。我更新了我的回覆。 – Nerdizzle 2014-10-10 21:35:55