I have six counters that need to be reset at a certain point of the program. I guess it is possible with a for loop? Thanks in advance
int counters[] = {score,count1,count2,count3,count4,count5};
for(int i=0; i<counters.length; i++) {
findViewById(counters[i]); //...and it resets them all?
}
score = R.id.score;
count1 = R.id.count1;
//etc.
If your integers has these numbers of resources, yes it is going to work.
Related
My problem is that I need to produce a random number, which is fine I've done that part, but then I need to use that random number to loop.
So for example if the random number was 13, then loop a piece of code 13 times.
I've had a mess around and haven't managed to get anything working and haven't been able to find anything online to help me.
Basically what I'm trying to find out if this is even possible?
thanks guys.
This is a simple for loop:
for (int i = new Random().nextInt(); i > 0; i--) {
// do stuff
}
This is pretty basic Java. You have a number that you get once you randomize. Just use it in a while loop or a for loop.
int x = GetRandomNumber(); //You have stated you have already done this...
for(int i = 0; i < x; i++){
//Do stuff here
}
Here you go:
Random random = new Random();
int a = random.nextInt(); // As you said, you got this so far...
while (a > 0) { // if you need your random number for something, just copy its value to new variable and place it here instead of a
// some code
a--;
}
I need this for an array, but basically the idea is that a for loop will run, and whatever number you tell it to skip, it won't do. So for(int x=0; x<50; x++) if I want 1-50 except 22, how would I write that?
This would give me the ability to skip a certain number in my array.
Sorry if this is an extremely simple question, I am not too familiar with Java.
Make use of continue, something like this:
for(int x=0; x<50; x++) {
if(x == 22)
continue;
// do work
}
Suggested reading: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
public static final void doSkippedIteration(final int[] pArray, final int pSkipIndex) {
for(int i = 0; i < pSkipindex; i++) {
// Do something.
}
for(int i = pSkipIndex + 1; i < pArray.length; i++) {
// Do something.
}
}
You would have to do some basic check to see whether pIndex lies within the confines of the array. This saves you from having to perform a check for every single iteration, but does require you to duplicate your code in this specific example. You could of course avoid this by wrapping the code in a wider control block which handles the two iterations in a cleaner manner.
I have two matrices for feature and weight elements.I am implementing an learning algorithm. I want to update elements of arraylist(vector for representing one sample of feature). Following is the code. But my elements of matrices(vector elements are not) updated. I have put the sample solution too. same value before and after updating is not expected. Could you please let me know where is the flaw in code?
for(int i =0 ; i< N ; i++){ //N is a large real number
ArrayList<Double> featureVector = new ArrayList<Double>();
featureVector = FeatureMatrix.get(i);
System.out.println("Before::"+ featureVector);
if(testList.contains(i)){
for(int j=0 ; j< testList.size(); j++){
if(i == testList.get(j)){
int indexInTestList= j;
List<Double> subListNextCandidate ;
subListNextCandidate = weightVectorNextCandidate.subList((10*indexIntTestList),((10)*(indexInTestList+1))); //clips a portion of member from long list of members
List<Double> approxWeight = new ArrayList<Double>();
approxWeight = getApproxWeight(FeatureVector, indexInTestList, FeatureMatrix,WeightMatrix, bias); //approxWeight is a vector of same dimension as of featureVector
for(int l=0 ; l< 10;l++){
double Update = featureVector.get(l)+ Rate*((subListCandidate.get(l)-approxWeight.get(l))-(lambda*featureVector.get(l)*(1/M)));//M is large real number
featureVector.set(l,Update);
}
}
}
}
else{
for(int l=0 ; l< 10;l++){
double Update = featureVector.get(l) -Rate*(lambda*featureVector.get(l)*(1/M));
featureVector.set(l, Update);
}
}
System.out.println("After:::"+ FeatureMatrix.get(i) );
}
Sample output is::
Before::[0.04539928251182193, -0.16233604402485394, 0.905018369795912, -1.2817141994528614, 0.7065420460225843, -0.8946090188977665, -1.74892020689701, -2.1539901172158187, 1.8229765478806985, -1.8109945435256574]
After:::[0.04539928251182193, -0.16233604402485394, 0.905018369795912, -1.2817141994528614, 0.7065420460225843, -0.8946090188977665, -1.74892020689701, -2.1539901172158187, 1.8229765478806985, -1.8109945435256574]
I can think of only a couple of reasonable reasons why this would happen:
Rate == 0
testList.contains(i) is always false
I would strongly suggest using breakpoints to debug this. At the very least, put a System.out.println where featureVector.set() is called to make sure it is ever called. I'm guessing it's never called because the conditions never become true.
Do use breakpoints, it'll be a life saver...
What is the return type of testList.get(j)? You're comparing an integer to what I suspect is a double. That's not very likely to go well ...
Hi I have been given a task for my course and it is to create an algorithm to make a 5 by 5 square like below:
*****
*****
*****
*****
*****
I've spent hours attempting to do it and read tutorials and books. It's so frustrating as I know it must be so easy if you know what you are doing. Can anyone give me any guidance as to where to start?
You probably know and understand how to create a "Hello World" style program in Java.
Now think - how would you go about having that same program print 5 times "Hello World"?
After that, think about how you would make it write N times "Hello World".
After that, think about how you would output a series of N stars.
Good luck!
Seems like you should have a variable x equal to the dimension (5). A for loop i that loops from 1-x. In it a for loop j that loops from 1-x. The j loops outputs *, or appends * to a string. After the j loop, the i loop does a new line.
This solution will allow for a square grid of any size.
int size = input;
for (i=0; i<size; i++){
for (j=0; j<size; j++){
// output a single "*" here
}
// output a new line here
}
If I got you right, then it's about a NxN square with a given N. Your question is just about N := 5, but your comments let me assume that you've to program a more general solution.
Split the work that have to be done into more basic and smaller problems:
create a String that contains * N times.
call System.out.println() with the generated String N times
This will work for you as well, but the professor will frown that you found the answer online and did not think of it yourself.
System.out.println("*****\n*****\n*****\n*****\n*****");
Here's how I did it:
class Main {
public static void main(String[] args) {
int size = 25;
int pos = 0;
for(int i = 0; i<size; i++){
if(pos % 5 == 0){
System.out.println();
}
System.out.print("*");
pos++;
}
}
}
If I understood correctly, what you need is a console output with 5 lines of stars.
You can outpt text to console with System.out.print() or System.out.println() with the second option making a line break.
As you have to repeat the output, it is advisable to do enclose the output statement in a loop. Better in a nested loop to separate the x and the y axis.
In order to make the output modifiable - for the case you will need to output a 6x6 or 12x15 square tomorrow without any code modification, I would make the limits of the loop parametrized.
All in all, something like this :
public void printStartSquare(int width, int height){
for(int i = 0; i < height;i++){
for(int j = 0; j < width;j++){
System.out.print("*");
}
System.out.println("");
}
}
Im trying to write a game of Yahtzee as part of an online course (not actually enrolled, just playing along at home) I have hit a bit of a wall manipulating values in the array that keeps track of the dice values. This is the section of code that seems to be causing trouble;
for (int i=0; i<N_DICE; i++){ //goes through each die.
if (display.isDieSelected(i) == false){ //checks if player wants to reroll the die.
dice [i] = dice[i];//problem line-should reassign the same value back to the die.
}
else {
dice [i] = rgen.nextInt(1, 6);
}
}
Assigning a new random number works, and if I roll all 5 dice every turn its happy.
I've changed the offending line to dice[i]=1 for testing purposes and while it takes some fun out of the game, the program works, so I'm thinking its something simple I'm doing wrong with that line.
I've tried assigning dice[i] to a temp variable (inside and out of the if loop) and then assigning temp back to dice[i].
I've tried just leaving the if loop empty.
I've tried setting it up as a multi dimesional array with a seperate array for each roll.
I've tried adding a cast (didnt think that'd do it but I was out of ideas).
All of these have had the same results.
Only been programming a few weeks, I'd be very gratefull if someone could tell me what I'm doing wrong.
Not sure what you're trying to do with that line:
dice[i] = dice[i];
Since it's a NOP, why not just omit it?
I don't really see the purpose of:
dice[i] = dice[i]
Can't you just use something like:
for (int i=0; i<N_DICE; i++){
if (display.isDieSelected(i)){
dice [i] = rgen.nextInt(1, 6);
}
}
The code looks completely correct, although I would write it a bit more compactly:
for (int i = 0; i < N_DICE; i++) {
if (display.isDieSelected(i)) {
dice[i] = rgen.nextInt(1, 6);
}
}
So your problem probably lies somewhere else. Is dice a new array, or is it always the same in the program? If it is a field in some class, it should have the final modifier, like this:
public class YahtzeeState {
private final int[] dice = new int[N_DICE];
}
This declaration makes sure that the dice array cannot be replaced later with a completely different array. The values in the array can still be changed though.
How is the dice-array initialized? I don't know your entire code but I could imagine that it doesn't work because the dice-array gets it values only in this loop?
Maybe you should try something like:
for (int i=0; i<N_DICE; i++){
if (display.isDieSelected(i) || dice[i] == null)
dice [i] = rgen.nextInt(1, 6);
}