I'm learning Java and got stuck on one of the "Loops" lesson exercises. Wherever I look online, this kind of question is solved using arrays; while I'm aware this is probably the more efficient way, it bugs me that I can't understand what is wrong with my solution. What would be a good solution here?
package practice05;
import java.util.Random;
public class Practice04
{
public static void main(String[] args)
{
new Practice04();
}
public Practice04()
{
String number = "";
String suit = "";
System.out.println(number(number) + " " + sign(suit));
}
private String number(String number)
{
Random n = new Random();
int num = n.nextInt(14);
if (num > 10 || num < 2)
{
switch (num)
{
case 1: number = "Ace";
case 11: number = "Ace";
case 12: number = "Jack";
case 13: number = "Queen";
case 14: number = "King";
}
}
else
{
number = Integer.toString(num);
}
return number;
}
private String sign(String suit)
{
Random s = new Random();
int sign = s.nextInt(4);
switch (sign)
{
case 1: suit = "of clubs";
case 2: suit = "of diamonds";
case 3: suit = "of hearts";
case 4: suit = "of spades";
}
return suit;
}
}
Aucun commentaire:
Enregistrer un commentaire