Finding odd number results using the for loop [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am experiencing some problems with finding odd results w/ the user input. I am not sure how to code the for loop statement to get the odd results only. Current for loop statement is fixed to increment by two, but this will only work if the user enters odd numbers as well ex(0,5). Sorry i really dont have much experience with forloops, i tried nesting the for loops, but it only made it more confusing. This is my for-loop statement;
for(counter = num1; counter <= num2; counter+=2){ //this is my for loop statement
System.out.println(counter);//print out all of the odd values
}
}

If a user enters an even number, just fix it before the loop:
//if num1 is even, move it up to the next odd number
if (num1 % 2 == 0) {
num1++;
}
//print all of the odd values
for (int counter = num1; counter <= num2; counter+=2) {
System.out.println(counter);
}

As an alternative to the accepted answer:
//if num1 is even, move it up to the next odd number
num1 |= 1;
It probably doesn't really matter, but I think it's neater that way.

Related

How do I split an indice of an array into two different pices, I.E to validate that neither number is odd/even? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have to check, roomNumber[i], if BOTH numbers (assuming it's two-digit) are not even, and not odd. Only then can I accept the number. Also need to determine if the second digit is not less than double the first. I assume that I need to some how split these into two different ints (a,b), but I'm not sure.
If you assume that you have only two-digit numbers, you can go with it this way:
int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
int second = num % 10;
int first = (num - second) / 10;
System.out.println("first: " + first + " second: " + second);
}
You take the second number using the modulo (%) operator. To get the first number, you need to subtract the second number and divide it by 10.
Now you can process those numbers further.
Your questions have nothing to do with arrays. Let's reword them:
How do I test if a number is even?
You can google this and easily find the answer. Hint: use the modulus operator (%).
How do I test if the second digit is not less than double the first digit
Break this into smaller pieces: First you need to separate out the first and second digit. Again, you can google this if you don't know. (Hint: it's very similar to the first question. You can use the % operator and division (/).
Then you have to compare these according to the rule. Try to write something on your own and see what you can come up with.

Dice rolling through a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to make a program that takes the string "1d8" and make the program identify it as int i = (Int) ((Math.Random()*8)+1) one time. It would also be nice if I could make it identify "10d8" to do something like :
for(int i = 1; i <= 10; i++){
int j += (int) ((Math.Random()*8)+1);
}
Thus returning basically the roll of 10 eight sided dice. So my question is how do I get the code to recognize the numbers on either side of the character d and make this work with whatever roll I do.
You can use split method of the String class.
String s = "10d8";
String[] numbers = s.split("d");
numbers[0] will have 10 and numbers[1] will have 8

In need of help of with a while loop program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm a novice programmer is looking to get help with a program I've been working on for hours now.
Anyway my issue happens to be with initializing my "sum" variable in a way so that it is not equal a number or become an input. (It's set to equal 0 in my program)
I also seem to be struggling with my while loop statement as I can't think of a condition in which I don't cause the program to terminate or cause a infinite loop.
Any help at this point would highly appreciated.
I'm assuming you are trying to compute sum of integers from 1 to n, where n is input from the user.
One of the simpler ways to do that is to use a for loop as below
for(int i = 1; i <= n; i++) {
sum = sum + i;
}
or using while loop
while(input > 0) {
sum = sum + input;
input = input - 1;
}
Alternatively, sum of first N natural numbers is given by formula n*(n+1)/2, so you might as well do
int sum = (n * (n+1))/2;
Ensure that n is a positive number > 0 through an if conditional
your While loop is going infinity as your input is never 0 until you give it.
So you have to deal with your input.
Possibly use or declare input=0 so that when your while loop goes for the execution for 2nd Time...It finds 0.
Hope this helps...Please share doubts if any.

Generate a random integer within a given range AND is even ONLY [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having trouble generating this code which I'm sure I'm just in a coder's block or something because it seems as though it should be easy but can't get it for the life of me.
I have a program which needs random numbers generated within a certain range which is to represent money. The stipulation is that the money need be in between 2 and 200 and represent only even dollars only so $2, $4, $6...so on. I have done extensive searching online which yield a bounty of code to represent random numbers in a range in java but not the part about being only even.
Any ideas?
If you wanted to be clever, you could make sure the least significant bit of the number is not set:
int num = (new Random().nextInt(199) & ~1) + 2;
This will ensure that the number is always even.
Thanks Eyal Shneider and Omaha
Marcelo's comment from the OP is the correct answer, though.
Get a number between 1-100, and multiple by 2:
int num = (new Random().nextInt(100) + 1) * 2;
int rand = new Random.nextInt(200);
int result = 2;
while(result < rand) {
result += 2;
}
return result;
(I'd make it recursive but I've got to go meet my wife for dinner.)

Subroutine dice rolling assignment [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm just learning subroutines and I'm still very, very amateurish at this. My assignment asks me to toss two dice, display what the dice display (random numbers), display total of the two die, and let the program continue until 7 or 11 is the total. We haven't done anything on arrays, so I don't know if I should use them or not.
Also, I was going to use a for loop, but how would I get the program to stop at either 7 or 11 using it? Should I try another loop?
Please help guide me on what I have to do! I'm very confused about how to make the methods and put them into the main method. Just an explanation would do!
Thank you!
Here's the pseudocode for this problem. I won't give you the actual code but this will set you in the right direction:
Loop forever: {
Integer A = Random #1-6
Integer B = Random #1-6
Integer total = A + B
If total == 7 or total == 11 BREAK from the loop
Print total
}
Some more hints: Looping forever is usually achieved by setting up a while loop whose condition is always true. For example, while (true) or while (1 == 1).
Also, look into the java.util.Random class for generating a random number. It's really straightforward and it's good to start learning how to use the Java docs early in your learning process.
I would suggest a while loop. In your case,
instantiate the two dice
while the total isn't either 7 or 11 {
roll again
}
print out the result of the roll that wasn't either 7 or 11.
should be a reasonable starting point for you. I won't go into too much detail so that you still get the chance to implement the idea yourself and learn from it. If you don't know how to simulate a dice roll, #Kon's answer is helpful.
You may write a subroutine that returns the total number and call it in main method in while-loop
class Rolling-dice {
public static int roll() {
// roll the first dice and display the number
// roll the second dice and display the number
// return total`number
}
public static void main(String[] args) {
int result = roll();
// while result not 7 or 11 call roll()
}
}

Categories

Resources