Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It is coming that
/sc_squareofgreaterandrootoflesser.java:15: error: unexpected type
(n1>n2) ? r1 = n1*n1 : r2 = Math.sqrt(n2);
^
required: variable
found: value
/sc_squareofgreaterandrootoflesser.java:16: error: unexpected type
(n2>n1) ? r2 = n2*n2 : r2 = Math.sqrt(n1);
^
required: variable
found: value

What I have tried:

// to find the square of the greater and the square root of the lesser
import java.io.*;
import java.util.*;

public class sc_squareofgreaterandrootoflesser
{
public void main()throws IOException
{
Scanner sc = new Scanner(System.in);
double n1,n2,r1,r2;
System.out.println("Enter the first number: ");
n1 = sc.nextInt();
System.out.println("Enter the second number: ");
n2 = sc.nextInt();
(n1>n2) ? r1 = n1*n1 : r2 = Math.sqrt(n2);
(n2>n1) ? r2 = n2*n2 : r2 = Math.sqrt(n1);
System.out.println("Square of greater = " + r1 );
System.out.println("Square root of the lesser = " + r2);
}
}
Posted
Updated 21-Jul-22 5:38am
Comments
Mighty Abhishek Music time 21-Jul-22 10:27am    
please say how to correct that mistake
Richard MacCutchan 21-Jul-22 10:47am    
You could start by tagging your question with the correct language: Java, not Javascript.
Patrice T 21-Jul-22 12:37pm    
What is the problem with your code ?

This corrects your syntax errors, but you still need to fix your logic.
Java
Scanner sc = new Scanner(System.in);
double n1,n2,r1,r2;
System.out.println("Enter the first number: ");
n1 = sc.nextInt();
System.out.println("Enter the second number: ");
n2 = sc.nextInt();
r1 = r2 = 0.0;
if (n1>n2) {
    r1 = n1*n1;
}
else
{
    r2 = Math.sqrt(n2);
}
if (n2>n1) {
    r1 = n2*n2;
}
else {
    r2 = Math.sqrt(n1);
}
System.out.println("Square of greater = " + r1 );
System.out.println("Square root of the lesser = " + r2);
 
Share this answer
 
To add to what Richard has said ... You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900