I have an array and I want to implement FIFO. my project consist of 5 games and a score for each individual game. I had already coded it. heres my code:
if (Game.isNewScore) {
for (int i = 0; i < 5; i++) {
if (addition[i] == 0) {
addition[i] = Game.score;
break;
}
}
}
now my problem is when Im already in the sixth game the scoring should replace the score in the 1st game then transfering the scores from left but it seems the score from the game 6 is not showing.
e.g
2 4 6 8 9 - scores
1 2 3 4 5 - no. of Games
when on sixth game
4 6 8 9 (new score)
can someone teach me on this kind of logic? when you seems not getting my point please tell me. Im badly needing a help. thanks
Your best for this would be to change addition for int[] to ArrayList<Integer>. Your code could look something like this then :
ArrayList<Integer> addition = new ArrayList<>();
/*some other code....
*/
if(Game.isNewScore) {
if (addition.size() == 5){
addition.remove(0);
}
addition.add(Game.score);
}
Related
I am trying to implement a bus seat booking application which is very similar to RedBus app. I came across over the seating arrangement in that and i was struck over there.
I really need your help in achieving this. I have tried with Recycler View but that doesn't get me into the exact layout.
I have tried with recycler view and my layout goes as,
But, the actual screen shot from redbud application is as,
I have gone through one of the code that is available in git
https://github.com/TakeoffAndroid/SeatBookingRecyclerView
But, using the above code, i can get the layout design but the problem with this is, Say like if there are 41 seats, but using the above git code it will be showing 33 seats on the screen comes up like as,
Help would be really appreciated.
Snippet Code:
numOfColumns = 4;
mArrayBusSeats = new ArrayList<>();
for (int i = 0; i < mArraySeats.size(); i++) {
if (i % numOfColumns == 0 || i % numOfColumns == (numOfColumns - 1)) {
BusLayoutModel model = mArraySeats.get(i);
model.setSeatType(AbstractItem.TYPE_EDGE);
mArrayBusSeats.add(model);
} else if (i % numOfColumns == 1 || i % numOfColumns == (numOfColumns - 2)) {
BusLayoutModel model = mArraySeats.get(i);
model.setSeatType(AbstractItem.TYPE_CENTER);
mArrayBusSeats.add(model);
} else {
BusLayoutModel model = new BusLayoutModel();
model.setSeatType(AbstractItem.TYPE_EMPTY);
mArrayBusSeats.add(model);
}
}
In your example, your recyclerview has 3 viewTypes - TYPE_EDGE, TYPE_CENTER, TYPE_EMPTY i.e, seats are of 3 types. As in your question
Say like if there are 41 seats, but using the above git code it will be showing 33 seats
Infact this is the expected behaviour since your recyclerview consists of a viewtype - TYPE_EMPTY which prints an empty view. So if you give 41 seats only 33 will be visible since the rest of the seats are of type TYPE_EMPTY . If you count the empty positions in your recyclerview as well, then you can see that the total will be 41.
In your Recycler View implementation try to reduce row height, as compared to redbus your row height is almost double.
Have you tried with the number of column 5 ??
I haven't use the library but when I checked it I think I should be done like this
numOfColumns = 5;
mArrayBusSeats = new ArrayList<>();
for (int i = 0; i < mArraySeats.size(); i++) {
BusLayoutModel model = mArraySeats.get(i);
if (i % numOfColumns == 0 || i % numOfColumns == 4) {
model.setSeatType(AbstractItem.TYPE_EDGE);
mArrayBusSeats.add(model);
} else if (i % numOfColumns == 1 || i % numOfColumns == 3) {
model.setSeatType(AbstractItem.TYPE_CENTER);
mArrayBusSeats.add(model);
} else {
model.setSeatType(AbstractItem.TYPE_EMPTY);
mArrayBusSeats.add(model);
}
}
For the first time today, I've met an OutOfMemory Error. I'm trying to calculate moving averages out of some data into an ArrayList, and had a crash at the first .add() step. The method is shown below
public ArrayList<Long> getNdaySMA(List<HistoricalQuote> history, int range){
long sum =0;
long SMA = 0;
ArrayList<Long> SMAs = new ArrayList<Long>();
//realRange is made due to the differences in defining "range in calculation vs speech
//a 10 day range for day 9 is actually from prices of day0 to day9, inclusive
int realRange =range-1;
//First step, add in placeholder 0s for the days within the range that have no value
//so if 10 day range, we have 0-> 9
for (int i=0;i<i+realRange;i++){
SMAs.add(i,0L);
}
//Next, actually calculate the SMAs for i.e. day 10
for (int i =0;i<history.size();i++){
//should be k<10, 0......9 = 10 days
for(int k=i+realRange;k==i;k--){
//Sum first from k=i+range-1 , go down to i.
//This should give us a value of RANGE
sum +=history.get(k).getClose().longValue();
}
//after summing up, we add calculate SMA and add it to list of SMAs
SMA = sum/range;
//we add the corresponding SMA to index i+range, made up of values calculated from before it
//to excel
SMAs.add(i+realRange,SMA);
sum =0;
}
return SMAs;
}
The stacktrace is as follows
java.lang.OutOfMemoryError
at java.util.ArrayList.add(ArrayList.java:154)
at com.xu.investo.MethodDatabase.getNdaySMA(MethodDatabase.java:46)
Where Line 46 refers to
SMAs.add(i,0L);
Is this error occuring due to the use of the Long number format? Any suggestions are welcome.
looks like infinite loop:
for (int i=0;i<i+realRange;i++)
i will always be less then i+realRange for realRange greater then zero:
I think I've identified the problem.
I may have created an infinite loop at this line
for (int i=0;i<i+realRange;i++){
SMAs.add(i,0L);
}
I am trying to compare items out of my DB to the value of an EditText (user input). The answer can have multiple answers, seperated by a ','. I first put them into a stringarray and then compare them to the answer. The LevenshteinDistance checks if the answer is more or les good (http://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance).
userAnswer = etUserAnswer.getText().toString().toLowerCase();
String[] answers = qAnswer.split(",");
for (String answer : answers) {
if (answer.equals(userAnswer)) {
Toast.makeText(getApplicationContext(), ("Answer Correct"),
Toast.LENGTH_SHORT).show();
tvMessage.setText("You smartass!");
} else {
Toast.makeText(getApplicationContext(), ("Wrong"),
Toast.LENGTH_SHORT).show();
points = points - 4;
String answerGood = answer.toLowerCase();
LevenshteinDistance lDistance = new LevenshteinDistance();
int comparisonCheck = lDistance.computeLevenshteinDistance(
userAnswer, answerGood);
if (comparisonCheck == 1) {
tvMessage.setText("Almost there, but not quite yet!");
} else if (comparisonCheck > 1) {
tvMessage.setText("Are you serious, totally wrong?!");
}
}
}
Suppose I am having the answers for a question in the DB as follows: tree,test,radio
I am having two problems:
1. When I type "radi" it gives me 'Almost there...' which is good. It should also give me this if I enter "tes", but instead it gives me the 'Are you serious,...' line. I guess it keeps comparing to the last one.
2. Every time I type in something which is not correct, I get -12 instead of -4. I suppose this is due to the fact I am having three answers and it loops three times.. but I don't know how I can make it count only once..
Anyone can help me on the way? Thanks!
Assuming you don't need to know the word which gives the least Levenshtein distance, you could modify your loop to find smallest distance only;
userAnswer = etUserAnswer.getText().toString().toLowerCase();
String[] answers = qAnswer.split(",");
LevenshteinDistance lDistance = new LevenshteinDistance();
int minDistance = lDistance.computeLevenshteinDistance(
userAnswer, answers[0].toLowerCase());
for (int i = 1; i < answers.length; ++i) {
minDistance = Math.min(minDistance, lDistance.computeLevenshteinDistance(
userAnswer, answers[i].toLowerCase()));
}
if (minDistance == 0) {
// Correct answer...
} else {
// Wrong answer...
points -= 4;
// etc etc...
}
I have a 10-field average lap calculator. However, in testing, someone said they normally only run X laps in practice, vs. 10 (let's say 7).
I think I could use an if statement, but there'd be at least 10 of them and a bunch of clumsy code, and I'm not sure on arrays/switch statements exactly. I think all of those might be possible, but my low level of experience has yet to fully comprehend these useful tools.
CURRENT CODE:
double tenLapAvgVar = ((lap1Var + lap2Var + lap3Var + lap4Var + lap5Var + lap6Var + lap7Var + lap8Var + lap9Var + lap10Var) / 10);
So essentially, if someone leaves a field or fields blank, I want to calculate the average based on the populated fields, not 10 (if they leave 3 fields blank, calculate based on 7, for instance). Any help you guys could provide would be much appreciated, thanks!
You could have an ArrayList<EditText> object and a method which iterates over it and adds up the values. Something like:
public double getLapAverage()
{
int noOfCompletedLaps = 0;
double lapAve = 0;
double lapsTotal = 0;
for(EditText text : textBoxes)
{
if(text.getText().toString().length() > 0)
{
//psuedo code, and assuming text is numerical
lapsTotal += Double.parse(text.getText().toString());
noOfCompletedLaps++;
}
}
if( noOfCompletedLaps > 0)
{
lapAve = lapsTotal / noOfCompletedLaps;
}
return lapAve;
}
Maybe it would be better if you used an array instead of 10 different variables.
Then you can use a for statement and initialize them to 0, afterwords let the user fill the array and count how many are not zero.
Finally sum up all the array and divide by the count you previously calculated.
In project 1 question & Its 4 Answer .
Here As question I want to take 1 image Randomaly
& Its Answer 4 images Randomaly.
But Problem is that which image As question me take Randomaly
It also want to take in Answer 4 images also contain question image
how to this possible
Pls Reply
Here top is the Question & col1,col2,col3,col4
this r the Answers which r the Randomaly come
int num is the totally questions & d Answers
int top,col1,col2,col3,col4,num=8;
top=(int)Math.floor(Math.random()*num);
col1=(int)Math.floor(Math.random()*num);
col2=(int)Math.floor(Math.random()*num);
col3=(int)Math.floor(Math.random()*num);
col4=(int)Math.floor(Math.random()*num);
After this int convert to String
String topstr,col1str,col2str,col3str,col4str;
topstr=String.valueOf(top);
col1str=String.valueOf(col1);
col2str=String.valueOf(col2);
col3str=String.valueOf(col3);
col4str=String.valueOf(col4);
check condition through if loop
here Imageview imgtopcolor,imgcolortap1,imgcolortap2,imgcolortap3,imgcolortap4;
here int Toppickid[]=new int[num];
int Colpickid[]=new int[num];
Toppickid[0]=R.drawable.img0;
//...
Toppickid[7]=R.drawable.img7;
Same as Colpickid[]
if(topstr.equalsIgnoreCase(col1str) || topstr.equalsIgnoreCase(col2str) || topstr.equalsIgnoreCase(col3str) || topstr.equalsIgnoreCase(col4str))
{if(!col1str.equalsIgnoreCase(col2str) )
{ if(!col1str.equalsIgnoreCase(col3str))
{if(!col1str.equalsIgnoreCase(col4str))
{if( !col2str.equalsIgnoreCase(col3str) )
{if( !col2str.equalsIgnoreCase(col4str))
{if( !col3str.equalsIgnoreCase(col4str))
{
imgtopcolor.setImageResource(Toppickid[top]);
imgcolortap1.setImageResource(Colpickid[col1]);
imgcolortap2.setImageResource(Colpickid[col2]);
imgcolortap3.setImageResource(Colpickid[col3]);
imgcolortap4.setImageResource(Colpickid[col4]);
} } } } } } } }
This link here might be help..
It contains shuffle of 4 images and one text question. You can add the question with image.