Java :: Aufgabe #20
9 Lösungen

Programmier-Basics: Rabattaktion
Anfänger - Java
von Freki
- 27.12.2012 um 14:53 Uhr
Während einer Sonderaktion wird ein Rabatt von 10% auf alle
Einkäufe mit einem Gesamtbetrag von mehr als $10.00 gewährt.
Schreiben Sie ein Programm, das nach dem Gesamtbetrag fragt
und den Rabatt berechnet.
Der Gesamtbetrag wird in Cent (als Ganzzahl) eingegeben.
Einkäufe mit einem Gesamtbetrag von mehr als $10.00 gewährt.
Schreiben Sie ein Programm, das nach dem Gesamtbetrag fragt
und den Rabatt berechnet.
Der Gesamtbetrag wird in Cent (als Ganzzahl) eingegeben.
Konsolenausgabe:
Geben Sie den Gesamtbetrag ein:
2000
Discountpreis: 1800
Lösungen:
Main.java
Java-Code

package de.trainyourprogrammer.java20; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Calculate discount of ten percent on shopping for over ten dollar. * * @author jsb */ public class Main { /** * Execute the program. * * @param args * Is ignored in our case. */ public static void main(String[] args) { System.out.println("Geben Sie den Gesamtbetrag ein:"); // print the // request BufferedReader buffer = new BufferedReader(new InputStreamReader( System.in)); // start reading from the default input try { // try to... int i = Integer.parseInt(buffer.readLine()); // wait for an input if (i > 1000) { // if price is greater than ten dollar i = (i / 100) * 90; // grant ten percent discount } System.out.println("Discountpreis: " + i); // print the result } catch (NumberFormatException e) { // if there were no valid input main(new String[0]); // restart the program } catch (IOException e) { // ignore occurring IOExceptions } finally { // on quit... try { // try to... buffer.close(); // stop reading the input } catch (IOException e) { // again ignore occurringIOExceptions } } } }

/** * @author ElPapito * @date 08.05.2015 */ import java.util.Scanner; public class Rabattaktion { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Geben Sie den Gesamtbetrag ein: "); int total = scanner.nextInt(); scanner.close(); if (total > 1000) { total = (int) (Math.round(total * 0.9)); } System.out.println("Discountpreis: " + total); } }

import java.util.Scanner; public class Kasse{ public static void main(String[] args) { System.out.println("Geben Sie den Gesamtbetrag ein:"); Scanner inScann = new Scanner(System.in); int betrag = inScann.nextInt(); if(betrag > 1000){ System.out.println("Neuer zu zahlender betrag mit Rabatt " + (int)(betrag * 0.9)+"$"); } else{ System.out.println("Zahlen Sie"+ betrag +"$"); } } }

import java.util.Scanner; public class Rabatt { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Bitte Betrag eingeben: "); int betrag = s.nextInt(); if (betrag >= 2000) { System.out.println("Discountpreis: " + (betrag - (betrag / 10))); } s.close(); } }

public class Rabattrechner { public static void main (String[]args) { Scanner input = new Scanner(System.in); System.out.println("Geben Sie den Gesamtbetrag ein."); int sc = input.nextInt(); double s = sc; if (sc >= 10) { s = s*0.9; System.out.println("Discountpreis: "+s+" €"); } } }
Anmerkung: Ich arbeite mit dem Programm BlueJ
Java-Code

import javax.swing.JOptionPane; import java.text.DecimalFormat; /** * Berechnungen des Preises eines Einkaufs, welcher ab 10 Dollar 10% Rabatt bekommt. * * @author (nOrdan) * @version (03.06.2019) */ public class Kasse { DecimalFormat df = new DecimalFormat("0.00"); public static void main(String [] args) { Kasse k = new Kasse(); k.inputs(); } private void inputs() { boolean valid1 = false; int betrag = 0; while (valid1 == false) { String input1 = userInput("Geben sie ihren Gesamtbetrag des Einkaufs in Cent ein"); errorUserInput(input1); try { betrag = parseInt(input1); valid1 = true; } catch(Exception e) { errorMessage("Invalid user input","Invalid input"); } } berechnungen(betrag); } private void berechnungen(int betrag) { if (betrag > 1000) { informationMessage("Die Kosten für ihren Einkauf betragen " + df.format((betrag * 0.9)) + " Cent","Preis"); } else { informationMessage("Die Kosten für ihren Einkauf betragen " + betrag + " Cent","Preis"); } } private String userInput(String message) { return JOptionPane.showInputDialog(message); } private int parseInt(String input) { return Integer.parseInt(input); } private void errorUserInput(String input) { if (input == null) { System.exit(0); //Drückt der User auf abbrechen wird null zurück gegeben und das Programm wird beendet } else if (input.length() == 0) { //continue; wenn nötig } } private void errorMessage(String message,String errorName) { JOptionPane.showMessageDialog(null,message,errorName,JOptionPane.ERROR_MESSAGE); } private void informationMessage(String message,String informationName) { JOptionPane.showMessageDialog(null,message,informationName,JOptionPane.INFORMATION_MESSAGE); } }

import java.util.*; public class Rabattaktion { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double gesamtbetrag, rabatt; System.out.print("Geben Sie den Gesamtbetrag ein:\t"); gesamtbetrag = scanner.nextDouble(); if(gesamtbetrag > 10) { rabatt = gesamtbetrag - (gesamtbetrag * 0.1); } else rabatt = gesamtbetrag; System.out.print("Discountpreis:\t" + rabatt); scanner.close(); } }

import java.util.Scanner; public class Rabattaktion { public static void main(String[] args) { fragen(); } public static void fragen() { Scanner scan = new Scanner(System.in); String stEingabe; System.out.println("Geben Sie den Gesamtbetrag ein:"); stEingabe = scan.nextLine(); berechnePreis(stEingabe); } public static void berechnePreis(String stEingabe) { int gesamtbetrag; double discountpreis; try { gesamtbetrag = Integer.parseInt(stEingabe); if (gesamtbetrag > 1000) { discountpreis = gesamtbetrag * 0.9; } else { discountpreis = gesamtbetrag; } System.out.println("Discountpreis: " + Integer.toString((int) discountpreis)); } catch (NumberFormatException e) { System.out.println("Bitte geben Sie ganze Zahl ein!"); fragen(); } } }

import java.util.Scanner; public class ProgrammierBasicsRabattaktion { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Geben sie den Gesamtbetrag ein:"); int betrag = scanner.nextInt(); if (betrag > 10000) { betrag = betrag - (betrag / 100 * 10); } System.out.println("Discountpreis: " + betrag); } }