Java:
import java.util.Scanner;
public class Betrag {
public static int betrag(int wert) {
if(wert<0) {
wert = wert *-1;
}
return wert;
}
public static double betrag(double wert) {
if(wert<0) {
wert = wert *-1;
}
return wert;
}
public static float betrag(float wert) {
if(wert<0) {
wert = wert *-1;
}
return wert;
}
public static short betrag(short wert) {
if(wert<0) {
wert =(short)( wert *-1);
}
return wert;
}
public static long betrag(long wert) {
if(wert<0) {
wert = wert *-1;
}
return wert;
}
public static void main (String[]args) {
Scanner s = new Scanner(System.in);
int eingabe = 0;
do {
System.out.println("welchen Datentyp wollen sie übergeben?");
String a = s.nextLine();
if(a.equals("int")) {
System.out.println("welche Zahl wollen sie übergeben?");
int x = s.nextInt();
System.out.println("Betrag: "+betrag(x));
}
else if (a.equals("double")) {
System.out.println("welche Zahl wollen sie übergeben?");
double x = s.nextDouble();
System.out.println("Betrag: "+betrag(x));
}
else if (a.equals("float")) {
System.out.println("welche Zahl wollen sie übergeben?");
float x = s.nextFloat();
System.out.println("Betrag: "+betrag(x));
}
else if (a.equals("short")) {
System.out.println("welche Zahl wollen sie übergeben?");
short x = s.nextShort();
System.out.println("Betrag: "+betrag(x));
}
else if (a.equals("long")) {
System.out.println("welche Zahl wollen sie übergeben?");
long x = s.nextLong();
System.out.println("Betrag: "+betrag(x));
}
else if (a.equals("beenden")) {
System.exit(0);
}
eingabe ++;
}
while(eingabe != 10);
System.out.println("Programm ist vorbei");
}
}