Write a java program to demonstrate the road signaling with default as 'prepare to go' operation.
Assume red for 'stop', green for 'go', and yellow for 'proceed with caution'.
Answers
Answer:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the color");
String color=sc.next();
switch(color){
case "red":
System.out.println("stop");
break;
case "green":
System.out.println("go");
break;
case "yellow":
System.out.println("proceed with caution");
break;
default:
System.out.println("prepare to go");
break;
}
}
}
Explanation:
Answer:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the color");
String color=sc.nextLine();
String a="green";
String b="red";
String c="yellow";
if(a.equals(color))
{System.out.print("go");}
else if(b.equals(color))
{System.out.print("stop");}
else if(c.equals(color))
{System.out.print("proceed with caution");}
else
{System.out.println("prepare to go");}}}