Код: Выделить всё
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"));
}
}
Код: Выделить всё
while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
while(!operation=="add"||"subtract"||"multiply");
while(!operation.equals("multiply"));
Как я могу выполнить несколько операций, используя цикл do- while в одной консоли окно
Подробнее здесь: https://stackoverflow.com/questions/786 ... witch-case