estefania Newbie Poster

This program is a game which allows the user to play against a computer in a dice game called ``Pig''.
Rules are:

  • Players alternate turns and try to be the first to reach 100 points.
  • A turn consists of rolling a die repeatedly until either a 1 is rolled or the player chooses to quit rolling.
  • If a turn ends with the roll of a 1 then 0 points are earned on that turn.
  • If a turn ends by the player choosing to hold then the number of points earned is the sum of the rolls for that turn.
  • The points earned in a turn are added to a player's total score for the game.

my problem: How do i get the total score for the round to add up and each time it passes thru the methods?

[CODE]import java.util.Scanner;

public class PlayPig
{
public static void main(String [] args)
{
int usertotal, comptotal;

do
{
usertotal=userTurn();
comptotal=compTurn();
}while(usertotal<100 || comptotal<100);

}

public static int userTurn()
{
    Scanner scan= new Scanner(System.in);
    int random, current, usertotal;
    String ans="n";

    current=0;

do
{
    random= (int) (Math.random()*6.0)+1;
    System.out.println("Your roll is "+random);

    if (random==1)
    {
    current = 0;
    System.out.println("Your score is: "+current);
    }

    else if (random !=1 && random<=6)
    {
    current= current+random;
    System.out.println("Your current score is: "+current);
    System.out.println("Do you want to roll again? [Y/N]");
    ans=scan.nextLine();
    }
}while(ans.equals("Y")&& random!= 1);

    System.out.println("Your total score is: "+current);

    usertotal=current;
    return usertotal;
}

public static int compTurn()
{
    Scanner scan= new Scanner(System.in);
    int times, random, ...

estefania

how do u add the elements?

this is my program

[CODE]import java.util.Scanner;

public class RandomNumbers
{
public static void main(String [] args)
{
Scanner scan= new Scanner(System.in);

int pos, value;

System.out.println("Enter value of numbers to generate(0-100): ");
value=scan.nextInt();

while (value<0 || value>100)
{
System.out.println("Please enter value again (0-100): ");
}   
    double[]random =new double[value];

    for (pos=0; pos< value; pos++)
    {
    random[pos]=Math.random()*1000.0 + 17.5;
    }

    System.out.println("Here are the random numbers: ");
    for (pos=0; pos<value; pos++)
    System.out.println(random[pos]);    

}

}
[/CODE]

i need to generate the sum of the random numbers.

estefania Newbie Poster

Hi, how do u calculate the sum of the numbers in the array?

Thanks!

estefania

hey, thanks, I managed to figure it out =)

estefania

it still doesn't work :(

estefania

thanks so much, it works better already but problem now is that when i enter the number, the "too high" and "too low" parts dont work.

[CODE]import java.util.Scanner;

public class Guess
{
public static void getNumber()
{

    int n, number;  //a random computer generated number

        do
        {
        Scanner scan= new Scanner(System.in);
        System.out.println("Enter number between 0-100: ");
        number= scan.nextInt();

        n= (int)(Math.random()*100.0)+1;    //Math equation to generate random number

            if (number > n)
            System.out.println("Sorry, too high.");

            if (number < n)
            System.out.println("Sorry, too low.");  
        }               
         while ( number != n );

            System.out.println("Congrats you have guessed the correct answer!");
    }       
    public static void main (String [] args)
    {
        getNumber();

    }

}

[/CODE]

estefania

this is the 1st program i need to extract the method from
[CODE]
import java.util.Scanner;

public class GuessingGame
{
public static void main(String [] args)
{

    Scanner scan= new Scanner(System.in);
    int n;  //a random computer generated number
    int guess;  //a number that the user types  
    String reply;   //user's decision to replay the game or not. 
    String Y, N;    //Answer that the user types that will restart the loop or end the program.

    do  
    {
    n= (int) (Math.random()*100.0)+1;   //Math equation to generate random number
        do
        {
    System.out.println("Enter number between 0-100: ");
    guess= scan.nextInt();

        if (guess > n)
        System.out.println("Sorry, too high.");

        if (guess < n)
        System.out.println("Sorry, too low.");  
        }               

        while ( guess != n );

            System.out.println("Congrats you have guessed the correct answer!");

            System.out.println("Do you want to play again? [Y/N]");
            reply=scan.next();
            }

            while (reply.equals("Y"));

                System.out.println("Thank you for playing!");

}

}
[/CODE]

and i need to take the part that plays the game and put it to a new method. This is what i have

[CODE]import java.util.Scanner;

public class Guess
{
public static void getNumber(int guess)
{
int n, number; //a random computer generated number

        do
        {
        Scanner scan= new Scanner(System.in);
        System.out.println("Enter number between 0-100: ");
        number= scan.nextInt();

        n= (int)(Math.random()*100.0)+1;    //Math equation to generate random number

            if (number > n)
            System.out.println("Sorry, too high.");

            if (number < n)
            System.out.println("Sorry, too low.");  
        }               
         while ( number != n );

            System.out.println("Congrats you have guessed the correct answer!");
    }       
    public static void main (String [] args)
    {
    Scanner scan= new Scanner(System.in);
    System.out.println("Enter number between 0-100: ");
            int number= ...

estefania

how do i edit it?

estefania

er, how do i do that?

estefania Newbie Poster

I am suppose to move the code from the 1st program that actually plays the game into a new method. I am really new at this and have no idea...

import java.util.Scanner;

public class GuessingGame
{
    public static void main(String [] args)
    {

        Scanner scan= new Scanner(System.in);
        int n;  //a random computer generated number
        int guess;  //a number that the user types  
        String reply;   //user's decision to replay the game or not. 
        String Y, N;    //Answer that the user types that will restart the loop or end the program.

        do  
        {
        n= (int) (Math.random()*100.0)+1;   //Math equation to generate random number
            do
            {
        System.out.println("Enter number between 0-100: ");
        guess= scan.nextInt();

            if (guess > n)
            System.out.println("Sorry, too high.");

            if (guess < n)
            System.out.println("Sorry, too low.");  
            }               

            while ( guess != n );

                System.out.println("Congrats you have guessed the correct answer!");

                System.out.println("Do you want to play again? [Y/N]");
                reply=scan.next();
                }

                while (reply.equals("Y"));

                    System.out.println("Thank you for playing!");

    }

}

Is this how you do it? It still doesnt work for me.

import java.util.Scanner;

public class Guess
{
        public static void getNumber(int guess);
    {
        int n;  //a random computer generated number

            n= (int)(Math.random()*100.0)+1;    //Math equation to generate random number

                if (guess > n)
                System.out.println("Sorry, too high.");

                if (guess < n)
                System.out.println("Sorry, too low.");  
            }               

             while ( guess != n );

                System.out.println("Congrats you have guessed the correct answer!");
        }       
        public static int main (String [] args)
        {
            Scanner scan= new Scanner(System.in);
            System.out.println("Enter number between 0-100: ");
            guess= scan.nextInt();
            getNumber(guess);

        }       
}

thanks!