请大家看看下面这个程序:
import java.io.*;
class TinyEdit{
public static void main (String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str[]=new String[100];
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop'to quit");
for (int i=0; i<100;i++)
{
str[i]=br.readLine();
if(str[i].trim().equals("stop"))
{ break;}
}
System.out.println("\nHere is your file:");
for(int i=0;i<100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}当我写了几行之后再写如STOP就发现程序不会终止了.然后我只要在str[i]=br.readLine();后写一句System.out.println(str[i]);程序运行就会变正常了,我不知道是什么原因啊,也许是那个"回车"符号的问题吧,请各位帮帮忙!
import java.io.*;
class TinyEdit{
public static void main (String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str[]=new String[100];
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop'to quit");
for (int i=0; i<100;i++)
{
str[i]=br.readLine();
if(str[i].trim().equals("stop"))
{ break;}
}
System.out.println("\nHere is your file:");
for(int i=0;i<100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}当我写了几行之后再写如STOP就发现程序不会终止了.然后我只要在str[i]=br.readLine();后写一句System.out.println(str[i]);程序运行就会变正常了,我不知道是什么原因啊,也许是那个"回车"符号的问题吧,请各位帮帮忙!
解决方案 »
- 如何编写以下测试类啊
- 我用java写的遗传算法的严重问题,程序没错,但不知道为什么结果总和预想的不同,实在是困惑,请帮忙啊!!!!(二)
- 关于socket接收XML后乱码问题(JAVA)
- 跪求Java俄罗斯方块游戏的程序
- 关于写程序的问题,大家帮我想想办法~ 谢谢~(UP都有分)
- java通过socket通讯发送excel文件到c#端,excel文件乱码
- 请教一个java的小问题!万分急!!!!!!!!!!!!!!!!!!!!
- 大家來看一下我的程序,我是用GridBagLayout的,但為什麼不行?指點指點~!
- 我要处理用户输入的一个字符窜。怎样写用户输入这段代码?
- 网络咖啡 希望你帮帮忙!
- 求正则表达式的一个问题。
- 大家给解释下桶排序
public static void main (String args[]) throws IOException{
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop 'to quit");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str[]=new String[100];
for (int i=0; i <100;i++)
{
str[i]=br.readLine();
if(str[i].trim().equals("stop"))
{ break;}
}
System.out.println("\nHere is your file:");
for(int i=0;i <100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}
问题出在顺序上,当上面一行在输出前面时,实际上输出已经被输入阻塞。
第二个循环是一个嵌套循环呀,i在外循环已经定义了;
内循环的作用是什么?
第二次判断起什么作用?你的意思是不是输入一个字符串,最后的是STOP是退出?
{
public static void main(String[] args) throws IOException
{
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop' to quit"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = new String[100]; for (int i =0;i<100 ;i++ )
{
str[i]=br.readLine();
if(str[i].trim().equals("stop"))
{
break;
}
//System.out.println("\nHere is your file:");
}
System.out.println("\nHere is your file:");
for (int i = 0;i<100 ; i++)
{
if (str[i].equals("stop"))break;
System.out.println(str[i]);
}
}
}
前加上if(!("stop".equals(str[0].trim())))即:if(!("stop".equals(str[0].trim())))
{
System.out.println("\nHere is your file:");
}
我在我机子里调试过了,你调试下看下还有什么问题!!
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = new String[100];
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop 'to quit");
for (int i = 0; i < 100; i++) {
str[i] = br.readLine();
//System.out.println("-----------");
if (str[i].trim().equals("stop")) {
break;
}
}
System.out.println("\nHere is your file:");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().equals("stop"))//加个trim() ?
break;
System.out.println(str[i]);
}
}
}