能帮我的Java小程序差错吗 求高手帮忙
publicclassCountLettersInArray{/**Mainmethod*/publicstaticvoidmain(Stringargs[]){//De...
public class CountLettersInArray {
/**Main method*/
public static void main(String args[]){
//Declare and create an array
char[] chars = createArray();
//Display the array
System.out.println("The lowercase letters are:");
displayArray(chars);
//Count the occurrences of each letter
int[] counts = countLetters(chars);
//Display counts
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] = RandomCharacter.getRandomLowerCaseLetter();
return chars;
}
/**Create an array of characters*/
public static void displayArray(char[] chars){
//Display the characters in the array 20 on each line
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){
//Declare and create an array of 26 int
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') + "");
}
}
}
运行后报错 错误是这样的:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
RandomCharacter cannot be resolved
at CountLettersInArray.createArray(CountLettersInArray.java:20)
at CountLettersInArray.main(CountLettersInArray.java:6)
我想问一下 是不是Random包含在那个包
没有导入
我是想随机输入100个字符 在统计
帮我查一下 帮我运行起来 谢谢
我不太清楚 能帮我运行起来吗 Random这里报错了 我也不知道怎么回事 展开
/**Main method*/
public static void main(String args[]){
//Declare and create an array
char[] chars = createArray();
//Display the array
System.out.println("The lowercase letters are:");
displayArray(chars);
//Count the occurrences of each letter
int[] counts = countLetters(chars);
//Display counts
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] = RandomCharacter.getRandomLowerCaseLetter();
return chars;
}
/**Create an array of characters*/
public static void displayArray(char[] chars){
//Display the characters in the array 20 on each line
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){
//Declare and create an array of 26 int
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') + "");
}
}
}
运行后报错 错误是这样的:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
RandomCharacter cannot be resolved
at CountLettersInArray.createArray(CountLettersInArray.java:20)
at CountLettersInArray.main(CountLettersInArray.java:6)
我想问一下 是不是Random包含在那个包
没有导入
我是想随机输入100个字符 在统计
帮我查一下 帮我运行起来 谢谢
我不太清楚 能帮我运行起来吗 Random这里报错了 我也不知道怎么回事 展开
3个回答
2010-09-20
展开全部
不知道这是不是你需要的:
import java.util.Random;
public class CharPro {
public static void main(String[] args) {
Counter counter=new Counter();
char[] chars=counter.createArray(100);
System.out.println("The lowercase letters are:");
counter.displsyArray(chars);
int[] counts = counter.countLetters(chars);
System.out.println("The occurrences of each letter are:");
counter.displayCounts(counts);
}
}
class Counter {
public char[] createArray(int count) {
char[] chars = new char[count];
Random ran = new Random();
for (int i = 0; i < chars.length; i++)
chars[i] = (char) ('a' + ran.nextFloat() * 25);
return chars;
}
public void displsyArray(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] + " ");
}
System.out.println("");
}
public int[] countLetters(char[] chars)
{
int [] count= new int[26];
for(int i=0;i<chars.length;i++)
{
count[chars[i]-'a']++;
}
return count;
}
public void displayCounts(int[] count)
{
for(int i=0;i<count.length;i++)
{
if((i+1)%10==0)
{System.out.println(""+count[i]+(char)('a'+i));}
else
{System.out.print(""+count[i]+(char)('a'+i)+" ,");}
}
System.out.println("");
}
}
板式可能有点乱,复制一下Ctrl+Shift+f自动排版。
import java.util.Random;
public class CharPro {
public static void main(String[] args) {
Counter counter=new Counter();
char[] chars=counter.createArray(100);
System.out.println("The lowercase letters are:");
counter.displsyArray(chars);
int[] counts = counter.countLetters(chars);
System.out.println("The occurrences of each letter are:");
counter.displayCounts(counts);
}
}
class Counter {
public char[] createArray(int count) {
char[] chars = new char[count];
Random ran = new Random();
for (int i = 0; i < chars.length; i++)
chars[i] = (char) ('a' + ran.nextFloat() * 25);
return chars;
}
public void displsyArray(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] + " ");
}
System.out.println("");
}
public int[] countLetters(char[] chars)
{
int [] count= new int[26];
for(int i=0;i<chars.length;i++)
{
count[chars[i]-'a']++;
}
return count;
}
public void displayCounts(int[] count)
{
for(int i=0;i<count.length;i++)
{
if((i+1)%10==0)
{System.out.println(""+count[i]+(char)('a'+i));}
else
{System.out.print(""+count[i]+(char)('a'+i)+" ,");}
}
System.out.println("");
}
}
板式可能有点乱,复制一下Ctrl+Shift+f自动排版。
展开全部
RandomCharacter,这个类是什么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我差了下API文档,发现里面根本就没有RandomCharacter这个类,你这个程序估计是从网上或书上抄过去的,但是没有抄全。
你要再自己定义一个RandomCharacter类,里面包含一个返回char类型的getRandomLowerCaseLetter,按照你上面的写法,它必须是静态的,其实我看了下,这个类要完成的功能无非就是随机产生一个字母,目前我也不知道有没有直接完成这个功能的类,但是我大概写了,
class RandomCharacter
{ public static char[] getRandomLowerCaseLetter()
{ Random random=new Random();
int intChar=random.next(137)+65;
char[] charArray=Character.toCharrrs(intChar);
}
/*接着你可以在这里验证一下看下能不能随机产生一个字母*/
}
你要再自己定义一个RandomCharacter类,里面包含一个返回char类型的getRandomLowerCaseLetter,按照你上面的写法,它必须是静态的,其实我看了下,这个类要完成的功能无非就是随机产生一个字母,目前我也不知道有没有直接完成这个功能的类,但是我大概写了,
class RandomCharacter
{ public static char[] getRandomLowerCaseLetter()
{ Random random=new Random();
int intChar=random.next(137)+65;
char[] charArray=Character.toCharrrs(intChar);
}
/*接着你可以在这里验证一下看下能不能随机产生一个字母*/
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询