Write a java program to repeatedly get two numbers from the user and display the sum of their squares. User numbers and result are real numbers. Repeat this interactive input/output until the first number exactly matches the sentinel value 990. Do not process the input matching the sentinel value. For each input use at most one prompting message on the same line the user will type their two numbers. For each set, place a message in front of the computer's answer which identifies it. The identifying message should end in an = sign (prefixed legend)

I think i got it right but is there a better way to write this because it just seems like it can be done better??????

mport java.util.*;
public class Main
{
public static void main(String[] args)
   {double a,b,result;
    Scanner in=new Scanner(System.in);
    System.out.print("Enter 2 numbers(first is 990 to exit): ");
    a=in.nextDouble();
    while(a!=990)
       {b=in.nextInt();
        result=a*a+b*b;
        System.out.println(a+"^2+"+b+"^2="+result);
       System.out.print("Enter 2 numbers(first is 990 to exit): ");
       a=in.nextDouble();
        }
}   
}

hmm.. I havent tested the code so I assume it runs perfectly. One way to eliminate 1 line would be by using the "do-while" loop instead of the "while" loop.
The difference between both being that do-while will execute the code within it atleast once before check for the stopping condition. Your program then looks like:

import java.util.*;
public class Main
{
    public static void main (String[] args)
    {
        double a, b, result;
        Scanner in = new Scanner (System.in);
        
        do
        {
            System.out.print ("Enter First number(990 to exit): ");
            a = in.nextDouble ();
            System.out.print ("Enter Second number: ");
            b = in.nextDouble ();
            result = (a * a) + (b * b);
            System.out.println (a + "^2+" + b + "^2=" + result);
        }
        while (a != 990)
    }
}

The downside of this is that it will execute the whole thing for 990 ATLEAST ONCE, in the beginning. Apart from that, yours and mine seem to be the only 2 ways that come to mind. Although mine doesn't follow specification and so is not a good choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.