标题:如何更正程序
只看楼主
wangping274
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-7-26
 问题点数:0 回复次数:8 
如何更正程序

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

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 
2007-07-26 16:55
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
得分:0 
回复:(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));
}

}


广告位招租
2007-07-26 17:25
wangping274
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-7-26
得分:0 
谢谢,帮助
但我还是不明白,为什么这样加这个
private static RandomCharacter myCharacter = new RandomCharacter();

还要这样在这一行上
2007-07-26 17:35
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
得分:0 
你的原来的myCharacter作用域仅仅是main方法里面

广告位招租
2007-07-26 17:38
yxh
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-7-26
得分:0 

加上public static 就得了

2007-07-26 17:41
wangping274
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-7-26
得分:0 

谢谢,四楼楼主,但看下下面的程序为什么又能这样写在里面
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;
}
}
   请说详细点好吗?谢谢

2007-07-26 17:51
myfor
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:357
专家分:6
注 册:2006-3-13
得分:0 
myCircle 并未在main方法之外被引用

广告位招租
2007-07-26 17:53
wangping274
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-7-26
得分:0 
喔,我明白了,谢谢大家啊,还有有时候自己太粗心大意了的,没有看清楚
2007-07-26 17:56
happyniannia
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-7-26
得分:0 

我也明白了

2007-07-26 18:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-158350-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.094136 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved