请大家看看下面这个程序:
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]);程序运行就会变正常了,我不知道是什么原因啊,也许是那个"回车"符号的问题吧,请各位帮帮忙!
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]);
}
}
}