System.out.print("Enter operation to perform (+ OR - OR * OR /):");
operator= userInput.next();
我希望如果輸入+, - ,*或/,它接受該值,如果輸入了其他內容,則重複該問題。在進入四個操作員之一之前,它保持循環。我將如何驗證此Java代碼?
我曾嘗試:
while (!operator.equals("-") ||operator.equals("+") || operator.equals("*") || operator.equals("/"))
{
System.out.print("Enter operation to perform (+ OR - OR * OR /):");
userInput.next();
}
operator = userInput.next();
,但它不工作。有什麼建議麼?由於
'operator'是一個String還是'double'?如果它是'String',你爲什麼要用'nextDouble()'分配它? – azurefrog 2014-12-10 23:23:22