Usebenzisa njani i-Enum Type kwiJava

NguBarry Burd

Ubomi buzaliswe ziiseti ezincinci zamathuba, kwaye iJava inento enokukunceda ujongane nezi zinto zinokwenzeka. Umsebenzi weJava ubizwa ngokuba yienumchwetheza. Iyafumaneka ukuqala ngeJava version 5.0 ukuya phambili.



Cinga ukuba ngubani onokuphumelela umdlalo webhola.



Imiphumo emibi yexesha elide

umfanekiso0.jpg

import java.util.Scanner; import static java.lang.System.out; class Scoreboard {      enum WhoWins {home, visitor, neither}  public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); int hankees, socks;      WhoWins who;  out.print('Hankees and Socks scores? '); hankees = keyboard.nextInt(); socks = keyboard.nextInt(); out.println(); if (hankees > socks) {      who = WhoWins.home;  out.println('The Hankees win :-)'); } else if (socks > hankees) {      who = WhoWins.visitor;  out.println('The Socks win :-('); } else '); out.println(); out.println('Today's game is brought to you by'); out.println('SnitSoft, the number one software'); out.println('vendor in the Hankeeville area.'); out.println('SnitSoft is featured proudly in'); out.println('Chapter 6. And remember, four out'); out.println('of five doctors recommend'); out.println('SnitSoft to their patients.'); out.println();      if (who == WhoWins.home)  { out.println('We beat 'em good. Didn't we?'); }  if (who == WhoWins.visitor)  { out.println('The umpire made an unfair'); out.println('call.'); }       if (who == WhoWins.neither)  { out.println('The game goes into overtime.'); } keyboard.close(); } }

Nantsi into eyenzekayo:



  • Uyila umahluko ukugcina amaxabiso odidi Ngubani ophumeleleyo .

    Njengomgca

    ukuthatha amanyathelo iipilisi
    double amount;

    uxelaisixaukugcinakabinivalues ​​(amaxabiso anjenge-5.95 kunye nama-30.95), umgca



    WhoWins who;

    uxelai-WHOukugcinaNgubani ophumeleleyovalues ​​(amaxabiso anje ngeekhaya,undwendwe, kunyehayi).

  • Unika ixabiso kwi i-WHO umahluko.

    Wenza enye ye

    who = WhoWins.  something  ;

    iingxelo zesabelo. Ingxelo oyiphumezayo ixhomekeke kwisiphumo seukubaingxelohankees> iikawusiuthelekiso.

    who = home; //This assignment doesn't work!

    Umhlanganisi ukunika iikhaya alinakuconjululwa kuguqukoumyalezo wemposiso. Yindlela njeenumiintlobo ziyasebenza.

  • Thelekisa ixabiso eliguquguqukayo kunye nganye Ngubani ophumeleleyo amaxabiso.

    Ngenyeukubaingxelo, ujongaNgubani == Ngubani ophumeleleyoimeko. Kwezi zimbini zishiyekileyoukubaiingxelo, ujonga enyeNgubani ophumeleleyoamaxabiso.

Kufuphi nesiphelo, ngewenze ngaphandleenumamaxabiso. Unokuvavanya izinto ezinjehankees> iikawusiokwesibini.

if (  hankees > socks  ) { out.println('The Hankees win :-)'); } // And later in the program... if (  hankees > socks  ) { out.println('We beat 'em good. Didn't we?'); }

Kodwa elo qhinga liza kuba nzima. Kwinkqubo enzima ngakumbi, unokugqibela ngokujongahankees> iikawusiamatyeli aliqela. Kuya kufana nokubuza umbuzo omnye kaninzi.

Endaweni yokujonga kwakhonahankees> iikawusiimeko, ugcina iziphumo zomdlalo njengexabiso le-enum. Emva koko ujonga ixabiso le-enum amaxesha ngamaxesha njengoko ufuna. Yindlela ecocekileyo kakhulu yokusombulula ingxaki yokukhangela ephindaphindwayo.

Ngaba dan 5513 sisiyobisi