2012-02-07 148 views
3
try { 


    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter the continent;"); 
    String CN = in.readLine(); 
    String MaxDate="1"; 
    for(Earthquakerecd e : eqList) 
    { 
     if(e.getContinent().equals("CN")) 
     { 
      MaxDate=e.getDate(); 
     } 

     { 
      System.out.println(e.toString()); 
     } 


    } 

    System.out.println(MaxDate); 
    } 

    catch (IOException e) 
    { 
     System.out.println("IOException has been caught"); 
    } 

这是一个我认为很简单的问题。在这个问题中,Maxdate被声明为1.CN是大陆的字符串。如果用户输入与欧洲大陆相匹配,那么日期应该从e.getDate()传递给Maxdate。在任何情况下,我们都不应该得到输出为1,它应该是来自对象e的某个日期。 Maxdate总是得到1。任何可能的解我的语法正确吗?将对象字符串的值传递给字符串

+0

你试过MAXDATE = e.getDate()的toString ()?我不懂Java,但值得一拍 – ComputerSaysNo 2012-02-07 23:05:04

+0

对象e中的日期属性是一个字符串顺便说一句 – 2012-02-07 23:06:05

+0

为什么MaxDate是一个字符串?使它相同的类e.getDate()返回...此外,顺便说一句,Java约定是启动小写的变量,以及大写的类。 – m0skit0 2012-02-07 23:06:56

回答

7

好像你想:

if(e.getContinent().equals(CN)) 

现在你对字符串 “CN” 比较。对于任何事情你都没有使用变量CN

我假设你打算在if声明后插入else

另外,在Java中,不用大写字母来启动变量名是很常见的(将字符串命名为cn而不是CN)。

1

尝试做这种方式:

try { 


    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter the continent;"); 
    String CN = in.readLine(); 
    String MaxDate="1"; 
    for(Earthquakerecd e : eqList) 
    { 
     if(e.getContinent().equals(CN)) //When you put it with "" the continent value was compared to CN and not from the user input 
     { 
      MaxDate=e.getDate(); 
     } 

     { 
      System.out.println(e.toString()); 
     } 


    } 

    System.out.println(MaxDate); 
    } 

    catch (IOException e) 
    { 
     System.out.println("IOException has been caught"); 
    } 

如果不工作,然后通过这种方式做了尝试:

for (int i=0; i<eqList.size(); i++) { 
    Earthquakerecd e = eqList.get(i); 
    //... code ... // 
}