当前位置: 代码迷 >> Java相关 >> 如何更正程序
  详细解决方案

如何更正程序

热度:98   发布时间:2007-07-26 16:55:33.0
如何更正程序

各位大虾们,大家好,小弟我刚来论坛,还是菜鸟,望指点下面的程序,到底是什么地方出错,是什么问题(红色的地方无法通过调试),先谢了

import java.lang.Math;
public class Wang3
{
public static void main(String[] args)
{ RandomCharacter myCharacter = new RandomCharacter();
char chars[] = createArray();
System.out.println("The lowercase letters are:");
displayArray(chars);
int[] counts = countLetters(chars);
System.out.println();
System.out.println("The occurrences of each letter are:");
displayCounts(counts);
}
public static char[] createArray()
{ char[] chars = new char[100];
for(int i = 0; i < chars.length; i++)
chars[i] = myCharacter.getRandomCharacter();
return chars;
}
public static void displayArray(char[] chars)
{ for(int i = 0; i < chars.length; i++)
{if((i + 1) % 20 == 0)
System.out.println(chars[i] + " ");
else
System.out.print(chars[i] + " ");
}
}
public static int[] countLetters(char[] chars)
{ int[] counts = new int[26];
for(int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;
}
public static void displayCounts(int[] counts)
{ for(int i = 0; i < counts.length; i++)
{ if((i + 1) % 10 == 0 )
System.out.println(counts[i] + " " + (char)(i+'a'));
else
System.out.print(counts[i] + " " + (char)(i+'a') + " ");
}
}

}


class RandomCharacter
{ char getRandomCharacter()
{ char ch1 = 'a';
char ch2 = 'z';
return (char)(ch1 + Math.random()*(ch2 - ch1 + 1));
}

}

搜索更多相关的解决方案: void  public  import  

----------------解决方案--------------------------------------------------------
回复:(wangping274)如何更正程序

import java.lang.Math;
public class Wang3
{
private static RandomCharacter myCharacter = new RandomCharacter();
public static void main(String[] args)
{
char chars[] = createArray();
System.out.println("The lowercase letters are:");
displayArray(chars);
int[] counts = countLetters(chars);
System.out.println();
System.out.println("The occurrences of each letter are:");
displayCounts(counts);
}
public static char[] createArray()
{ char[] chars = new char[100];
for(int i = 0; i < chars.length; i++)
chars[i] = myCharacter.getRandomCharacter();
return chars;
}
public static void displayArray(char[] chars)
{ for(int i = 0; i < chars.length; i++)
{if((i + 1) % 20 == 0)
System.out.println(chars[i] + " ");
else
System.out.print(chars[i] + " ");
}
}
public static int[] countLetters(char[] chars)
{ int[] counts = new int[26];
for(int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;
}
public static void displayCounts(int[] counts)
{ for(int i = 0; i < counts.length; i++)
{ if((i + 1) % 10 == 0 )
System.out.println(counts[i] + " " + (char)(i+'a'));
else
System.out.print(counts[i] + " " + (char)(i+'a') + " ");
}
}

}


class RandomCharacter
{ char getRandomCharacter()
{ char ch1 = 'a';
char ch2 = 'z';
return (char)(ch1 + Math.random()*(ch2 - ch1 + 1));
}

}


----------------解决方案--------------------------------------------------------
谢谢,帮助
但我还是不明白,为什么这样加这个
private static RandomCharacter myCharacter = new RandomCharacter();

还要这样在这一行上

----------------解决方案--------------------------------------------------------
你的原来的myCharacter作用域仅仅是main方法里面
----------------解决方案--------------------------------------------------------

加上public static 就得了


----------------解决方案--------------------------------------------------------

谢谢,四楼楼主,但看下下面的程序为什么又能这样写在里面
public class TestSimpleCircle
{
public static void main(String[] args)
{
SimpleCircle myCircle = new SimpleCircle(5.0);
System.out.println("The area of the circle of radius " + myCircle.radius + " is " +

myCircle.findArea());
}
}
class SimpleCircle
{
double radius;
SimpleCircle(double newRadius)
{
radius = newRadius;
}
double findArea()
{
return radius * radius * 3.14159;
}
}
   请说详细点好吗?谢谢


----------------解决方案--------------------------------------------------------
myCircle 并未在main方法之外被引用
----------------解决方案--------------------------------------------------------
喔,我明白了,谢谢大家啊,还有有时候自己太粗心大意了的,没有看清楚
----------------解决方案--------------------------------------------------------

我也明白了


----------------解决方案--------------------------------------------------------
  相关解决方案