0.127
Это тип Double, значение 0.127
Это тип String, значение 0.127
Это тип String, значение 0.127
127
Это тип short, значение 127
hhghg
Это тип String, значение hhghg
Это тип String, значение hhghg
128
Это тип Integer, значение 128
exit
Process finished with exit code 0
Ответы выглядят так? Почему int и short по одной строчке в ответе, а Double и String нет? И валидатор не принимает. Благодарю.
package com.javarush.task.task15.task1519;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Разные методы для разных типов
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String str = reader.readLine();
if (str.equals("exit")) {
break;
} else {
if (str.contains(".")) {
try {
Double n = Double.parseDouble(str);
print((Double) n);
} catch (NumberFormatException e) {
print((String) str);
}
}if (true) {
try {
Integer n = Integer.parseInt(str);
if (n <= 0 || n >= 128)
print((Integer) n);
} catch (NumberFormatException e) {
print((String) str);
}
} if (true) {
try {
Short n = Short.parseShort(str);
if (n > 0 && n < 128)
print((short) n);
} catch (NumberFormatException e) {
print((String) str);
}
} else
print((String) str);
}
}
}//напиште тут ваш код
public static void print (Double value){
System.out.println("Это тип Double, значение " + value);
}
public static void print (String value){
System.out.println("Это тип String, значение " + value);
}
public static void print ( short value){
System.out.println("Это тип short, значение " + value);
}
public static void print (Integer value){
System.out.println("Это тип Integer, значение " + value);
}
}