不好意思,希望的结果是: who is your friend? 随便写个string, you mean your friend is + 上面写的string Are you sure? 随便写 tThen you must be in trouble!各位可以试运行一下,就会发现问题了。
楼主的程序写的太难看了! import java.io.IOException;public class JustTest{ public static void main(String args[]){ try{ System.out.println("Who is your friend?"); char ch=(char)System.in.read(); while(ch!='\n') ch=(char)System.in.read(); StringBuffer s=new StringBuffer(); while((ch=(char)System.in.read())!='\n') s.append(ch); System.out.println("You mean your friend is "+s); System.out.println("Are you sure?"); StringBuffer a=new StringBuffer(); while((ch=(char)System.in.read())!='\n') a.append(ch); System.out.println("Then you must be in trouble!"); } catch(IOException e){ System.out.println("It is impossible!"); } } } 没有问题!我测试了!
没错,刚开始写程序吗,以后会注意的,不过刚才我也修改了一下,原来的问题解决了,有出了一个新的问题,希望打下看一下: import java.io.IOException;class JustTest{ public static void main(String args[]){ try{ System.out.println("Who is your friend?"); char ch=(char)System.in.read(); StringBuffer s=new StringBuffer(); while((ch=(char)System.in.read())!='\n') s.append(ch); StringBuffer w=s.append(ch); System.out.println("You mean your friend is "+w+"?"); System.out.println("Are you sure?"); StringBuffer a=new StringBuffer(); while((ch=(char)System.in.read())!='\n') a.append(ch); System.out.println("Then you must be in trouble!"); } catch(IOException e){ System.out.println("It is impossible!"); } } } 运行一下就会发现有问题,还希望帮忙解决一下,谢谢!
System.out.println("Who is your friend?"); char ch=(char)System.in.read(); //很明显,你弄丢了这个字符 StringBuffer s=new StringBuffer();
多谢几位,我又把程序改了一下:import java.io.IOException;class JustTest{ public static void main(String args[]){ try{ System.out.println("Who is your girlfriend?"); char ch; //=(char)System.in.read(); StringBuffer s=new StringBuffer(); while((ch=(char)System.in.read())!='\n') s.append(ch); StringBuffer w=s.append(ch); System.out.println("You mean your girlfriend is "+w+"?");//为什么问号会在下一行? System.out.println("Are you sure?"); StringBuffer a=new StringBuffer(); while((ch=(char)System.in.read())!='\n') a.append(ch); System.out.println("Then you must be in trouble!"); } catch(IOException e){ System.out.println("It is impossible!"); } } } 但是为什么会出现这个问题,请解释一下好吗?
具体你要实现什么我也不太清楚???
who is your friend?
随便写个string,
you mean your friend is + 上面写的string
Are you sure?
随便写
tThen you must be in trouble!各位可以试运行一下,就会发现问题了。
import java.io.IOException;public class JustTest{
public static void main(String args[]){
try{
System.out.println("Who is your friend?");
char ch=(char)System.in.read();
while(ch!='\n')
ch=(char)System.in.read();
StringBuffer s=new StringBuffer();
while((ch=(char)System.in.read())!='\n')
s.append(ch);
System.out.println("You mean your friend is "+s);
System.out.println("Are you sure?");
StringBuffer a=new StringBuffer();
while((ch=(char)System.in.read())!='\n')
a.append(ch);
System.out.println("Then you must be in trouble!");
}
catch(IOException e){
System.out.println("It is impossible!");
}
}
}
没有问题!我测试了!
import java.io.IOException;class JustTest{
public static void main(String args[]){
try{
System.out.println("Who is your friend?");
char ch=(char)System.in.read();
StringBuffer s=new StringBuffer();
while((ch=(char)System.in.read())!='\n')
s.append(ch);
StringBuffer w=s.append(ch);
System.out.println("You mean your friend is "+w+"?");
System.out.println("Are you sure?");
StringBuffer a=new StringBuffer();
while((ch=(char)System.in.read())!='\n')
a.append(ch);
System.out.println("Then you must be in trouble!");
}
catch(IOException e){
System.out.println("It is impossible!");
}
}
}
运行一下就会发现有问题,还希望帮忙解决一下,谢谢!
char ch=(char)System.in.read(); //很明显,你弄丢了这个字符
StringBuffer s=new StringBuffer();
public static void main(String args[]){
try{
System.out.println("Who is your girlfriend?");
char ch; //=(char)System.in.read();
StringBuffer s=new StringBuffer();
while((ch=(char)System.in.read())!='\n')
s.append(ch);
StringBuffer w=s.append(ch);
System.out.println("You mean your girlfriend is "+w+"?");//为什么问号会在下一行?
System.out.println("Are you sure?");
StringBuffer a=new StringBuffer();
while((ch=(char)System.in.read())!='\n')
a.append(ch);
System.out.println("Then you must be in trouble!");
}
catch(IOException e){
System.out.println("It is impossible!");
}
}
}
但是为什么会出现这个问题,请解释一下好吗?