|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 | ||||||||||||
|
Junior Member
Iscritto dal: Jul 2006
Città: Emilia
Messaggi: 26
|
[Java] Problema grave!
Ho appena iniziato a studiare Java ed ho cercato di realizzare un piccolo programmino che crei un mazzo di carte mescolato in maniera diversa ogni volta! Però mi segnala sempre un errore in fase di esecuzione!
Vi posto il codice sperando possiate aiutarmi! Quote:
Quote:
Quote:
Quote:
|
||||||||||||
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Codice:
for(int i=0; i<40; i=i+1)
{
int valoreSeme = (int)(Math.random()*4+1);
int numero = (int)(Math.random()*10+1);
String seme = new String();
switch(valoreSeme)
{
//...
}
//La prima carta va sicuramente bene
if(i==0)
carta[i] = new Carta(numero, seme);
else if(i>0)
{
for(int j=1, k=0; k<i; k=k+1)
{
if((numero!=carta[k].getNumero())&&(seme!=carta[k].getSeme()))
{
j=j+1;
if(j==i)
{
carta[i] = new Carta(numero, seme);
}
}
}
}
System.out.println(carta[i].getNumero() + " di " + carta[i].getSeme());
}
Nel primo if (if (i==0), l'iterazione della prima carta) la carta viene sicuramente creata (si arriva ad un new "Carta(numero, seme)"), se invece l'if non viene verificato (dalla seconda iterazione in poi) nel corpo dell'else non è detto ceh si arrivi ad allocare una carta [vedi blocchi evidenziati in grassetto] dato che il System.out.printl() lo fai dentro il ciclo for subito dopo l'if-else, dalla seconda iterazione in poi la carta potrebbe non essere stata creata (nessuna chiamata a new Carta(...), quindi ti ritrovi una reference che punta a null). Inoltre mi sembra che la condizione di verifica sia semanticamente errata: Codice:
if ( (numero!=carta[k].getNumero()) && (seme!=carta[k].getSeme()) )
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Jul 2006
Città: Emilia
Messaggi: 26
|
Sei un genio! E poi anche io sono impedito!
Grazie mille! |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Solo un consiglio: per questioni di chiarezza nella lettura del codice è bene esplicitare sempre le parentesi graffe nei costrutti if, if-else, e ancora più negli if-else if a catena.
E anche nei for/while che hanno una sola riga di istruzioni.
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#5 | |
|
Junior Member
Iscritto dal: Jul 2006
Città: Emilia
Messaggi: 26
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jan 2002
Città: Napoli
Messaggi: 1727
|
__________________
Se buttassimo in un cestino tutto ciò che in Italia non funziona cosa rimarrebbe? Il cestino. |
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Un altro modo in effetti sarebbe quello di generare il mazzo di carte con due cicli for: il primo cicla per seme, il secondo, annidato nel primo, per valore della carta.
Per memorizzare le carte potresti usare un ArrayList invece di un semplice array, in questo modo puoi usare il comodo metodo shuffle() della classe Collections [java.util.Collections.shuffle(aList)] che prende come parametro una List e ne mescola gli elementi. Ad esempio: Codice:
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
// ...
ArrayList listCarte = new ArrayList(); // il mazzo, ancora vuoto
// genera il mazzo ordinato per seme e valore
for (int seme = 1; i <= 4; seme++)
{
for (int valore = 1; valore <= 10; valore++)
{
listCarte.add(new Carta(valore, seme));
}
}
// mescola il mazzo
Collections.shuffle(listCarte);
//...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: May 2004
Città: Londra (Torino)
Messaggi: 3692
|
Voto per l'ultima impostazione di Baynru, anche se la condirei ulteriormente con una classe "Mazzo" che circondi e nasconda la lista.
E che abbia almeno il metodo Shuffle. In piu', proprio per la natura di questi problemi, al posto di un normale Lista metterei (se c'e' in Java) una Coda o Stack, in modo tale da esporre la Dequeue, per scodare, quando serve, una carta per volta. Proprio come quando si pesca o si distribuiscono le carte...
__________________
Se pensi che il tuo codice sia troppo complesso da capire senza commenti, e' segno che molto probabilmente il tuo codice e' semplicemente mal scritto. E se pensi di avere bisogno di un nuovo commento, significa che ti manca almeno un test. |
|
|
|
|
|
#9 | ||
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Alcune informazioni in merito. Codice:
The Queue Interface
A Queue is a collection for holding elements prior to processing. Besides
basic Collection operations, queues provide additional insertion, removal,
and inspection operations. The Queue interface follows.
public interface Queue<E> extends Collection<E> {
E element();
boolean offer(E e);
E peek();
E poll();
E remove();
}
Each Queue method exists in two forms: (1) one throws an exception
if the operation fails, and (2) the other returns a special value if the
operation fails (either null or false, depending on the operation).
The regular structure of the interface is illustrated in the following table.
Queue Interface Structure Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()
Queues typically, but not necessarily, order elements in a FIFO
(first-in-first-out) manner. Among the exceptions are priority queues,
which order elements according to their values — see the Object
Ordering section for details). Whatever ordering is used, the head
of the queue is the element that would be removed by a call to remove
or poll. In a FIFO queue, all new elements are inserted at the tail of
the queue. Other kinds of queues may use different placement rules.
Every Queue implementation must specify its ordering properties.
Link
Quote:
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 23-09-2008 alle 10:25. |
||
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: May 2004
Città: Londra (Torino)
Messaggi: 3692
|
Perfetta direi
__________________
Se pensi che il tuo codice sia troppo complesso da capire senza commenti, e' segno che molto probabilmente il tuo codice e' semplicemente mal scritto. E se pensi di avere bisogno di un nuovo commento, significa che ti manca almeno un test. |
|
|
|
|
|
#11 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Già che ci siamo, posto anche questo (circa ArrayList e LinkedList):
Quote:
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 23-09-2008 alle 10:52. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:55.




















