Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 65

Thread: Random variant.

  1. #26
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    How can I insert that code in my program?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    What does the program need to have done and Where does the program need that functionality?
    Often you create a method to do some task and add calls in the program where it needs that task to be done.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    In my program I have this:
    public void Turnnumber(){
    scan2 = new Scanner(System.in);
    System.out.println("Enter the desired number of turns");
    game = scan2.nextInt();
    scan2.nextLine();
    er = new int [game];
    turnswinners = new int [game];
    }
    And I want that the number of turns be random. How to do that?

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    want that the number of turns be random
    What variable are you talking about?
    Use code like you used in testing how to create random numbers back in posts #18 and#22
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    My program:
    package Dice;
     
    import java.util.Random;
    import java.util.Scanner;
     
    public class Dice {
     
    static int game;
     
    String [] players;
     
    int [] Wonthenumberofturns;
     
     
    int esk;
     
    int [] er;
     
    int [] turnswinners;
     
    private Scanner scan;
    private Scanner scan2;
    private Scanner scan3;
     
    private int number;
     
    private int number2;
     
     
    public void Players(){
    scan = new Scanner(System.in);
    System.out.println("Insert how many players play in game:");
    int game = scan.nextInt();
    scan.nextLine();
    players = new String [game];
    Wonthenumberofturns = new int [game];
    for(int i = 0; i < game; i++){
    System.out.println("Insert player number" + Integer.toString(i+1));
    players[i] = scan.nextLine();
    Wonthenumberofturns[i] = 0;
    }
    //scan.close();
    }
    public void Turnnumber(){
    int min = 0;
    int max = 10;
    game = (int) (Math.random() * (max+min));
    System.out.println(game);
    er = new int [game];
    turnswinners = new int [game];
    }
    public void Go(int Gonumber){
    int Biggest = 0;
    int Record = 0;
    scan3 = new Scanner(System.in);
    for(int i = 0; i < game; i++){
    System.out.println("Enter the number of players rolling the" + players[i]);
    int Rezult = scan3.nextInt();
    scan3.nextLine();
    if (Rezult > Biggest){
    Biggest = Rezult;
    Record = i;
    }
    }
    System.out.println("Turn win the player" + players[Record]);
    turnswinners[Gonumber] = Record;
    er[Gonumber] = Biggest;
    Wonthenumberofturns[Record]++;
    }
     
    public void Winner(){
    for(int i = 0; i < game; i++){
    int winnernumber = turnswinners[i];
    int taskai = er[i];
    System.out.println("Turn" + Integer.toString(i+1) + "won" + players[winnernumber]);
    System.out.println("collected" + Integer.toString(taskai) + "points");
    }
    int winners = 0;
    int winner = 0;
    for (int i = 0; i < game; i++) {
    if (Wonthenumberofturns[i] > winners) {
    winners =Wonthenumberofturns[i];
    winner = i;
    }
    }
    System.out.println("Game winner" + players[winner]);
    }
    public void zaid(){
    Players();
    Turnnumber();
    for(int i = 0; i < game; i++){
    Go(i);
    }
    Winner();
    }
    public int getNumber() {
    	return number;
    }
    public void setNumber(int number) {
    	this.number = number;
    }
    public int getNumber2() {
    	return number2;
    }
    public void setNumber2(int number2) {
    	this.number2 = number2;
    }
    public static void main(String[] args) {
     
    Dice game = new Dice ();
    game.zaid();
     
    }
    public static int getI() {
    	return game;
    }
    }
    When the program print me random number like 2 in this:
    public void Turnnumber(){
    int min = 0;
    int max = 10;
    game = (int) (Math.random() * (max+min));
    System.out.println(game);
    er = new int [game];
    turnswinners = new int [game];
    }
    My program end good.
    But when random number is like 9 program give me the error:
    Insert how many players play in game:
    2
    Insert player number1
    Lukas
    Insert player number2
    Matty
    9
    Enter the number of players rolling theLukas
    2
    Enter the number of players rolling theMatty
    2
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    	at Dice.Dice.Go(Dice.java:57)
    	at Dice.Dice.zaid(Dice.java:92)
    	at Dice.Dice.main(Dice.java:111)

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at Dice.Dice.Go(Dice.java:57)
    At line 57 the code used an index that was past the end of the array. Remember array indexes range in value from 0 to the length-1. If an array has two elements, 0 and 1 are the valid indexes.


    A lot of the code in the program needs formatting by indenting nested statements. Too many statements in the posted code are not properly intented which makes the code hard to read.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    And how can I correct it?

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Change the code so the index's value does not go past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Lukas (November 6th, 2013)

  10. #34
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Okey I change. Thanks.

    --- Update ---

    And can you help me in my post?:
    http://www.javaprogrammingforums.com...ats-wrong.html

  11. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #36
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    My post: http://www.javaprogrammingforums.com...ats-wrong.html
    When I starting program everything is okey, but when I inserting the books the program showed me the last book. I want that the program show all books where I insert.

  13. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    Please copy the full contents of the command prompt window that shows the program's input and output and paste it here. Add some comments showing what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #38
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    import java.util.Scanner;
     
    public class Authors2 {
     
    	int authors03;
    	String authors;
    	String books2;
    	Scanner scan;
    	public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.nextLine();		
    		System.out.println("Please write how much author books you want to write: " + authors);
    		authors03 = scan.nextInt();
    		scan.nextLine();
     
    		for(int i = 0; i < authors03; i++){
    		System.out.println("Please write author books: " + authors);
    		books2 = scan.next();
    	}
    	}
    	public void result(){
    		System.out.println("You have writed the author: " + authors);
    		System.out.println("You have entered these author books: " + books2); //Program print me the last book. I want that the program print me all books.
    	}
    	public void Kiak(){
    		Authors();
    		result();
    		}
    	}

  15. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    The posted code can't be executed for testing. It is missing a main() method.

    Did you have questions about the code in post#38?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #40
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    He has main() method.
    public class Main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Authors2 book = new Authors2 ();
    		book.Kiak();
     
    	}
    }
    My question is: When I starting program everything is okey, but when I inserting the books the program showed me the last book. I want that the program show all books where I insert.

  17. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    the program showed me the last book.
    Please copy the full contents of the command prompt window that shows the program's input and output and paste it here. Add some comments showing what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #42
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    Please write the author: 
    Lukas
    Please write how much author books you want to write: Lukas
    2
    Please write author books: Lukas
    Test1
    Please write author books: Lukas
    Test2
    You have writed the author: Lukas
    You have entered these author books: Test2

  19. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    What is wrong with the program's output?

    What do you want the output to look like?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #44
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    I want that the program print me:
    You have entered these author books: Test1, Test2

    or:
    You have entered these author books: Test1
    You have entered these author books: Test2

  21. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    The posted code has ONE variable that holds one String that is the value of the last name the user entered.
    If you want to save more than one book name, you will need to change the program to save all the names the user entered. For example it could use a collection like ArrayList to save the names.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #46
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    public class Authors2 {
     
    	int authors03;
    	String authors;
    	int books2;
    	Scanner scan;
    	int[] anArray;
    	private String books22;
    	public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.nextLine();		
    		System.out.println("Please write how much author books you want to write: " + authors);
    		authors03 = scan.nextInt();
    		scan.nextLine();
     
    		for(int i = 0; i < authors03; i++){
    		System.out.println("Please write author books: " + authors);
    		setBooks22(scan.next());
    		anArray = new int[1];
    	}
    	}
    	public void result(){
    		anArray[0] = books2;
    		System.out.println("You have writed the author: " + authors);
    		System.out.println("You have entered these author books: " + anArray); //Program print me the last book. I want that the program print me all books.
    	}

    My result is:
    You have entered these author books: [I@b988a6

  23. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    [I@b988a6
    That is the String returned by an int array's toString() method.
    If you want to print the array's contents you will need to index into the array to access each element in the array.

    Why are you trying to use an int array to save book names? The names are String, not int.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #48
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    for(int i = 0; i < authors03; i++){
    		System.out.println("Please write author books: " + authors);
    		setBooks(scan.next());
    		anArray = new String[1];
    	}
    	}
    	public void result(){
    		anArray[0] = books;
    		System.out.println("You have writed the author: " + authors);
    		System.out.println("You have entered these author books: " + anArray); //Program print me the last book. I want that the program print me all books.
    	}

    You have entered these author books: [Ljava.lang.String;@b988a6

  25. #49
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random variant.

    [Ljava.lang.String;@b988a6
    That is the String returned by an array of Strings toString() method.
    If you want to see the contents of the array, you will need to access each element in the array one at a time using array notation: theArray[theIndex]

    For debugging you can print the full contents of the array:
        System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  26. #50
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Random variant.

    public void Authors(){
    		scan = new Scanner(System.in);
     
    		System.out.println("Please write the author: ");
    		authors = scan.nextLine();		
    		System.out.println("Please write how much author books you want to write: " + authors);
    		authors03 = scan.nextInt();
    		scan.nextLine();
     
    		for(int i = 0; i < authors03; i++){
    		System.out.println("Please write author books: " + authors);
    		setBooks(scan.next());
    		anArray = new String[1];
    	}
    	}
    	public void result(){
    		anArray[0] = books;
    		System.out.println("You have writed the author: " + authors);
    		String[] setBooks = anArray;
    		System.out.println("You have entered these authors books "+ java.util.Arrays.toString(setBooks)); //Program print me the last book. I want that the program print me all books.
    	}

    Now there is no errors but the result is the last book. I did theArray(theIndex);
    anArray[0] = books;

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM