Как я могу сравнить строку в цикле do- while из случая переключателяJAVA

Программисты JAVA общаются здесь
Anonymous
Как я могу сравнить строку в цикле do- while из случая переключателя

Сообщение Anonymous »

Код: Выделить всё

import java.util.Scanner;
public class WorkArea {

public static void main(String[] args) {
Scanner input=new Scanner(System.in);

String errorMessage="Invalid input entered. Terminating...";

do {
System.out.println("Operations: add subtract multiply");
System.out.println("Enter an operation:");
String operation=input.next();

switch(operation.toLowerCase())
{
case "add":
System.out.println("Enter two integers:");
if(input.hasNextInt())
{
int int1=input.nextInt();
if(input.hasNextInt())
{
int int2=input.nextInt();
System.out.println("Answer:"+(int1+int2));
}else{
System.out.println(errorMessage);
}
}else{
System.out.println(errorMessage);
}
break;
case "subtract":
System.out.println("Enter two integers:");
if(input.hasNextInt())
{
int int1=input.nextInt();
if(input.hasNextInt())
{
int int2=input.nextInt();
System.out.println("Answer:"+(int1-int2));
}else{
System.out.println(errorMessage);
}
}else{
System.out.println(errorMessage);
}
break;
case "multiply":
System.out.println("Enter two integers:");
if(input.hasNextDouble())
{
double double1=input.nextDouble();
if(input.hasNextDouble())
{
double double2=input.nextDouble();
System.out.printf("Answer:%.2f\n",double1*double2);
}else{
System.out.println(errorMessage);
}
}else{
System.out.println(errorMessage);
}
break;

default:
System.out.println(errorMessage);
break;
}
}while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
}
}
Я попробовал цикл do- while для выполнения операций, которые я неоднократно перечислял во фрагменте, сравнивая строку со случаем переключателя. Я ожидаю, что программа должна завершить работу, если операция отсутствует в списке.

Код: Выделить всё

while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
while(!operation=="add"||"subtract"||"multiply");
while(!operation.equals("multiply"));
когда я сравниваю строку с помощью цикла do- while, результат говорит: «операция не может быть разрешена».
Как я могу выполнить несколько операций, используя цикл do- while в одной консоли окно

Подробнее здесь: https://stackoverflow.com/questions/786 ... witch-case

Вернуться в «JAVA»