我是一个新的人类学习代码!
我的扫描仪出了问题,我需要它来重置'在一个无效的字符上。
我的代码:
describe("my text with Spies", function () {
it("should be altered", function () {
const fixture = TestBed.createComponent(AppComponent);
const app = this.fixture.debugElement.componentInstance;
spyOn(app, 'changeText');
expect(app.text).toBe("My text")
expect(app.changeText()).toBe("New text"); //Fails
expect(app.changeText).toHaveBeenCalledTimes(1);
});
});
现在我的问题是如何让它重置'在一个无效的字符上,例如' f',因为Scanner只接受数字。
感谢您的帮助!
答案 0 :(得分:0)
如果我理解正确,那么这就是你要找的东西, 如果用户输入invaild字符而不是int,则抛出InputMismatchException。您可以使用循环直到用户输入整数
import java.util.Scanner;
import java.util.InputMismatchException;
class Example
{
public static void main(String args[])
{
boolean isProcessed = false;
Scanner input = new Scanner(System.in);
int value = 0;
while(!isProcessed)
{
try
{
value = input.nextInt();
//example we will now check for the range 0 - 150
if(value < 0 || value > 150) {
System.out.println("The value entered is either greater than 150 or may be lesser than 0");
}
else isProcessed = true; // If everything is ok, Then stop the loop
}
catch(InputMismatchException e)
{
System.out.print(e);
input.next();
}
}
}
}
如果您不是在寻找,请告诉我们!