snippetjavaMinor
How to make a program that lies one-third of the time?
Viewed 0 times
thirdthemakeprogramliestimeonethathow
Problem
I am supposed to take this code (that I made):
And change it so it does this:
Have the program lie one-third of the time that the guess is too high;
it says it is too low. But it never lies about the guess being too
low, nor does it lie twice in a row.
I believe I have managed to do this with this method for checking whether the guess is "too high."
I just want to know whether or not it seems like it would work fine. I would just test it, but it would probably take me quite a while to make sure that it not only works 1/3 of the time, but also that it doesn't do it twice in a row.
EDIT: I managed to get everything working fine with this code:
```
public void showUpdatedStatus()
{
if(((itsUsersNumber - itsSecretNumber) = -2))
JOptionPane.showMessageDialog(null, "You're hot!");
else if (((itsUsersNumber - itsSecretNumber) = -6))
JOptionPane.showMessageDialog(null, "You're warm.");
if ((itsUsersNumber - itsSecretNumber) 6)
{
itsLieNum = randy.nextInt(3);
if(canLie && itsLieNum == 1){
canLie = false;
JOptionPane.showMessageDialog(null, "Too low.");}
else{
canLie = true;
JOptionPane.showMessageDialog(null, "Too h
public void showUpdatedStatus()
{
if(((itsUsersNumber - itsSecretNumber) = -2))
JOptionPane.showMessageDialog(null, "You're hot!");
if (((itsUsersNumber - itsSecretNumber) = -6))
JOptionPane.showMessageDialog(null, "You're warm.");
if ((itsUsersNumber - itsSecretNumber) 6)
JOptionPane.showMessageDialog(null, "Too high.");
}And change it so it does this:
Have the program lie one-third of the time that the guess is too high;
it says it is too low. But it never lies about the guess being too
low, nor does it lie twice in a row.
I believe I have managed to do this with this method for checking whether the guess is "too high."
public void TooHigh()
{
int numLies = 1;
if ((itsUsersNumber - itsSecretNumber) > 6)
{
itsLieNum = randy.nextInt(2);
if(itsLieNum == 1 && numLies == 1){
numLies = 2;
JOptionPane.showMessageDialog(null, "Too low.");}
else
numLies = 1;
JOptionPane.showMessageDialog(null, "Too high.");
}
}I just want to know whether or not it seems like it would work fine. I would just test it, but it would probably take me quite a while to make sure that it not only works 1/3 of the time, but also that it doesn't do it twice in a row.
EDIT: I managed to get everything working fine with this code:
```
public void showUpdatedStatus()
{
if(((itsUsersNumber - itsSecretNumber) = -2))
JOptionPane.showMessageDialog(null, "You're hot!");
else if (((itsUsersNumber - itsSecretNumber) = -6))
JOptionPane.showMessageDialog(null, "You're warm.");
if ((itsUsersNumber - itsSecretNumber) 6)
{
itsLieNum = randy.nextInt(3);
if(canLie && itsLieNum == 1){
canLie = false;
JOptionPane.showMessageDialog(null, "Too low.");}
else{
canLie = true;
JOptionPane.showMessageDialog(null, "Too h
Solution
The concept you have going is 'OK' (cumbersome, but for a beginner it's OK). There are two bugs, and one recommendation that will help a lot.
First the recommendation....
Change
Then, your code becomes:
This makes the purpose of the code more apparent. Also, a bug is that you have to declare this ourside your method. It should be a class-variable, not a method variable, otherwise it will always be the wrong value.
The bug you have is a technical one (but important that you learn) ...
First the recommendation....
Change
numLies to be a boolean value like: boolean canlie = true;Then, your code becomes:
if(canlie && itsLieNum == 1){
canlie = false;
....
} else {
canlie = true;This makes the purpose of the code more apparent. Also, a bug is that you have to declare this ourside your method. It should be a class-variable, not a method variable, otherwise it will always be the wrong value.
The bug you have is a technical one (but important that you learn) ...
Random.nextInt(value) will never return the input value. It will retrun 0(inclusive) to value(exclusive). In your case, with nextInt(2) it will only ever return 0, or 1. Change that to be nextInt(3) and you will get the values 0, 1, or 2 and it will be 1 1 third of the time.Code Snippets
if(canlie && itsLieNum == 1){
canlie = false;
....
} else {
canlie = true;Context
StackExchange Code Review Q#38212, answer score: 3
Revisions (0)
No revisions yet.