#Override
public void onClick(View v) {
List<Integer> list = new LinkedList<Integer>();
for (int i = 1; i <= 10; i++) {
list.add(i);
}
long seed = System.nanoTime();
Collections.shuffle(list, new Random(seed));
int number1 = list.remove(0);
int number2 = list.remove(0);
int number3 = list.remove(0);
int number4 = list.remove(0);
int number5 = list.remove(0);
int number6 = list.remove(0);
math.setText(number1 + "+" + number2);
buttona.setText("" + (number1 + number2));
buttonb.setText("" + (number3 + number4));
buttonc.setText("" + (number5 + number6));
}
I have this code, 6 different numbers between 1 to 10, but my problem is, I need the buttons text will be different, my code doesn't do it, for example:
Number1 = 3 , Number2 = 4, (7) ..
Number3 = 5 , Number4 = 2, (7) ..
How can I make all the texts be different?
You can try to set a random value to the shuffle function. Add a seed at the random and the output will be probably different :).
Try :
long seed = System.nanoTime();
Collections.shuffle(list, new Random(seed));
Related
I have two Button + and - when i click on the + button the value increases in the textview and vice versa. but i want to multiply the textview number to the price and change the price accordingly.
viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PojoCategory pojoCategory = (PojoCategory) v.getTag();
int mValue = pojoCategory.getmQuantity();
mValue++;
viewHolder.tv_Number.setText("" + mValue);
pojoCategory.setmQuantity(mValue);
String value1 = viewHolder.tv_Number.getText().toString();
String value2 = pojoCategory.getDish_rate();
Log.e("value1", value1);
Log.e("value2", value2);
int x = Integer.parseInt(value1);
int y = Integer.parseInt(value2);
int z = x * y;
Log.e("z", "" + z);
viewHolder.Dish_rate.setText(String.valueOf(z));
notifyDataSetChanged();
}
});
I am trying to generate a multiplication table,where the 1st EditText takes the actual number while 2nd EditText takes the actual range of multiplication table. When I run the project , the result is only number * range..
can anyone help me in the loop or code below mentioned Or,any alternatives to display the table in GridLayout or TableLayout rather than TextView.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiplication_table);
number = (EditText)findViewById(R.id.numberTable);
range = (EditText)findViewById(R.id.numberRange);
click = (Button)findViewById(R.id.click);
result = (TextView)findViewById(R.id.display);
final String x = range.getText().toString();
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int a = Integer.parseInt(number.getText().toString());
int b = Integer.parseInt(range.getText().toString());
for(int i = 1 ; i <= 10; i++)
{
for(int j = 1 ; j <= 10; j++)
{
int res = a * b;
result. setText(a +" * " + b + " = " + res);
}
}
return;
}
});
}
}
you are calling setText() for every row, but setText() will reset the text of the TextView, you might want to use append() instead
result.setText("");
int a = Integer.parseInt(number.getText().toString());
int b = Integer.parseInt(range.getText().toString());
for(int i = 1 ; i <= b; i++){
int res = a * i;
result.append(a +" * " + i + " = " + res + "\n");
}
or maybe use StringBuilder
int a = Integer.parseInt(number.getText().toString());
int b = Integer.parseInt(range.getText().toString());
StringBuilder builder = new StringBuilder();
for(int i = 1 ; i <= b; i++){
int res = a * i;
builder.append(a +" * " + i + " = " + res + "\n");
}
result.setText(builder.toString())
want to use the the value of variable b in spinner selection. See the code .
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View view) {
String name = editText.getText().toString();
if (yes.isChecked()) {
int age = Integer.parseInt(ageText.getText().toString());
int l;
l = 80 ;
int y ;
y = l - age ;
c = y - 7;
final int z;
b = 2016 + c;
editText3.setText((name + " you might die in the year ")+ b + "");
} else {
if (no.isChecked()) {
int age = Integer.parseInt(ageText.getText().toString());
int c;
int l;
l = 80;
int y;
y = l - age;
c = y + 10;
b = 2016 + c;
editText3.setText(name + (" you might die in year ") + b + "");}}} // this is button code //
now comes the spinner code .
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
final List<String> list = new ArrayList<String>();
list.add("none,i am healthy");
list.add("Diabetes");
list.add("cancer");
list.add("HIV/AIDS");
list.add("Tuberculosis");
list.add("Coronary Artery");
list.add("respiratory disease");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item, list);
String disease = spinner1.getSelectedItem().toString();
spinner1.setAdapter(dataAdapter);
if (disease.equals("Diabetes")) {
int x = b-5;
int j = 2016+ x ;
String name = editText.getText().toString();
editText3.setText(name + (" you might die in year ") + j + ""); }
else if (disease.equals("none,i am healthy")) {
String name = editText.getText().toString();
int x = b + 7 ;
int j = 2016 + x ;
editText3.setText(name + ( "you might die in year ") + j + "");
}
};});
};};
this app is executing without an error yet it is not giving me the desired output . How do i use the value of b in spinner ( i have already defined it global)
I have this randoms:
final Random rand2 = new Random();
int number1 = rand2.nextInt(10) + 1;
final Random rand3 = new Random();
int number2 = rand3.nextInt(10) + 1;
final Random rand4 = new Random();
int number3 = rand4.nextInt(10) + 1;
final Random rand5 = new Random();
int number4 = rand5.nextInt(10) + 1;
final Random rand6 = new Random();
int number5 = rand6.nextInt(10) + 1;
final Random rand7 = new Random();
int number6 = rand7.nextInt(10) + 1;
My problem is, im setting text's to:
1.number1+number2
2.number3+number4
3.number5+number6.
What I need is to make sure text1 will be different from text2, and text3 will be different from text1, and text2 will be different from text3, it means:
(number1 + number2) != (number3 + number4)
&& (number3 + number4) != (number5 + number6)
&& (number5 + number6) != (number1 + number2)
How can I do it?
Don't create a new Random object for each new number. Call rand.nextInt() on the first Random object repeatedly instead.
Random rand = new Random();
int number1 = rand.nextInt();
int number2 = rand.nextInt();
...etc
To verify uniqueness, put each generated number in a Hashtable, and use the contains() method to see if it's already been used.
I want to generate random number in two field between range 1-8 and also field sum should be less than 9.
what I've done done till now
for (int i; i <= 6; i++) {
fieldOne = rand.nextInt(9 - 5) + 5;
fieldTwo = rand.nextInt(9 - 5) + 5;
fieldSum = fieldOne + fieldTwo;
System.out.print(fieldSum); // should be < 9 and not repetition
}
but fieldSum become greater then 9 so How it is control this condition?
And Sequence should be random should not repeat 2 or more time.
rand.nextInt(9-5) won't calculate a random number between 5 and 8, it will make the operation 9 minus 5 and then will calculate rand.nextInt(4).
To calculate a random number between 5 and 8 you have to do something like this:
Random r = new Random();
for (int i = 0; i < 10; i++) {
int rand1 = r.nextInt(4) + 5;
int rand2 = r.nextInt(4) + 5;
int result = rand1 + rand2;
Log.v("", "" + result);
}
The problem is that result can't be < than 9 because you are summing two numbers that are => 5 so the lower result you can get is 10.
Edit for a solution with no repetition:
private List<Integer> rdmNumList = new ArrayList<Integer>();
private final int leftLimitRange = 1;
private final int rightLimitRange = 10;
private final int numOfIteration = 10;
public void generateNumList() {
for (int i = leftLimitRange; i <= rightLimitRange; i++) {
this.rdmNumList.add(num);
}
}
public int getRandomNumer() {
Random r = new Random();
int rNum = r.nextInt(rdmNumList.size());
int result = this.rdmNumList.get(rNum);
this.rdmNumList.remove(rNum);
return result;
}
public void test() {
this.generateNumList();
if(this.numOfIteration <= ((this.rightLimitRange - this.leftLimitRange + 1))) {
for (int i = 0; i < this.numOfIteration; i++) {
Log.v("Debug Output", String.valueOf(this.getRandomNumer()));
}
}
}
Note: this is efficient only with small range of number
This code works but it's far from being good. You should find some other solution by yourself. This answer will help you find your way
Maybe try this --> random.nextInt()%9; another way Get random nuber with random.nextInt(9).
Try,
fieldSum=(fieldOne+fieldTwo)%9;
This will def