Java for every odd multiply of 90 [closed] - java

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 8 years ago.
Improve this question
I need to create a program which prints something for every odd multiply of 90 so for example
90 - text
180 - nothing happens
270 - text
360 - nothing happens
etc.
Thanks a lot!

A simpler solution is to print the multiples of 180.
for(int i = 180; i < max; i += 180)
System.out.println(i);
I assume this is homework, but there is no point copying someone's answer unless you can explain it. This is for you education purposes. (And your marker will be able to pick up code you probably didn't write yourself)

Your basic structure here should be pretty simple - just use a multiplier and an if statement with a modulus operator.
For example (pseudo-code - check a tutorial if you can't implement this idea):
int multiplier=1;
int maxMultiplier = 10;
int value = 0;
while (multiplier < maxMultiplier) {
value = 90 * multiplier;
if (multiplier % 2 == 0) {
// print something;
}
multiplier++;
}

Related

Convert Java loop to Kotlin [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm studying implementation with Kotlin. When I implement Kotlin version by below situation, I can't imagine cool way.
for(i = 0 ; i + 8 <= table.size; i++){
for(j = 0 ; j + 8 <= table[0].size; j++{
whatever
}
}
Above code is Java version
for(i in 0 until table.size){
if(i+8 > table.size)break
for(j in until table[0].size){
if(j+8 > table[0].size)break
whatever
}
}
Above is Kotlin version which I think.
Is this fine way?
You can just move the -8 into the upper limit, and since you include (<=) the upper limit you shouldn't be using until, but the regular range expansion with two dots.
So it becomes:
for (i in 0..table.size-8){
for (j in 0..table[i].size-8){}
}
(I imagine you would also want to replace the magical number eight with a variable with a meaningful name)

How do I display all numbers that end with the number 3 with a for loop in java? [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 need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(i);
}
}

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

Java: create a new variable or do multiple times the same calculation? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
i have this piece of code:
// ...
if (result[1] / (abs ? res[0] : 1 > highest) {
highest = result[1] / (abs ? res[0] : 1);
}
and I'm wondering if it's Worth enought to create a variable to avoid repeating the same calculation:
// ...
double temp = result[1] / (abs ? res[0] : 1;
if (temp > highest) highest = temp;
Which is the best soluton and also in general when is better to use the first piece of code and when the second one?
The second one is better:
1) It has better performance.
2) It adds to the readability of the code.
3) It's easier to debug when the calculation happens only once.
4) The first option is error prone since you might make a mistake when copying the calculation.
You eliminate duplicate calculation primarily to avoid repeating the same code; repeating the same computation for more optimal use of CPU comes a distant second.
Therefore, it is absolutely worth creating a temporary variable.
However, in this situation you can avoid creating it explicitly by using Math.max:
highest = Math.max(highest, result[1] / (abs ? res[0] : 1));
Here, the second parameter b of max(double a, double b) is used in place of a temporary variable. The value of the expression argument is set into the parameter variable at the call site, and used inside the max implementation to decide the return value, and ultimately the assignment.

Extracting a specific digit from an integer [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 8 years ago.
Improve this question
I'm rather sure I haven't found an answer to this because I'm not sure of the correct term for what I'm trying to do, so apologies in advance if it's very straight forward/well documented.
I have a set of numbers which I need to output a number in a specific position (hundreds), IE:
For 1302, I need to output 3
For 1802, I need to output 8
etc
How can I accomplish this with Java?
I should note that this is easy with 100's ( / 100), however I can't seem to figure out how to do this when the number is > 100.
a easy way is use % (modulo) and then /
like :
(input % 1000) / 100
Edit It works if you use only int number.
If you want to keep the number,
(num / 100) % 10
should generally work.
If you want to output the second number of your number you could convert it to a String and then use the substring method (doc) to get the character you want. (of course it only works if the number you want is always located at the same position).
An example of one solution, to get you on your way :)
int i = 1302;
String hundreds = Integer.toString(i).substring(1, 2);
System.out.println(hundreds);

Categories

Resources