# If statements and arrays, problem

• 11-12-2011, 12:05 AM
If statements and arrays, problem
Hello, just started with JAVA programming and need some help.
I'm from sweden and its swedish if you dont understand some sentences.
Its a start for yatzy and i know it is not optimized , so to speak. But i would like to advance in my own speed but would gladly get help with this problem.

Code:

public class HEj {
public static void main(String[] args){
int[] Tarning = new int[5];
//Slå 5
Tarning[0] = (int)(Math.random() * 6) +1;
Tarning[1] = (int)(Math.random() * 6) +1;
Tarning[2] = (int)(Math.random() * 6) +1;
Tarning[3] = (int)(Math.random() * 6) +1;
Tarning[4] = (int)(Math.random() * 6) +1;
System.out.println("Du slog: "+ Tarning[0] + " " + Tarning[1] + " " + Tarning[2] + " " + Tarning[3] + " " + Tarning[4]);
System.out.println("Hur många vill du kasta om?");
int antal = 0;
if (antal==0){
System.out.println("Du ska skriva \"0\" i nästa fråga.");
}
System.out.println("Slå om " + antal + " av tärningarna, ange med 1 ,2 ,3 ,4 eller 5.");
//Antal 2 anger hur många gånger "vilka" ska köras.
int antal2 = antal;
int[] vilka = new int [antal];
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (vilka[0]==1){
Tarning[0] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==2){
Tarning[1] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==3){
Tarning[2] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==2){
Tarning[1] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==3){
Tarning[2] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[2]==3){
Tarning[2] = (int)(Math.random() * 6) +1;
}
if (vilka[2]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[2]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[3]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[3]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[4]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
System.out.println("Du slog: " + Tarning[0] + " " + Tarning[1] + " " + Tarning[2] + " " + Tarning[3] + " " + Tarning[4] + ".");
}
}

• 11-12-2011, 12:40 AM
Norm
Re: If statements and arrays, problem
Do you get errors? Is so please copy and paste the full text of the message here.
• 11-12-2011, 11:33 AM