Cześć,
wpisując wartość zmiennoprzecinkową z klawiatury, w konsoli wyświetlany jest błąd:
Wprowadź liczbę:
1.2
1.2 Nie jest liczbą
proszę o podpowiedź, jak można to rozwiązać.
package com.operacjelogiczne;
import java.util.Scanner;
public class Logarithm {
public static void printLogarithm( double x) {
if(x <= 0.0) {
System.err.println("Błąd: x musi być dodatnie.");
return;
}
double result = Math.log(x);
System.out.println("Logarytm x wynosi " + result);
}
public static void main(String[] args) {
printLogarithm(3.0);
scanDouble();
}
public static void scanDouble() {
Scanner in = new Scanner(System.in);
System.out.println("Wprowadź liczbę: ");
if(!in.hasNextDouble()) {
String word = in.next();
System.err.println(word + " Nie jest liczbą");
return;
}
double x = in.nextDouble();
printLogarithm(x);
}
}