Writing a very simple "Poker" game in Java
Hello, everyone.
I'm writing an extremely simple "Poker" type of game but I keep getting an error.
The code is supposed to do the following things; Prompt the user to enter 7 integers in the range 1 to 13 (inclusive), display a table of frequencies of the integers from 1 to 13, find the integer that occurs most frequently, and finally check if the 7 integers contain a continuous sequence of 5 or more.
Where the code messes up is at the boolean and where I try to return i. What's going wrong?
Code Java:
import java.util.Scanner;
public class Poker
{
public static void main(String[] args)
{
int[] arrayOfInt1 = new int[7];
int[] arrayOfInt2 = new int[14];
readCards(arrayOfInt1);
updateFreq(arrayOfInt1, arrayOfInt2);
System.out.println("Number\tFrequency ");
for (int i = 1; i < arrayOfInt2.length; i++) {
System.out.println(i + "\t" + arrayOfInt2[i]);
}
int i = findMax(arrayOfInt2);
System.out.println("The most frequent number is " + i + " with " + arrayOfInt2[i] + " occurrences.");
int j = findSequence(arrayOfInt2);
if (j > 0) {
System.out.println("The sequence of 5 starts at " + j);
}
else
System.out.println("There is no sequence of 5.");
}
public static void readCards(int[] paramArrayOfInt)
{
boolean bool = false;
Scanner localScanner = new Scanner(System.in);
do {
System.out.print("Enter 7 integers (1-13): ");
for (int i = 0; i < paramArrayOfInt.length; i++) {
paramArrayOfInt[i] = localScanner.nextInt();
}
bool = checkRange(paramArrayOfInt);
}while (!bool);
}
public static int checkRange(int[] paramArrayOfInt)
{
int i = 1;
for (int j = 0; j < paramArrayOfInt.length; j++) {
if ((paramArrayOfInt[j] < 1) || (paramArrayOfInt[j] > 13)) {
i = 0;
}
}
return i;
}
public static void updateFreq(int[] paramArrayOfInt1, int[] paramArrayOfInt2) {
for (int i = 0; i < paramArrayOfInt1.length; i++)
paramArrayOfInt2[paramArrayOfInt1[i]] += 1;
}
public static int findMax(int[] paramArrayOfInt) //find integer that occurs most often
{
int i = paramArrayOfInt[0];
int j = 0;
for (int k = 1; k < paramArrayOfInt.length; k++) {
if (paramArrayOfInt[k] > i) {
j = k;
i = paramArrayOfInt[k];
}
}
return j;
}
public static int findSequence(int[] paramArrayOfInt) { //check if the integers contain a continuous sequence of 5 or more
int i = 0;
for (int j = 0; j < paramArrayOfInt.length; j++) {
if (paramArrayOfInt[j] != 0) {
i++;
if (i >= 5)
return j - i + 1;
}
else
{
i = 0;
}
}
return -1;
}
}
I forgot to include the error I receive;
Code error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to boolean
at Poker.checkRange(Poker.java:51)
at Poker.readCards(Poker.java:38)
at Poker.main(Poker.java:11)
Re: Writing a very simple "Poker" game in Java
boolean bool = false;
bool = checkRange(paramArrayOfInt);
bool is a boolean value, but checkRange(int[] i) returns an int.
Re: Writing a very simple "Poker" game in Java
So I convert the int into a boolean, correct?
Re: Writing a very simple "Poker" game in Java
You can make checkRange() return a boolean instead of an int.. or you can change bool to an int depending on what you're trying to do.
Remember to take while (!bool); into consideration when you make your decision.
Re: Writing a very simple "Poker" game in Java
Can I return a boolean by replacing "return i;" with the following?
Code java:
boolean bool = (i != 0);
return bool;
Re: Writing a very simple "Poker" game in Java
Yeah, you can simply just say.. return (i != 0); aslong as the method type is set to return a boolean ofcourse.
Re: Writing a very simple "Poker" game in Java
Ah, okay, it works wonderfully now! Thanks!