0
我想创建一个计算器的菜单,其中用户将输入1,2,3,4来选择他们想要的操作#4是该部门。 我试图让它当secondN(我要求2个输入分隔一个空格),这是第二个值是0,它会说“你不能用0除数。请输入另一个号码:“并且将允许用户再次输入两个号码。但是因为除以零自然会给无穷大,所以我很难完成这个任务。试图打印内的东西,以便当其中一个输入为0时,它显示的东西
我试过如果如果...其他内部尝试,但我无法得到它的工作。 我的代码目前如下。
if(inputInt == 4){ do {
System.out.print("Please enter two floats to divide, separated by a space: ");
try { firstN = readInput.nextFloat();
secondN = readInput.nextFloat();
break;
}
catch (final InputMismatchException e) {
secondN = readInput.nextFloat();
System.out.println ("You have entered invalid value(s). Please enter valid value(s).");
readInput.nextLine();
continue;
}
}while (true);
System.out.printf("Result of dividing %5.2f by %5.2f is %5.2f", firstN, secondN, firstN/secondN);
System.out.println("\n \nPlease press enter to return to the main menu.");
Scanner keyboard = new Scanner(System.in);
keyboard.nextLine();
}