0
I need help with the Java logical and OR challenge I can't figure it out
11 Réponses
+ 5
To check if an integer is a multiple of another integer you can use the modulo operator %. If the remainder is 0 then it is a multiple of that number.
Example: to check if a number is a multiple of 10 use;
num % 10 == 0
So if you wanted to check if a number was a multiple of both 10 AND 9 (i.e. 90, 180, 900, etc), and then run some code if true you could do;
if (num % 10 == 0 && num % 9 == 0) {
... code to run if true ...
}
For either 4 OR 6;
if (num % 4 == 0 || num % 6 == 0) {
... code to run if true ...
}
+ 2
num is a variable in my examples, so you just store the number input by the user in a variable making sure you converted it to the correct type. nextInt()
+ 2
The source of the number is irrelevant, the code ChaoticDawg notes above works for a number no matter it's source.
+ 2
#Dan Carney:
else {System.out.println("Try again");}
I hope will be work.
+ 1
struggling with this one. it passes tests 1-4 but not the 5th one. any sense on what im doing wrong here?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int ticketNumber = read.nextInt();
int zero = 0;
if (ticketNumber%9 == zero && ticketNumber%10 == zero) {System.out.println("You won $200");}
else if (ticketNumber%4 == zero || ticketNumber%6 == zero){System.out.println("You won $50");}
else {System.out.println("Please try again");}
}
}
0
Yes I am suppose to create a program were I use both to decided on a winners of lottery tickets
It's not so that don't know how to use the logical and OR
It the fact that the winner have to be decided based on a multi of a number
Like if the ticket number
Is equal to a multiple of 10 and 9 they win the first prize
Or they have ether a multiple of 4 or 6
That's what throws me of
0
Hello Chaotic Dawg thank you it worked
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int num = read.nextInt();
int z = 0;
//your code goes here
//Hint: Number a is a multiple of number b, if a%b
//If the ticket number is a multiple of 10 and 9, the program outputs “You won $200”.
if(num%10 == z && num%9 == z)
{System.out.println("You won $200");}
//If it is a multiple of 4 or 6, the program outputs “You won $50”.
else if(num%4 == z || num%6 == z)
{System.out.println("You won $50");}
//Otherwise, there is no prize and the output is “Try again”.
else { System.out.println("Try again");}
}
}
//FZ
// I hope you guys can understand my code
- 1
And what if the number comes from a user input
- 1
Yes I did see
Once again thanks
And there any other practice test you have I be
- 1
Glad to try it out