public class ReadLine {
public static void main(String[] args) {
// TODO: Add your code here
byte buf[]=new byte[1024];
String strInfo=null;
int pos=0;
int ch=0;
System.out.println("请输入字符:");
while(true)
{
try
{
ch=System.in.read();
}
catch(Exception e)
{
e.getStackTrace();
}
switch(ch)
{
case '\r':
break;
case '\n':
strInfo=new String(buf,0,pos);
if(strInfo.equals("bye"))
return; else
System.out.println(strInfo);
pos=0;
break;
default:
buf[pos++]=(byte)ch;
}
}
}
}
这段代码很简单,键盘输入"bye"停止,输入其他的复制,其中的return,是返回什么值,是跳出哪里?问题比较弱智,希望大家不要笑
public static void main(String[] args) {
// TODO: Add your code here
byte buf[]=new byte[1024];
String strInfo=null;
int pos=0;
int ch=0;
System.out.println("请输入字符:");
while(true)
{
try
{
ch=System.in.read();
}
catch(Exception e)
{
e.getStackTrace();
}
switch(ch)
{
case '\r':
break;
case '\n':
strInfo=new String(buf,0,pos);
if(strInfo.equals("bye"))
return; else
System.out.println(strInfo);
pos=0;
break;
default:
buf[pos++]=(byte)ch;
}
}
}
}
这段代码很简单,键盘输入"bye"停止,输入其他的复制,其中的return,是返回什么值,是跳出哪里?问题比较弱智,希望大家不要笑
return就是退出当前函数,现在当前函数是main函数,所以就退出程序了。
你在code中return的话就退出程序(main方法)了,
如果你想退出当前循环,用break就好了。
我只知道c++里,那个控制台应用程序,main的返回值最好不写void,写成int的,程序正常退出就return 0;否则运行时有问题的话,最后控制台得到的返回值就不是0了。