0

I need help with the Java logical and OR challenge I can't figure it out

21st Oct 2020, 4:50 PM
Carlton
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 ... }
21st Oct 2020, 8:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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()
21st Oct 2020, 8:10 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
The source of the number is irrelevant, the code ChaoticDawg notes above works for a number no matter it's source.
21st Oct 2020, 8:12 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
#Dan Carney: else {System.out.println("Try again");} I hope will be work.
19th Jul 2021, 8:51 AM
Aziz Ergashev
Aziz Ergashev - avatar
+ 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");} } }
23rd Nov 2020, 3:57 AM
Dan Carney
Dan Carney - avatar
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
21st Oct 2020, 7:30 PM
Carlton
0
Hello Chaotic Dawg thank you it worked
21st Oct 2020, 8:10 PM
Carlton
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
13th Feb 2022, 10:05 AM
Fabian Zelaya
Fabian Zelaya - avatar
- 1
And what if the number comes from a user input
21st Oct 2020, 8:08 PM
Carlton
- 1
Yes I did see Once again thanks And there any other practice test you have I be
21st Oct 2020, 8:16 PM
Carlton
- 1
Glad to try it out
21st Oct 2020, 8:17 PM
Carlton