EDIT: Voll-Code für Menschen, die es brauchen: https://hastebin.com/tovazutuni.csJava: Wie ändert man den Wert einer Variablen außerhalb eines Schalters? (Lesen Sie einfach die Post)
ok so im auf einer großen Burger Shop Herausforderung zu arbeiten, aber es einfach zu halten, ist hier ein similair Problem im Besatz:
public class Burger {
private double priceWithAdditions = 5.00;
private double price = 5.00;
private String addition1;
private double addition1Price;
switch(burgersAdditionScanner()){
case "mayo":
this.addition1 = "Mayo";
this.addition1Price = 0.40;
this.priceWithAdditions += this.addition1Price;
System.out.println("Added Additions " + this.addition1 + " for the price of $" +this.addition1Price);
break;
default: System.out.println("Get Out");
später unten im Code, habe ich eine Methode, um die Quittung zu drucken, aber die Werte der Variablen innerhalb des Schalters verändert nicht außerhalb davon aktualisieren:
public void Receipt() {
System.out.println("You Bought " + this.name + " burger for the price of $" + this.price + " with the following \nAdditions:");
if(this.addition1 != null) {
System.out.println("Addition " + this.addition1 + " for $" + this.addition1Price);
}
System.out.println("The final price is " + this.priceWithAdditions); }
die Werte von Addition1 Unter Berücksichtigung und priceWithAdditions hat sich in dem Fall nicht geändert, addition 1 ist null und priceWithAdditions ist immer noch 5,00 statt 5,40. Weiß jemand, wie man die Werte dieser Variablen an der Außenseite des Schalters aktualisieren kann? Wenn Sie mich Id wirklich helfen kann, es zu schätzen wissen, bitte und danke
Konsolenausgabe: (Es gibt einen Scanner, so kann ich meine Entscheidungen geben)
add addition 1
Choose Addition 1
Lettuce - $0.50
Cheese - $1.00
Ketchup - $0.20
Mayo - $0.40
mayo
Added Addition Mayonaise for the price of $0.4
Add Additions or none
Add Addition 1
Add Addition 2
Add Adition 3
Add Adition 4
Receipt
receipt
You Bought Amer's Booty burger for the price of $5.0 with the following
Additions:
The final price is 5.0
Get Out
Sie sollten Ihren Code formatieren – Steephen
Sie scheinen wirklich keine Ihrer Ergänzungen zu "this.priceWithAdditions" hinzuzufügen. –
@ElliottFrisch Wenn du nach "Mayo" suchst, habe ich dies geschrieben.priceWithAdditions + = this.addition1Price; (Das ist im Grunde das gleiche wie dies. Preis mit Zusätzen = this.priceWithAdditions + this.addition1Price) Der addition1Price ist 0.40, also sollte es 5.40 nicht 5.00 ausdrucken, aber es geht weiter zu 5.00 –