Java 使用扫描仪帮助输入 public int Remove(int i,公文包c[],字符串[]m){ int-nChoice=0; 布尔inputisok=false; while(inputisok==false){ System.out.print(“\t请删除“+i+”案例:”); nChoice=input.nextInt(); 如果(c[nChoice]==null | | nChoice=c.length){ System.out.println(); System.out.println(“\t有效输入请重试\n”); }否则{ System.out.println(“\tI'm”+m[nChoice] +“您刚刚删除了case#”+nChoice); System.out.println(“\t |”+nChoice+“|包含$” +c[nChoice].getAmount()+“\n”); inputisok=true; } } 返回nChoice; }
我这里的问题是,当我输入一个字母和一个负数,或者一个大于27的数字时,我总是会得到一个异常错误,我该如何修复它 以下行不正确:Java 使用扫描仪帮助输入 public int Remove(int i,公文包c[],字符串[]m){ int-nChoice=0; 布尔inputisok=false; while(inputisok==false){ System.out.print(“\t请删除“+i+”案例:”); nChoice=input.nextInt(); 如果(c[nChoice]==null | | nChoice=c.length){ System.out.println(); System.out.println(“\t有效输入请重试\n”); }否则{ System.out.println(“\tI'm”+m[nChoice] +“您刚刚删除了case#”+nChoice); System.out.println(“\t |”+nChoice+“|包含$” +c[nChoice].getAmount()+“\n”); inputisok=true; } } 返回nChoice; },java,exception,input,java.util.scanner,Java,Exception,Input,Java.util.scanner,我这里的问题是,当我输入一个字母和一个负数,或者一个大于27的数字时,我总是会得到一个异常错误,我该如何修复它 以下行不正确: public int Remove(int i, Briefcase c[], String[] m) { int nChoice = 0; boolean inputisok = false; while (inputisok == false) { System.out.print("\tP
public int Remove(int i, Briefcase c[], String[] m) {
int nChoice = 0;
boolean inputisok = false;
while (inputisok == false) {
System.out.print("\tPlease remove " + i + " cases: ");
nChoice = input.nextInt();
if (c[nChoice] == null || nChoice < 0 && nChoice >= c.length) {
System.out.println();
System.out.println("\tInvalid Input please Try again\n");
} else {
System.out.println("\tI'm " + m[nChoice]
+ " You just removed case # " + nChoice);
System.out.println("\t|" + nChoice + "| contains $"
+ c[nChoice].getAmount() + "\n");
inputisok = true;
}
}
return nChoice;
}
if(c[nChoice]==null | | nChoice<0&&nChoice>=c.length){
您想这样更改它:
if (c[nChoice] == null || nChoice < 0 && nChoice >= c.length) {
if(nChoice<0 | | nChoice>=c.length | | c[nChoice]==null){
有两个变化:(1)&&
变成了|
;(2)子句被重新排序
(1) &
是错误的,因为nChoice<0&&nChoice>=c.length
总是计算为false
,因为nChoice
不能同时小于零和大于c.length
(谢谢,@Aleks G!)
(2) 在原始版本中,您尝试访问c[nChoice]
,然后确保nChoice
在c
的范围内。如果不在范围内,这将导致ArrayIndexOutOfBoundsException
而不是打印出“无效输入”
这是条款顺序重要的原因
最后,在读取输入之前,您可以调用以确保下一个标记可以被解释为有效的整数。使用以下方法:
public int Remove(int i,公文包c[],字符串[]m){
布尔值isNextIntCorrect=false;
int输入;
而(!isNextIntCorrect){
System.out.println(“\t请删除“+i+”个案例:”);
Scanner inputScanner=新扫描仪(input.next());
if(inputScanner.hasNextInt()){
enteredit=inputScanner.nextInt();
isNextIntCorrect=enteredit>=0&&enteredit
通过这种方式,您肯定可以处理正确的int!我认为第一部分的计算结果永远不会为真:c.length
将始终大于或等于0;因此此if
语句的第一部分本质上意味着if(cChoice<0&&cChoice>=0)
-这永远是错误的
。我想他可能需要的是如果(nChoice<0 | | | nChoice>=c.length | | c[nChoice]==null)
好了,现在唯一的错误是当我输入一个负数和一个字母时,还有什么?
if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
public int Remove(int i, Briefcase c[], String[] m) {
boolean isNextIntCorrect = false;
int enteredInt;
while(!isNextIntCorrect){
System.out.println("\tPlease remove " + i + " cases: ");
Scanner inputScanner = new Scanner(input.next());
if(inputScanner.hasNextInt()){
enteredInt = inputScanner.nextInt();
isNextIntCorrect = enteredInt >= 0 && enteredInt < c.length
&& enteredInt < m.length)
}
inputScanner.close();
if(!isNextIntCorrect){
System.out.println("\tInvalid Input please Try again\n");
}
}
System.out.println("\tI'm " + m[enteredInt]
+ " You just removed case # " + enteredInt);
System.out.println("\t|" + enteredInt+ "| contains $"
+ c[enteredInt].getAmount() + "\n");
}