Topic: 一个很低级的问题

  Print this page

1.一个很低级的问题 Copy to clipboard
Posted by: ice0819
Posted on: 2008-10-27 21:02

public class T
{
  static int aCount,bCount,cCount;
  static int buff[]=new int[]{aCount,bCount,cCount};
  public static void main(String args[])throws IOException
  {
  
  StringReader reader=new StringReader("abababbCABBCBCCCabbbbabaAAAcccccAbbbbababAAa");
  int re;
  while ((re=reader.read())!=-1)
    {
      char temp=(char)re;
      if(String.valueOf(temp).equalsIgnoreCase("a")){aCount++;}
      if(String.valueOf(temp).equalsIgnoreCase("b")){bCount++;}
      if(String.valueOf(temp).equalsIgnoreCase("c")){cCount++;}
      
    }
  reader.close();
  System.out.println(aCount);
  System.out.println(bCount);
  System.out.println(cCount);
  Arrays.sort(buff);
  for(int a:buff){System.out.println(a);}
  }
  
}

1
为什么打印结果前三行和后三行不一样呢?谢谢

2.Re:一个很低级的问题 [Re: ice0819] Copy to clipboard
Posted by: gbluo
Posted on: 2008-10-29 11:02

你没有把改变后的aCount,bCount,cCount的值赋给buff,可以加一点:
import java.io.*;
import java.util.*;

public class T
{
static int aCount,bCount,cCount;
static int buff[]=new int[]{aCount,bCount,cCount};
public static void main(String args[])throws IOException
{

StringReader reader=new StringReader("abababbCABBCBCCCabbbbabaAAAcccccAbbbbababAAa");
int re;
while ((re=reader.read())!=-1)
{
char temp=(char)re;
if(String.valueOf(temp).equalsIgnoreCase("a")){aCount++;}
if(String.valueOf(temp).equalsIgnoreCase("b")){bCount++;}
if(String.valueOf(temp).equalsIgnoreCase("c")){cCount++;}

}
reader.close();
System.out.println(aCount);
System.out.println(bCount);
System.out.println(cCount);
buff[0]=aCount;
buff[1]=bCount;
buff[2]=cCount;
Arrays.sort(buff);
for(int a:buff){System.out.printlnAngel;}
}

}
这样就会得到你要的结果.

3.Re:一个很低级的问题 [Re: ice0819] Copy to clipboard
Posted by: gbluo
Posted on: 2008-10-29 11:04

不过我也想知道有没有更好的方法,呵呵.

4.Re:一个很低级的问题 [Re: gbluo] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-11-11 22:12

gbluo wrote:
不过我也想知道有没有更好的方法,呵呵.

I think you can remove the buff array, due to the fact that it duplicates the aCount, bCount and cCount variables. There is always something agreed by rule of thumb. That is do *not* duplicate code.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923