Προς το περιεχόμενο

java headache


attilas

Προτεινόμενες αναρτήσεις

Δημοσ.

grafo ena programataki se java kai exo kolisei sto eksi simio:

 

thelo na ftiakso ena class gia ena paixnidaki to opoio tha dimiourgei to profile tou kathe paixti. dio paixtes tha paizoun mia seira apo paixnidia. diladi to class tha exei ta ekseis attributes: tha krataei to onoma tou paixti kai tha tou prosthetei ena vathmo enan kerdizei ton allo paixto. profanos to class pou exo kanei einai athlio kai den douleuei. to kano paste parakato kai tha ithela tin gnomi sas.

 

otan me to kalo to telioso to paixnidi tha sas to steilo.

 

class Player {

 

private String player;

private int score = 0;

 

public Player (String plr, int count) {

player = plr;

score = count;

} // end of constructor

 

public void playerName(String name) {

player = name;

} // end of playerName

 

 

public void numberOfWins(int score) score++;

} // end of numberOfWins

 

} // end of class Player

 

exontas ena textField me to onoma nameField, pos mporo na kano assign to onoma enos paixti apo to pedio tou nameField sto class gia na dimiourgithei o neos paixtis?

oi metavlites player kai score ekso apo tis methodous thelo na meinoun private.

 

eyxarsto gia opoia apantisi.

Δημοσ.

Αν καταλαβα καλα θελεις να φιαξεις 2 player object τα οποια να κρατανε κατα την δημιουργια τους το ονομα του παιχτη , που θα το οριζεις σε ενα textfield οκ.Θα σου προτεινα το εξης!

 

</font><blockquote><font size="1" face="Verdana, Helvetica, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">public class Player {

 

private String player;

private int score = 0;

 

/**

*

* @param plr A name for the new player

* @param count

*/

public Player (String plr) {

this.setPlayerName(plr);

} // end of constructor

 

/**

* Set the player's name

* @param name

*/

public void setPlayerName(String name) {

this.player = name;

} // end of playerName

 

/**

* Get the player's name

* @return

*/

public String getPlayerName(){

return this.player;

}

 

/**

* Increase th score

* @param aScore

*/

public void setScore(int aScore){

this.score=this.score+aScore;

}

 

/**

* Get the current score

* @return

*/

public int getScore(){

return this.score;

}

 

public static void main(String[] args){

 

Player NumOnePlayer = new Player("Kostas");

Player NumTwoPlayer = new Player("Ioannis");

NumOnePlayer.setScore(10);

 

}

} // end of class Player[/code]</blockquote><font size="2" face="Verdana, Helvetica, sans-serif">Παρομοια οσο αναφορα το jTextField μπορεις να κανεις το εξης

 

</font><blockquote><font size="1" face="Verdana, Helvetica, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">String aName =jtextField1.getText();

Player NumOnePLayer = new Player(aName);[/code]</blockquote><font size="2" face="Verdana, Helvetica, sans-serif">ελπιζω να βοηθησα ...γενικα τετοιυ ειδους ποστ καντα καλυτερα στο forum του προγραμματισμου (για λογους οργανωσης πιο πολυ!

 

<small>[ 28-10-2002, 20:59: Το μήνυμα επεξεργάστηκε από: apoc ]</small>

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...