Questions about if else conditionals
0. Write a program that receives the radius of a circle and shows the diameter and length of this area.
1. Write a program that receives three integers and tell which one is the highest and which the lowest. Can you create more than one solution?
2. Write a Java program that takes an integer and tell whether it is even or odd
Use the mathematical operator % (remainder of the division or module) and if conditional test.
3. Write a program that asks the coefficients of a quadratic equation and displays the roots of the equation, whether real or complex.
Solution
Challenge: Write a Java application that generates a random integer number between 1 and 10, and through conditional tests you have to guess what number this is.
What is the best technique, which guesses at least possible chance?
To generate a random number in the integer variable 'num_aleatorio', add in your program:
import java.util.Random;
To generate random numbers, create a type Random 'randomGenerator':
RandomGenerator Random = new Random ();
And declare a variable to receive the random number like this:
randomNum = randomGenerator.nextInt(10) + 1;
Marcadores:
Basic,
Exercises,
Flux Control
Subscribe to:
Post Comments (Atom)
1 comment:
package adivinhandonumeroentre1e10;
import java.util.Random;
public class AdivinhandoNumeroEntre1e10 {
public static void main(String[] args) {
Random randomGenerator = new Random();
int num_aleatorio;
num_aleatorio = randomGenerator.nextInt(10)+1;
for (int i=2; i<=10; i++){
if((i/num_aleatorio)==1){
System.out.printf("\nDescobri, o número gerado é: %d\n", i);
break;
}}
}
}
Post a Comment