0

HHeeeeellllpppp java

java code problem Please, the code contains errors, and I do not know how to amend them. When I enter the correct answers, they appear wrong Program of competitions to divide two randomly generated numbers, taking into account that division by zero is forbidden. try it on another editor not sololearn write it as a code please import java.util.Scanner; public class Lap5_2Sec4 { public static void main(String[] args) { int number1=(int)(Math.random()*30); int number2=(int)(Math.random()*30); if (number1<number2){ int temp=number1; number1=number2; number2=temp; } System.out.print("What is "+number1+"/"+number2+"?"); Scanner input = new Scanner (System.in); int answer= input.nextInt(); if (number1/number2==answer) System.out.println("You are correct"); else System.out.println("Your answer is wrong.\n"+number1+"/"+number2+"could be"+(number1/number2)); }}

26th Nov 2020, 6:38 PM
STOP
STOP - avatar
4 Réponses
+ 1
It's giving the correct answers to how it is coded. You're using integer division so the fractional part is truncated. I.E. 12/5 is 2 not 2.4 Also, you don't handle a division by 0. You can either do a check on the denominator, use a try-catch, or change how you get your random numbers.
26th Nov 2020, 6:50 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
It works fine. To handle 0 you can add 1 at the end of the random number. https://code.sololearn.com/czEbmJd45S76/?ref=app
26th Nov 2020, 6:54 PM
Avinesh
Avinesh - avatar
0
make it as a code
26th Nov 2020, 6:55 PM
STOP
STOP - avatar
0
What is your correct answer? when they appearing wrong? Your code have no errors...
26th Nov 2020, 8:01 PM
Jayakrishna 🇮🇳