What's new

Closed Light enabler java console

Status
Not open for further replies.

Princess Serrano

Eternal Poster
Joined
Apr 2, 2016
Posts
429
Reaction
285
Points
285
try niyo po .


import java.util.Scanner ;


class LightEnabler {

static void space ( ) {
System.out.println ( ) ;
} // static void


public static void main ( String [ ] args ) {

Scanner intel = new Scanner ( System.in ) ;

int [ ] switchStatus = { 0 , 0 , 0 , 0 } ;
int switchSelector = 0 ;
String [ ] switchName = { "Switch A" , "Switch B" , "Switch C" , "Switch D" } ;
String askSwitch = "" ;
boolean switchCheck = false ;

space ( ) ;
System.out.printf ( "%10s-- > Welcome to Light Enabler Program! < --" , "" ) ;
space ( ) ;
space ( ) ;
System.out.printf ( "%4sLegends:" , "" ) ;
System.out.printf ( "%n%5sSwitch A - splitter." , "" ) ;
System.out.printf ( "%n%5sSwitch B - bulb at ground floor." , "" ) ;
System.out.printf ( "%n%5sSwitch C - bulb at 2nd floor." , "" ) ;
System.out.printf ( "%n%5sSwitch D - bulb at 3rd floor." , "" ) ;
System.out.printf ( "%n%5s[ 1 ] - switch is on." , "" ) ;
System.out.printf ( "%n%5s[ 0 ] - switch is off." , "" ) ;
space ( ) ;
System.out.printf ( "%n%4sSwitching one of the bulb on, will cause other turned-" +
"%n%3son bulbs to be turned off." , "" , "" ) ;
space ( ) ;
System.out.printf ( "%n%4sHowever, turning Switch A on, will allow two or more" +
"%n%3sbulbs to be turned on simultaneously." , "" , "" ) ;
space ( ) ;
space ( ) ;

while ( true ) {
switchCheck = false ;
for ( int switchStatDex = 0 ; switchStatDex < switchStatus.length ; switchStatDex ++ ) {
System.out.printf ( "%8s%-5d" , "" , switchStatus [ switchStatDex ] ) ;
} // for
System.out.printf ( "%n%5sSwitch A%5sSwitch B%5sSwitch C%5sSwitch D" , "" , "" , "" , "" ) ;
space ( ) ;
space ( ) ;

while ( !switchCheck ) {
System.out.printf ( "%3sTurn on/off a switch: " , "" ) ;
askSwitch = intel.nextLine ( ) ;
for ( int switchNameDex = 0 ; switchNameDex < switchName.length ; switchNameDex ++ ) {
if ( askSwitch.equalsIgnoreCase ( switchName [ switchNameDex ] ) ) {
switchSelector = switchNameDex ;
switchCheck = true ;
} // if
} // for
if ( !switchCheck ) {
System.out.printf ( "%3sInvalid choices. Please try again." , "" ) ;
} // if
space ( ) ;
} // while

for ( int switchSelectDex = 0 ; switchSelectDex < switchStatus.length ; switchSelectDex ++ ) {
if ( switchSelector > 0 ) { // refers to bulb b , c and d. splitter is off.
if ( switchSelector == switchSelectDex ) {
if ( switchStatus [ switchSelectDex ] == 0 && switchStatus [ 0 ] == 0 ) {
for ( int checkOtherSwitches = 1 ; checkOtherSwitches < switchStatus.length ; checkOtherSwitches ++ ) {
if ( switchStatus [ checkOtherSwitches ] == 1 ) {
switchStatus [ checkOtherSwitches ] = 0 ;
} // if
} // for
switchStatus [ switchSelectDex ] = 1 ;
} // if switchStatus of current bulb is off.
else if ( switchStatus [ switchSelectDex ] == 1 && switchStatus [ 0 ] == 0 ) {
switchStatus [ switchSelectDex ] = 0 ;
} // else if
else if ( switchStatus [ switchSelectDex ] == 0 && switchStatus [ 0 ] == 1 ) {
switchStatus [ switchSelectDex ] = 1 ;
} // else if
else {
switchStatus [ switchSelectDex ] = 0 ;
} // else
} // if
} // if switchSelectDex

else if ( switchSelector == switchSelectDex ) {
if ( switchStatus [ switchSelectDex ] == 0 ) {
switchStatus [ switchSelectDex ] = 1 ;
} // if
else if ( switchStatus [ switchSelectDex ] == 1 ) {
for ( int offBulb = 1 ; offBulb < switchStatus.length ; offBulb ++ ) {
switchStatus [ offBulb ] = 0 ;
} // for
switchStatus [ switchSelectDex ] = 0 ;
} // else
} // else if
} // for
} // while




} // psvm

} // class
 
Status
Not open for further replies.

Similar threads

Back
Top