solutions for randomly mix arraynumbers in android [duplicate] - java

This question already has an answer here:
Inbuilt Permutation Generator
(1 answer)
Closed 8 years ago.
hi im beginner in android and need a void that get numbers in array and give these numbers randomly mix.i try to make this something like these code but this is have bugs when it arrive to near of end.any body have solution for mix randomly numbers in array,like numbers between 1 to 20?
rand1=randomBox();
do {rand2=randomBox();
}while (rand1==rand2);
do {rand3=randomBox();
}while (rand1==rand3 || rand2==rand3 );
do {rand4=randomBox();
}while (rand1==rand4 || rand2==rand4 ||rand3==rand4);
.
.
.

Use Collections.shuffle(Arrays.asList(yourArray)) as explained in an other StackOverflow question: Inbuilt Permutation Generator

Related

Java how to see if String has an integer in it [duplicate]

This question already has answers here:
Java - char, int conversions
(4 answers)
Closed 3 years ago.
I'm trying to determine if my ticket number has a "9" then change the price to 5. This is the code I have. The code itself compiles, but doesn't work as intended. I imagine it's because I'm using a wrong operator. If anyone could let me know how I could change it for it to work that'd be greatly appreciated.
for(int x=0; x<Integer.toString(e1.getNumber()).length(); x++)
{
if(Integer.toString(e1.getNumber()).charAt(x) == 9)
{
System.out.println("The price is $5");
}
}
Thank you! I just had to change 9 to '9'! Its working now!!!
it depends on specific requirements. what if 9 is more then one times in string? what you do in such case? if you are sure that 9 appears once in your ticket price. just you next code.
if (yourString.contains("9")){
yourString = yourString.replace("9", "5");
}
Just had to put '9' so it looks for a char!

Select all element that start with a specific numbers [duplicate]

This question already has answers here:
Retrieving the first digit of a number [duplicate]
(9 answers)
Closed 6 years ago.
Here is my code:
if(Rs.getInt("Number") == 100){
//Do something
}
Rs is a PreparedStatement.
Instead of defining the exact number, I want to look for numbers
which start with 1. Maybe there are '110' or '120' ... what should i write in the loop
instead of the operator == ?
This guy here seems to have the right idea.
Retrieving the first digit of a number
if ("1".equals(Integer.parseInt(Rs.getInt(number).substring(0, 1)))){
//do stuff
}

Outputting X^n without caret [duplicate]

This question already has answers here:
Subscript and Superscript a String in Android
(15 answers)
Closed 8 years ago.
I am writing a simple android app and I would like to set the text of a button to be X^n but I want it to look the same way as it would if you would write it on a piece of paper( for example 2⁶).
I know there are several unicode characters that does it for a small group of numbers but I am looking for a generel function (or any other way) that takes two integers and output the first by the power of the second. For example:
int X=2;
int n=6;
function(X , n) ==> 2⁶
I know a similar question was asked here before but its answers were not sufficient because I want to use variables' values and not actual numbers;
Thanks!
You can use html's <sup> tags for that:
button.setText(Html.fromHtml("X<sup>n</sup>"));

Choose function in Java math? [duplicate]

This question already has answers here:
Combinatoric 'N choose R' in java math?
(17 answers)
Closed 8 years ago.
Sorry about the ambiguous title, I basically need to know how to use the choose function in java, to work out the chance of something happening, for example say I wanted to work out my chance of winning the lottery, on a calculator I'd do:
QUANTITY_OF_LOTTERY_NUMBERS CHOOSE 6
and that would return the chance of those six numbers appearing at random. I believe it's called the binomial theorem, but I dropped out of college before I could learn anything else about it :P
Anyway, if you do 49 choose 6 on Google for example, you'll see the function I'm trying to use
Question: How do I implement this function in Java code?
Apache Commons Math has it. If you are curious, you can read about Pascal's triangle.
http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/CombinatoricsUtils.html#binomialCoefficient(int, int)

Convert numbers to roman numerals? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you find a roman numeral equivalent of an integer
I need to write a JSP function that will efficiently convert an integer (from 1 - 3000) to a roman numeral.
Start simple. Work out how you would convert the numbers 1 - 9 into Roman. Code and test. When it is working correctly, extend it to handle numbers up to 99. Again code and test. When it is working correctly, extend it to handle numbers up to 999. Code, test and extend up to 3,000.
Google has quite a few links to both algorithms and code:
http://www.google.com/webhp?hl=&sourceid=navclient-ff&rlz=1B3GGLL_enUS384US384&ie=UTF-8#hl=en&sugexp=gsis%2Ci18n%3Dtrue&cp=34&gs_id=3&xhr=t&q=decimal+to+roman+numeral+algorithm&pf=p&sclient=psy-ab&rlz=1B3GGLL_enUS384US384&site=webhp&source=hp&pbx=1&oq=decimal+to+roman+numeral+algorithm&aq=0&aqi=g1&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.&fp=d5bc1e92224c5138&biw=1003&bih=594

Categories