Random image in a table view - java

I have a TableView with 4 parts of the field. which in every field filled images A, B, C and D. You can see in the image below :
My question is how do I make the image appear randomly when the activity start ? Like this one :
Thanks for the questions !

You can always use the static random-method in the Math-class and then create a switch structure
int square = ((int)(Math.random() * 4) + 1);
switch (square) {
case 1:
// .. code
break;
case 2:
// .. code
break;
case 3:
// .. code
break;
case 4:
// .. code
break;
}

You can use
int min = 1;
int max = 4;
Random r = new Random();
int i = r.nextInt(max - min + 1) + min;
switch(i) {
case 0:
// .. code
break;
case 1:
// .. code
break;
case 2:
// .. code
break;
case 3:
// .. code
break;
}

There are many logics... Pseudocode of the one, I can think of right now is as follow:
Step 1: Save image resource info (path) in HashMap with key as 0 to 3.
Step 2: Generate Math.random() between 0 and 3
Step 3: Fetch the image from HashMap with the number got in step 2.
Step 4: Show it in the table.
Hope this helps. All the best.

Related

Switch function in while loop only giving the same answer

I have a java switch function that is:
while(i < 1) {
switch(hi) {
case "Hi":
case "hi":
case "Hi!":
case "hi!":
case "Hello!":
case "hello!":
case "Hello":
case "hello":
System.out.println("Hi!");
break;
case "How are you?":
x = Math.random() * Emotion.length;
Feeling = (int)x;
System.out.println(Emotion[Feeling] + " How are you?");
break;
default:
break;
} Hi.nextLine();
}
There are no errors in debug, but the output is this:
"Me: Hi,
Computer: Hi!,
Me: How are you?,
Computer: Hi!,"
How do I make it so I can use switch loop many times without getting the same result every time?
All help would be appreciated.
The problem is that you assign the hi variable before the loop, and never re-assign it later. You should assign the value of Hi.nextLine(); to hi within the loop as follows:
hi = Hi.nextLine();

How to activate buttons using a FOR loop?

I want to activate disabled buttons called tglBtnLevel1, tglBtnLevel2, tglBtnLevel3... from 1 till the integer received with a method.
They only way I achieve it is using switch case, but I guess there must be a way using loops. I have tried a For loop, but I cannot find the way to include the counter (i) in the line "tglBtnLevel(i).setEnabled(true)".
I would thank you any hint or help. This is the beginning of the switch case I use to make it work, but only the first buttons, there are more:
private void checkEnabledLevels(){
switch (d.sendPlayerStats().getWeekTournamentLevel()){
case 1:
tglBtnLevel1.setEnabled(true);
break;
case 2:
tglBtnLevel1.setEnabled(true);
tglBtnLevel2.setEnabled(true);
break;
case 3:
glBtnLevel1.setEnabled(true);
tglBtnLevel2.setEnabled(true);
tglBtnLevel3.setEnabled(true);
break;
case 4:
glBtnLevel1.setEnabled(true);
tglBtnLevel2.setEnabled(true);
tglBtnLevel3.setEnabled(true);
tglBtnLevel4.setEnabled(true);
break;
One way of doing this could be to have all the buttons in an array like this:
Button[] buttonArray = {tglBtnLevel1, tglBtnLevel2, tglBtnLevel3, tglBtnLevel4}
And then simply iterator over them depending upon the tournament level you get:
int tournamentLevel = d.sendPlayerStats().getWeekTournamentLevel()
for (int i = 0; i < tournamentLevel; i++) {
buttonArray[i].setEnabled(true);
}
A slightly different but simple way of doing this could be like:
int tournamentLevel = d.sendPlayerStats().getWeekTournamentLevel()
tglBtnLevel1.setEnabled(tournamentLevel <= 1);
tglBtnLevel2.setEnabled(tournamentLevel <= 2);
tglBtnLevel3.setEnabled(tournamentLevel <= 3);
tglBtnLevel4.setEnabled(tournamentLevel <= 4);

Switch Case Considered as a Duplicate Label with JOptionPane.CANCEL_OPTION

I am currently using IntelliJ and an error that says "Duplicate label '2'" appears when I am placing a case for pressing the CANCEL option in my menu.
import javax.swing.*;
public class Main {
public static void main(String[] args){
int choice=0;
Object menu= "1. Name Constructor\n" +
"2. Pretty Printing of text\n" +
"3. FLAMES\n" +
"4. Your Superhero name!\n" +
"5. return to the main menu\n";
do {
choice = Integer.parseInt(JOptionPane.showInputDialog(null,
"S T R I N G M A N I P U L A T I O N M E N U\n" +
menu, "Menu", 1));
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case JOptionPane.CANCEL_OPTION:
break;
default:
JOptionPane.showMessageDialog(null,"Enter a valid choice.","Error",1);
break;
}
}while(choice!=5);
}
}
That is happening because you can't define two cases with the same value in the switch statement.
If you take a look inside the JOptionPane, you'll see that the CANCEL_OPTION value is 2.
Here is the part of the JOptionPane class that shows the value:
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
As you already have a case 2: and the CANCEL_OPTION also returns 2, you have to change it. For example, if you change to case 6: it will work. Give it a try.
here you can see all the values that a JOptionPane has, so you can modify your case according to other values, so you don't get this duplicate case error anymore.

Isoltating a certain element in a HashSet in Java

I am making a poker game and to generate a hand that contains non-duplicated I decided to use a HashSet and for-loop to give the player 5 cards.
public class CardDriver {
public static void main(String[] args) {
Set<Card> hands = new HashSet<Card>();
while(hands.size()!= 5) {
hands.add(new Card());
}
System.out.println("Player 1: " + hands);
}
}
The Card constructor:
public Card() {
Random nb = new Random();
int switchNb = nb.nextInt(13) + 1;
switch(switchNb)
{
case 1: value = Value.Ace; break;
case 2: value = Value.Two; break;
....
case 11: value = Value.Jack; break;
case 12: value = Value.Queen; break;
case 13: value = Value.King; break;
}
switchNb = nb.nextInt(4) + 1;
switch(switchNb)
{
case 1: suite = Suite.Hearts; break;
case 2: suite = Suite.Diamonds; break;
case 3: suite = Suite.Clubs; break;
case 4: suite = Suite.Spades; break;
}
}
I am faced with three problems and I believe that both can be solved once I figure out how to isolate certain elements (if it is even possible) from the player's hand.
First off I need to use the same HashSet to give 5 more cards to the second player and so on. For example I would have elements 1-5 of the Set go to player 1 and 6-10 go to player 2.
Also, by being able to isolate a card from the set, it would also enable me to replace a card (video poker).
Lastly, I figured that by being able to isolate each card in the user's hand, it would make it easier for me to analyze the value of the hand each player has. I am not very experience when it comes to using sets so perhaps it would be better to use an array?
Edit: Example of an output:
Player 1: [Three of Hearts, King of Clubs, Ten of Clubs, Two of Spades, Ace of Hearts]

Java, questions about switches and cases?

So I want to do a certain action 60 % of the time and another action 40% of the time. And sometimes have it doing neither. The best way I can think to do this is through switches and making a bunch of cases. An example is below if this doesn't make any sense to ya'll.
My question is, is there a better way?
Is there a way to just do Case 0-5 does action 1 all in one statement?
Random rand = new Random(50);
switch(rand.nextInt())
{
case 1:
{
do action 1
}
break;
case 2:
{
do action 1
}
break;
case 3:
{
do action 1
}
break;
case 4:
{
do action 1
}
break;
case 5:
{
do action 1
}
break;
case 6:
{
do action 1
}
break;
case 7:
{
do action 2
}
break;
case 8:
{
do action 2
}
break;
case 9:
{
do action 2
}
break;
case 10:
{
do action 2
}
break;
}
Something like this would be much more readable IMO:
if( Math.random() >= probabilityOfDoingNothing ){
if( Math.random() < 0.6 ){
action1;
}else{
action2;
}
}
Re. your question about cases, the following is equivalent to your code:
Random rand = new Random(50);
switch(rand.nextInt())
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
{
do action 1
}
break;
case 7:
case 8:
case 9:
case 10:
{
do action 2
}
break;
}
Random rand = new Random(50);
int n = rand.nextInt(11);
if(n<=6)
do action 1
else
do action 2
You need to use nextInt(n) to generate a number between 0 (inclusive) and n (exclusive). In this case we use 11 which gives us a number between 0 and 10. Anything below 6 (60% chance) we do action 1 otherwise do action 2.
See this for more details on the Random class.
Using a switch statement is only useful if you have a lot of actions you want to perform, where the action performed depends on something. For example a different action is performed based on the current month. Its quicker than writing if-else statements.
If you want same action to happen for multiple cases, then don't put break. For example
case 1:
case 2:
do action 1; break;
In this case, action 1 will happen for both case 1 and 2.
All you can do is do not apply break in between like
case 1:
case 2:
case 3:
case 4:
case 5:
do action 1;
break
case 6:
case 7:
do action 2;
break
default : break;
or
use if-else if you have range of value ..
There are many approaches to "random" behavior. Some are easier to implement than others but these sacrifice the entropy bucket. Random() is an expensive operation. Switch is useful for complicated signalling, but for a binary decision if is what you want:
int signal = (int)(System.currentTimeMillis() % 5);
if(signal==0 || signal == 1){
doActionTwo();//one third of the time
}else{
doActionOne();//two thirds of the time
}

Categories