Randomly Switch Case - android

Hello friends I have 2 situation but I want to do it randomly. When user click button then there is 2 situation case0 or case1 randomly changes. So text's are randomly changes too...
*lastImageName and lastImageName2 are variable strings...
Random rand = new Random();
int newrand = rand.nextInt(1) + 1;
switch(newrand) {
case 0:
text1.setText(lastImageName);
text2.setText(lastImageName2);
break;
case 1:
text1.setText(lastImageName2);
text2.setText(lastImageName);
break;
}
But that's not working sometimes.. What's the rand. problem?

Random rand = new Random();
int a = 0;
int b = 1;
int c = random.nextBoolean() ? a : b;

Remove the +1. Just do that: int newrand = rand.nextInt(2);

Random#nextInt(int) generates a random number between 0 inclusive and the upper bound exclusive. I.e., nextInt(1) will always return 0. Instead, you should just use:
int newrand = rand.nextInt(2);

If you only have two cases then use
boolean state = rand.nextBoolean();
text1.setText(state?lastImageName:lastImageName2);
text2.setText(state?lastImageName2:lastImageName);
For multiple you can also use
Random rand = new Random();
int newrand = rand.nextInt() % count;//count = 2 for your case
switch(newrand) {
case 0:
text1.setText(lastImageName);
text2.setText(lastImageName2);
break;
case 1:
text1.setText(lastImageName2);
text2.setText(lastImageName);
break;
}

Related

Change Images or Bitmap when ever touched on screen

I want to change the bitmap that contains an image whenever user tap on the screen. i.e. the default image is shadow1, now what i want is that when user touched on screen then this image changed to shadow2, then again if user touched then shadow3, then on next touch the image again comes as shadow1 and it goes on and on and on. so basically there are three images and i want that when ever user touched on screen then image changes with each tap.
Following is the code that i tried but it is still not working as expected i.e. the image changes from shadow1 to shadow2 but then not changes to shadow3 or shadow1 even if i touched many times.
public void Touched(float x, float y)
{
boom = false;
try{
switch (bird.GetState()) {
case 0:
distance = 0;
bird.SetState(1);
flapped = true;
Bitmap workingBitmap = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow1);
bitmapBird = workingBitmap.copy(Bitmap.Config.ARGB_8888, false);
if (bitmapBird==workingBitmap)
{
}
riseCounter = 0;
pipeValues.clear();
//SoundManager.playSound(2, 1);
break;
case 1:
{
riseCounter = 0;
flapped = true;
t = 3;
Bitmap workingBitmappp = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow2);
bitmapBird = workingBitmappp.copy(Bitmap.Config.ARGB_8888, false);
//SoundManager.playSound(2, 1);
}
break;
case 2:
{
riseCounter = 0;
flapped = true;
t = 0;
}
break;
default:
Bitmap workingBitma = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow3);
bitmapBird = workingBitma.copy(Bitmap.Config.ARGB_8888, false);
break;
}
} catch(Exception e){}
}
I think there should be a for loop or while loop in 'case 1' and whenever user tap then image changes. Please help me with this.
You can simply use an int value to keep track of the image displayed as;
First Initialize a int at class level;
int num = 0;
then you can use it as;
if(num == 0){
loadFirstImage();
num++;
}
else if(num == 1)
{
loadSecondImage();
num++;
}
else if(num == 2){
loadThirdImage();
num = 0 ;
}
I think that you need to change the state of the bird in your second case statement.
First iteration will set the state to 1, from there, the only case statement you can hit is case 1: because you never change it.
So you need something like
case 1:
bird.SetState(2);
//....
Hope that helps

I want to generate random number in android but exclude some that end with zero

int min = 651;
int max = 999;
Random r = new Random();
do {
final int i1 = r.nextInt(max - min + 1) + min;
}
while(i1 %10 == 0);
I want to generat random number in this(651 to 999) range but don't want 660,670,680 so on which ends with zero or in middle(707,805) so help me in this case
in this code error:- i1 in while loop is not local variable
You can do as follows. All you have to do is, loop until you find a number that is not divisible by 10.
int min = 651;
int max = 999;
Random r = new Random();
int randomNum;
while(true){
randomNum = rand.nextInt((max - min) + 1) + min;
if(randomNum%10!=0){
break;
}
}
//now you have the required random number
What's wrong in your code
What you had done was right, apart from accessing a local variable outside of its context. You have to declare i1 outside of the while loop's context. Also, remember, a final int cannot be modified once assigned. In your case this doesn't affect since it is in the context of the while loop.
int i1; //this should come here.
do {
i1 = r.nextInt(max - min + 1) + min; // no final anymore
}
while(i1 %10 == 0);

Android generate small color difference RGB

I want to generate/randomize a color and then I want second one that is close to the generated one.
This is how I generate the colors ftm:
Paint colors = new Paint();
int red = ran.nextInt(256-100)+100;
int green = ran.nextInt(256-100)+100;
int blue = ran.nextInt(256-100)+100;
colors.setARGB(255, red, green, blue);
and later the 2nd color I generate like this:
switch (ran.nextInt(3)) {
case 0:
red = red - (40 - level);
break;
case 1:
green = green - (40 - level);
break;
default:
blue = blue - (40-level);
break;
}
The problem is that it works in some cases and sometimes it can give me a 2nd color that is off by miles.
Is there another, better and easier way to generate these colors?
br
You need to create a real random number between 0 and 3:
Random ran = new Random();
int max = 3;
int min = 0;
int randomNum = ran.nextInt((max - min) + 1) + min;
switch (randomNum ) {
case 0:
red = red - (40 - level);
break;
case 1:
green = green - (40 - level);
break;
default:
blue = blue - (40-level);
break;
}
You could use java.awt.Color.brighter() and Color.darker().

subtracting integer from string textview

I'm trying to replace a textview that has the contents "330" after either some addition or subtraction has occurred with fixed numbers.
for example
int points = 75
totalPoints = totalPoints - points
I think I have managed to get the data from the string and convert it into an integer but i am having trouble putting the result back into the textview
this is what i have tried so far but get a result of 2131230752
TextView tmpTxtview = (TextView) findViewById(R.id.premPoints);
int adjustPoints = Integer.valueOf(R.id.premPoints);
switch (v.getId()) {
case R.id.premTeam1:
points = 75;
adjustPoints = adjustPoints - points;
tmpTxtview.setText(String.valueOf(adjustPoints));
break;
case R.id.premTeam2:
points = 74;
adjustPoints = adjustPoints - points;
tmpTxtview.setText(String.valueOf(adjustPoints));
break;
}
Change this:
int adjustPoints = Integer.valueOf(R.id.premPoints);
into this:
int adjustPoints = Integer.valueOf(tmpTextview.getText());
(if it doesn't work, use tmpTextview.getText().toString() instead).

Spinner's getSelectedItemPosition() method returns unexpected results

My app has a spinner with four entries in it. I need to be able to retrieve which of these labels is selected, not the actual labels themselves. Below is my partial code for doing this:
// Set up the activity's Spinner
spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.pay_periods, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner s = (Spinner) findViewById(R.id.main_spinner_payperiod);
s.setAdapter(spinnerAdapter);
In a later method:
switch(payPeriod.getSelectedItemPosition()){
case(0): // Daily
dailyAllowance = Float.parseFloat(payment.getText().toString());
case(1): // Weekly
dailyAllowance = (Float.parseFloat(payment.getText().toString())) / 7;
case(2): // Bi-weekly
dailyAllowance = (Float.parseFloat(payment.getText().toString())) / 14;
case(3): // 30 days
dailyAllowance = (Float.parseFloat(payment.getText().toString())) / 30;
case(Spinner.INVALID_POSITION):
dailyAllowance = 0;
default:
dailyAllowance = 42; // Junk value, for debugging purposes
}
This method always seems to return 42, no matter which of the Spinner's items I have selected. Can anyone help me figure out why? Thank you!
Put a break in your switch-case condition
switch(payPeriod.getSelectedItemPosition()) {
case(0): // Daily
dailyAllowance = Float.parseFloat(payment.getText().toString());
break;
case(1): // Weekly
dailyAllowance = (Float.parseFloat(payment.getText().toString())) / 7;
break;
case(2): // Bi-weekly
dailyAllowance = (Float.parseFloat(payment.getText().toString())) / 14;
break;
case(3): // 30 days
dailyAllowance = (Float.parseFloat(payment.getText().toString())) / 30;
break;
case(Spinner.INVALID_POSITION):
dailyAllowance = 0;
break;
default:
dailyAllowance = 42; // Junk value, for debugging purposes
break;
}
Omitting break in switch-case condition might give a wrong behavior. If a case (without break) was chosen the flow still continues and the default condition is always executed.

Categories

Resources