Results 1 to 4 of 4
  1. #1
    Madlollipop is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Question 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.

    Java 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;
    		antal = Keyboard.readInt();
    		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){
    			vilka[0] = Keyboard.readInt();
    			antal2 = antal2 - 1;
    		}
    		if (antal2 > 0){
    			vilka[1] = Keyboard.readInt();
    			antal2 = antal2 - 1;
    		}
    		if (antal2 > 0){
    			vilka[2] = Keyboard.readInt();
    			antal2 = antal2 - 1;
    		}
    		if (antal2 > 0){
    			vilka[3] = Keyboard.readInt();
    			antal2 = antal2 - 1;
    		}
    		if (antal2 > 0){
    			vilka[4] = Keyboard.readInt();
    			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] + ".");
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: If statements and arrays, problem

    Please explain what your problem is.
    Do you get errors? Is so please copy and paste the full text of the message here.

  3. #3
    Madlollipop is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: If statements and arrays, problem

    First the program asks how many you wanna cast again(dices)
    If antal<5 the program gets this error message
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3 //(Here is the number you give to antal)
    at HEj.main(HEj.java:77) //Here you go to the line "if (vilka[1]==2){"

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: If statements and arrays, problem

    .ArrayIndexOutOfBoundsException:3
    Your are trying to use an index that is past the end of the array.
    Look at line 77 in your code and see why the index used on that line is past the end of the array.

Similar Threads

  1. Arrays and For statements for Images in Applet
    By SummitWhiteMaro in forum Java Applets
    Replies: 0
    Last Post: 03-07-2011, 03:59 AM
  2. Problem with arrays
    By Viper in forum New To Java
    Replies: 7
    Last Post: 10-07-2010, 02:49 PM
  3. Arrays.asList(...) Problem
    By plm-pusik in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 01:12 AM
  4. Problem to stop printing statements
    By Rekuta in forum New To Java
    Replies: 1
    Last Post: 05-13-2010, 06:05 PM
  5. Replies: 6
    Last Post: 02-20-2010, 03:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •