A faulty Java prgm
2009-12-17 00:15
267 查看
import java.io.*;
import java.util.*;
public class JScrabble_con
{
public static void main(String[] args) throws IOException
{
String answer;
//String riddle;
int j;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a word to make a riddle: ");
answer = br.readLine();
StringBuffer sb = new StringBuffer(answer);
j=answer.length(); //for "razer", it return 4 or 5??
char riddle[] = new char[j];
Random rd = new Random();
for(j=answer.length();j>=1;j--)
{
//Generate a random index to retrieve.
int idx=rd.nextInt(j-1);
//Copy the character to riddle[]
riddle[j-1] = sb.charAt(idx);
sb.deleteCharAt(j-1);
}
System.out.println("The riddle is: " + riddle.toString());
for(int i=0; i<5; i++)
{
if(answer.equalsIgnoreCase(br.readLine()))
{
System.out.println("You are correct!");
return;
}
else
System.out.println("Oops, it's INCORRECT! Try again. ("+(i-1)+" chances left.)");
}
/*
if(answer.equalsIgnoreCase(br.readLine()))
{
System.out.print("Congratulations! You are CORRECT!");
}
else
return;
*/
}
}
I want this program to obtain a input and then shuffle the sequence of the letter and output as a riddle.
Then user must guess what is the original word.
But during the compilation, I got "java.lang.ArrayIndexOutOfBoundsException" error, but I can not find which code occur this error. Would you like to debug for me??
Thanks for anyone who replied!
import java.util.*;
public class JScrabble_con
{
public static void main(String[] args) throws IOException
{
String answer;
//String riddle;
int j;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a word to make a riddle: ");
answer = br.readLine();
StringBuffer sb = new StringBuffer(answer);
j=answer.length(); //for "razer", it return 4 or 5??
char riddle[] = new char[j];
Random rd = new Random();
for(j=answer.length();j>=1;j--)
{
//Generate a random index to retrieve.
int idx=rd.nextInt(j-1);
//Copy the character to riddle[]
riddle[j-1] = sb.charAt(idx);
sb.deleteCharAt(j-1);
}
System.out.println("The riddle is: " + riddle.toString());
for(int i=0; i<5; i++)
{
if(answer.equalsIgnoreCase(br.readLine()))
{
System.out.println("You are correct!");
return;
}
else
System.out.println("Oops, it's INCORRECT! Try again. ("+(i-1)+" chances left.)");
}
/*
if(answer.equalsIgnoreCase(br.readLine()))
{
System.out.print("Congratulations! You are CORRECT!");
}
else
return;
*/
}
}
I want this program to obtain a input and then shuffle the sequence of the letter and output as a riddle.
Then user must guess what is the original word.
But during the compilation, I got "java.lang.ArrayIndexOutOfBoundsException" error, but I can not find which code occur this error. Would you like to debug for me??
Thanks for anyone who replied!
相关文章推荐
- JAVA学习笔记(2)
- java jackson 将对象转json时,忽略子对象的某个属性
- 第一个Java程序
- Java 使用 Redis
- java web会话跟踪技术(深入理解HTTP Session与Cookie)
- Java基本类型与引用类型
- java终极面试题目总结
- webuploader 分片上传时 保存信息的java类
- java使用HttpClient发送http请求
- Java集合概念
- java设计模式(二)——抽象工厂模式
- 如何迅速成为Java高手-王维树
- Java学习之路:不走弯路,就是捷径
- java CMS垃圾回收日志解释
- JAVA
- Java开发者需坚守的十大基本准则
- Javayibi回调接口Demo
- Java关键字之native,strictfp,transient,volatile
- 用JAVA实现多线程(生产者与消费者问题)
- java反射原理