java 怎样处理一个用户输入的命令后,用户可以接着输入命令

我把用户的命令赋给变量instruction执行完后怎样可以继续输入命令,在执行一遍,我知道可以用循环,那怎样写循环条件,怎样写关于instruction的代码呢(exit是注销用户,quit是退出程序,其他还有很多命令就不写了)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Test.main(Test.java:18)
我在运行时出现了这个就是相当于
command = in nextLine();出现了问题为什么

import java.util.*;
import java.io.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String command = null;
do {
System.out.println("Please Enter Command :");
command = in.nextLine();
if (command.equals("logout")) {
System.out.println("Logged Out!");
} else if (command.equals("quit")) {
System.out.println("Program Quit!");
break;
} else {
System.out.println("Unknow Command : " + command);
}
} while (true);
}
}

 java.util.NoSuchElementException: No line found

出现这个异常的原因,一般是调用了in.close();

记住!永远不要对标准输入调用close();

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-21

类似你这种情况,要写死循环,

死循环并不可怕,只要有限定退出条件就可以了,下面是示例,

while(true) {//关键是这里,这里写死了true,他就一直会死循环下去


//在这里面写相应的逻辑,当达到什么条件的时候,break;让他跳出循环。


}


command = in nextLine();

你的这句代码怎么写成这个样子,in 应该是你的Scanner 对象的名称吧,你想调用nextLine方法要点出来啊,怎么是空格。

建议你把这段代码贴出来,我才好仔细看看,

而且错误信息说:on line found 

意思是没找到行。

追问

一部分代码,编译没问题

追答

。。。

第2个回答  2014-10-21
getchar(),是命令提示符停留

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网