Where am I getting it wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Where am I getting it wrong?

IDE exeption "The local variable answer may not have been initialized" package com.operators; import java.util.Scanner; public class SquareRoot { //@MMS public static void main(String[] args) { String answer; do { //Ask for user input System.out.print("Insert a number to calculate: "); //User types number to be calculated Scanner sc = new Scanner(System.in); double a; a = sc.nextDouble(); //Calculates square root if user types negative number if (a <= 0) { double ans = Math.sqrt(a*-1); System.out.println("The square root of "+a +" is " +ans+"i"); } //calculates square root if user types positive number else { double ans = Math.sqrt(a); System.out.println("The square root of "+a +" is " +ans); System.out.println("Do you want to calculate another number? YES or NO\n"); answer = sc.next(); sc.close(); } } while (answer.equalsIgnoreCase("YES")); } }

3rd Jul 2020, 7:36 PM
Michael
Michael - avatar
1 Answer
+ 1
Awsome. Thank you.
3rd Jul 2020, 7:50 PM
Michael
Michael - avatar