Jump to content

Recommended Posts

Posted

Σε αυτον τον οδηγο οπως ειπα θα μιλησουμε για:

  • Συγκρισεις
  • Λιστες
  • Προτασεις if,else if, else
  • Προτασεις switch

 

Συγκρισεις

image10-01.PNG

Για να καταλαβετε εδω θα σας κανω μερικα παραδειγματα.Να ξερετε οτι οι συγκρισεις επιστρεφουν λογικες τιμες true η false και ειναι λογικο αυτο.

 

boolean j = 5 > 4; //epistrefei TRUE giati to 5 einai megalitero tou 4

int maxtor = 100;
int finito = 100;

boolean n = finito <= maxtor; //epistrefei true gt to 100 den ine mikrotero ala ine iso to 100 :P

int c = maxtor/finito; //epistrefei 1, kanoyme 100/100

boolean f = c > (finito-98); //epistrefei false gt 1 < 2

 

Προτασεις if,else if , else

Απο εδω αρχιζουν τα ωραια πραγματα.

Ενα απο τα βασικα στοιχεια ειναι ο τροπος με τον οποιο ενα προγραμμα εχει την δυνατοτητα να λαμβανει αποφασεις. Αποφασεις παιρνουμε και εμεις στην καθημερινη μας ζωη αφου πρωτα σκεφτουμε διαφορες εκδοχες , τα παρακατω ειναι μερικα παραδειγματα.

Αν εχει καλο καιρο θα παω για μπαλα.
Αν ο maxtor ειναι τσοντακιας θα βαλει porn section.
Αν διαβασω θα γραψω καλα

Ετσι και στην Java μπορουμε να χρησιμοποιησουμε προτασεις ωστε να ελεγξουμε το προγραμμα μας.Εδω θα μιλησουμε για το if , το else if και το else.

if = αν , else if = αλλιως εαν , else = αλλιως.

Ο τροπος με τον οποιο χρησιμοποιουμε το if ειναι καπως ετσι:

 

if(true)
{
//kane oti exei mesa sta blocks
}

Για παραδειγμα:

if(50>20)
{
System.out.println("50 > 20");
}

Ειναι απλο. Με τον ιδιο τροπο χρησιμοποιουμε και τα else if, else και οπως εχω ξανα πει με παραδειγματα μαθενετε καλυτερα γι αυτο κατω θα δουμε ενα ολοκληρωμενο προγραμμα που τα εχει ολα.

class Maxcheaters{

public static void main(String[] args)
{
	int x = 20;

	if(x > 20) //an to x > 20 pou den isxuei 
	{
		System.out.println(x+" is bigger than 20"); //print auto
	}
	else if(x < 20) //an to x < 20 pou pali den isxuei
	{
		System.out.println(x+" is smaller than 20"); //print auto
	}
	else{ //alios , dld emine mia ekdoxi tora mono x = 2
		System.out.println(x+" equals 20"); //print auto
	}
}
}

Καπως ετσι χρησιμοποιειται. Επισης τα if μπορουν να ειναι και μεσα σε αλλα if για πιο συγεκριμενα πραγματα , θα καταλαβετε τι εννοω με αυτο το παραδειγμα και με τα σχολια:

class Maxcheaters{

public static void main(String[] args)
{
	int x = 20;

	if(x > 10) //isxuei giati ine megalitero tou 10 ara proxoraei parakatw.
	{
		if(x == 19) //den isxuei giati to x = 20 ara de tha print to minima parakatw.
		{
			System.out.println("troll face");
		}
		else //edw omos isxuei gt ennoei oti an to x den ine 19 to opoio kai isxuei, ara tha ektelesei to minima parakatw
		{
			System.out.println("troll face 2 "); //ekteleite giati isxuei
		}
	}
}
}

 

 

Προτασεις switch

Μια πρακτικη στον προγραμματισμο ειναι το να συγκρινουμε μεταβλητες με μια τιμη. Αυτο ομως ειναι κουραστικο ομως μονο εαν χρησιμοποιουμαι προτασεις if αναλογα και με το πληθος των τιμων που θελουμε να ελεγξουμε.Εδω ενα παραδειγμα κουραστικο με την χρηση του if.

char vathmos = 'a';

	if(vathmos == 'a')
		System.out.println("Aristos");
	else if(vathmos == 'b')
		System.out.println("metrios");
	else if(vathmos == 'c')
		System.out.println("gtp");
	else if(vathmos == 'd')
		System.out.println("dianoitika kathisterimenos");

Ειδατε πως ειναι κουραστικο. Εδω να πω πως αν εχουμε κατω απο τις προτασεις if μονο μια εντολη οπως εδω , δεν χρειαζεται να βαλουμε τα { } .

Στην java μπορουμε να ομαδοποιησουμε ενεργειες χρησιμοποιωντας την προταση switch. Εδω ειναι ενα παραδειγμα για να δειτε πως ειναι καλυτερο το switch σε τετοιο ειδος προτασεων.

char vathmos = 'a';
   
   switch(vathmos)
   {
	   case 'a':
		   System.out.println("aristos");
		   break;
	   case 'b':
		   System.out.println("metrios");
		   break;
	   case 'c':
		   System.out.println("axristos");
		   break;
	   case 'd':
		   System.out.println("koimismenos");
		   break;
		  default:
		   System.out.println("skoupidiaris.");
   }

Το default σημαινει αν δεν ειναι καμια απο αυτες τι να κανει. Και το break; πρεπει να μπαινει στα cases για να σημαινει οτι τελειωνει απο το case, αν δεν το βαλετε πολυ πιθανο το προγραμμα σας να τρεξει οπως ναναι.

 

Και τωρα ενα προγραμμα που τα εχει ολα μεσα για να τα καταλαβετε καλα.

class Maxcheaters{
public static void main(String[] args){

	int olympiakos = 3; 
	int panathinaikos = 2;

	int olympiakosWin = 0; // 1 simenei nikise, 0 simenei exase
	boolean isopalia;

	if(olympiakos > panathinaikos) //isxuei 
		olympiakosWin = 1;
	else if(olympiakos < panathinaikos)
		olympiakosWin = 0;
	else
		isopalia = true;

	if(isopalia == false) //an den ine isopalia 
	{
		switch(olympiakosWin) //xrisimopoiw switch gia na dw an o olympiakos exe nikisei i exei xasei
		{
			case 1: //isxuei o olympiakos nikise
				System.out.println("Olympiakos Won panathinaikos");
				break;
			case 2:
				System.out.println("Olympiakos Lost from panathinaikos");
				break;
				default:
					; //den kanei tipota
		}
	}

}
}

 

Στον επομενο οδηγο θα δουμε:

  • Βροχοι for
  • Bροχοι while, do while
  • continue , break

 

Ασκηση:

1)Βρειτε το ΛΑΘΟΣ στο παρακατω παραδειγμα:

   int portokalia = 12;
   
   
   switch portokalia
   {
	   case 12:
		   System.out.println("to petixa");
		   break;
	   default:
		   ; //tipota.
   }

2)Καντε ελευθερη εξασκηση με τα if,else if,else , χρησιμοποιειστε if μεσα σε αλλα if και γενικα καντε εξασκηση , ειναι το παν.

Posted

καλα δεν εχω λογια εισαι πρωτος μπραβο σου και παλι πιστευω πως θα ακολουθησει και Tutorial 4.

Posted

Να πω το λαθος ή να αφησω αλλον να το βρει? :P

 

τι λαθος εχεις δει? για πες να μάθουμε και εμεις..

Posted

Πέστο :P.

 

Romeo για την ασκηση μιλαει.

 

Δεν χρησιμοποιησες () στο switch?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • Let's be clear about what's happening here. You came to this thread as a provocateur, having already created a coordinated thread on MMO-Dev and invited people who have never been our clients and never will be — because their entire approach to this industry is based on free downloads and reverse engineering other people's work. Not a single person who wrote negatively about us on MMO-Dev has ever been our customer. Not one. Meanwhile, real clients wrote there too — but you conveniently ignore those. Your goal was never objectivity. It was a deliberate smear campaign, and the pattern is transparent to anyone reading this thread. As for MMO-Dev — platforms built on facilitating hatred and settling personal scores don't tend to last. We've seen it before with Zonegame. Communities that exist to tear others down eventually turn on themselves. In the meantime, our clients are running successful projects — the kind that generate real revenue, real growth, real results. That's where our focus is and will remain. While you wait for the 58th reopening of your server, we'll be here doing what we've been doing for 20 years — working.     Haha, well, the math is flattering — thank you for doing it for us! 😄 On the AI point — you might actually be right, and we're not afraid of that. We've been adapting for 20 years already. When new tools arrive, experienced developers use them better than anyone else. AI helps us work faster, not replace us — at least for now. And when the day comes that AI can fully build, maintain, and support a complex private L2 server end-to-end — we'll be the first to embrace it. Until then, our developers are doing just fine. 😉
    • The core strength of Novproxy Perfectly unlock overseas AI large models: 100% pure residential IP, perfectly bypassing various strict risk controls such as ChatGPT, Claude, Midjourney, etc. This is an essential tool for parents to train AI on weekends and during work! Available in 195+ countries/regions worldwide: covering an extremely wide range. Whether it's setting up accounts for cross-border e-commerce, conducting overseas web crawling, or conducting network gray-scale tests, it can all be handled easily. Ultra-fast concurrency: Real residential network, stable and non-blocking, high-concurrency business can also run at full capacity.
    • You charge 10,000 - 15,000 USD for Java files and your site shows 370 projects and 786 customers. you're not just a millionaire, you're a multimillionaire bro 😂 and enjoy it while it lasts. Ai is coming in hot and your 18 developers better start updating their CVs. soon everyone will build whatever they need for free. The 15k Java files era is over 😄  
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..