only a beginner..help

im just asking how to write this syntax in java form:

cards[r][c]=rand()%8+1;//it is written in C
im trying to think what is the value of rand() in java,
is it Math.random?
pls help me...

this is my whole program in C;

#include
#include
#include
using namespace std;
int main()
{
char comma;
int r1, c1, r2, c2, cards[4][4];
srand((unsigned)time(NULL));
//fill board
for (int r=0; r<4; r++)
{
for (int c=0; c<4; c++)
{
cards[r][c]=rand()%8+1;
cout<<cards[r][c];
}
cout<<endl;
}
//display board
cout<<" 1 2 3 4
";
cout<<" ";
for (int i=0; i<=8; i++)
{
cout<<"-";
}
cout<<endl;
for (int r=0; r<4; r++)
{
cout<<r+1<<" | ";
for (int c=0; c<4; c++)
{
cout<<"* ";
}
cout<<endl;
}
cout<<endl;
//selection
cout<<"Please insert the first card row and column seperated by a comma.
";
cin>>r1>>comma>>c1;
cout<<"Please insert the second card row and column seperated by a comma.
";
cin>>r2>>comma>>c2;
//fix
r1--;
c1--;
r2--;
c2--;
//reveal
cout<<" 1 2 3 4
";
cout<<" ";
for (int i=0; i<=8; i++)
{
cout<<"-";
}
cout<<endl;
for (int r=0; r<4; r++)
{
cout<<r+1<<" | ";
for (int c=0; c<4; c++)
{
if ((r==r1)&&(c==c1))
{
cout<<cards[r][c]<<" ";
}
else if((r==r2)&&(c==c2))
{
cout<<cards[r][c]<<" ";
}
else
{
cout<<"* ";
}
}
cout<<endl;
}
//match?
if (cards[r1][c1]==cards[r2][c2])
{
}
else
{
}
//this pushes the next board onto a blank screen
for (int b=0; b<=20; b++)
cout<<endl;
//repeat
return 0;
}

hope you'll help me..

Comments

  • : im just asking how to write this syntax in java form:
    :
    : cards[r][c]=rand()%8+1;//it is written in C
    :
    In C rand() returns a random integer between 0 and RAND_MAX, usually 0x7FFF. So the author is using modulus 8 to reduce it to a number 0-7, then adding one to get 1-8.

    In Java there are several ways of achieving the same thing. Math.random() returns a real value between 0 and fractionally under 1.0. This means that (int) (Math.random() * 8 + 1) is a good replacement.

  • : : im just asking how to write this syntax in java form:
    : :
    : : cards[r][c]=rand()%8+1;//it is written in C
    : :
    : In C rand() returns a random integer between 0 and RAND_MAX, usually
    : 0x7FFF. So the author is using modulus 8 to reduce it to a number
    : 0-7, then adding one to get 1-8.
    :
    : In Java there are several ways of achieving the same thing.
    : Math.random() returns a real value between 0 and fractionally under
    : 1.0. This means that (int) (Math.random() * 8 + 1) is a good
    : replacement.
    :
    :




    tnx for the reply...
    it works..
    now i can finish my program..
    tnx again

  • : : : im just asking how to write this syntax in java form:
    : : :
    : : : cards[r][c]=rand()%8+1;//it is written in C
    : : :
    : : In C rand() returns a random integer between 0 and RAND_MAX, usually
    : : 0x7FFF. So the author is using modulus 8 to reduce it to a number
    : : 0-7, then adding one to get 1-8.
    : :
    : : In Java there are several ways of achieving the same thing.
    : : Math.random() returns a real value between 0 and fractionally under
    : : 1.0. This means that (int) (Math.random() * 8 + 1) is a good
    : : replacement.
    : :
    : :
    :
    :
    :
    :
    : tnx for the reply...
    : it works..
    : now i can finish my program..
    : tnx again
    :
    :



    :
    :
    :i have a question again,
    :why does (int) (Math.random() * 8 + 1) print not only a pair of numbers?
    :how do i make it only a pair?
    :it prints one number 3times,4times,5times..
    :pls help
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories