2017-10-30 1 views
-5

Ich habe ein kleines Problem mit diesem Programm, das ich schreibe. Wenn ich dies mit CMD ausführe, gibt es mir ein paar Fehler, wie die Variable "totalFry" kann nicht definiert werden, obwohl es eine globale Variable ist. Jede Hilfe wäre großartig, vielen Dank! (Hinweis: Dies ist meine erste Zeit mit dem Schreiben Looping/while Code)Java-Looping-Programm kann keine Symbole erkennen

  • Die Idee der Benutzer 3 Möglichkeiten zu geben, haben die Benutzereingabe eine Option (1-3) und haben die Optionen aufzurufen erscheinen der Benutzer, den Betrag einzugeben, den sie für besagtes Team wünschen.
  • Als nächstes wird das Programm fragt den Benutzer , wenn sie den Auftrag beenden wollen, vorausgesetzt, sie Eingang „Ja“ wird das Programm fragen, ob der Benutzer das Programm, wenn die Benutzereingaben „Ja“ wird das Programm beenden Geh und addiere die Kosten und zeige sie an.
  • Programmende

Code:

import java.io.*; 
import java.util.Scanner; 

//Lab 4_5 
public class Lab4_5 { 
    static Scanner keyboard = new Scanner(System.in); 

    // Declare local variables 
    static double totalBurger, totalFry, totalSoda; 
    static int option, burgerCount, fryCount, sodaCount; 

    // The main method 
    public static void main(String[] args) { 

     //Add a loop to run program again 
     String endProgram = "no"; 
     while (endProgram.equals("no")) 

      //Add a loop to take in order 
      String endOrder = "no"; 
     while (endOrder.equals("no")) 

      // Add statements to display the menu, get the user choice, and assign it to option 
      System.out.println("Enter 1 for Yum Yum Burger"); 
     System.out.println("Enter 2 for Grease Yum Fires"); 
     System.out.println("Enter 3 for Soda Yum"); 

     //Add a Select Case statement based on the value of option 
     int option = 0; 
     option = keyboardnextInt(); 

     //When option = 1 
     if (option == 1) 
      double totalBurger = 0; 
     int burgerCount = 0; 
     System.out.println("Enter the number of burgers you want "); 
     burgerCount = keyboard.nextDouble(); 
     totalBurger = totalBurger + burgerCount * .99; 
     //When option = 2 
     if (option == 2) 
      double totalFry = 0; 
     int fryCount = 0; 
     System.out.println("Enter the number of fires you want "); 
     fryCount = keyboard.nextDouble(); 
     totalFry = totalFry + fryCount * .79; 
     //When option = 3 
     if (option == 3) 
      double totalSoda = 0; 
     int sodaCount = 0; 
     System.out.println("Enter the number of sodas you want "); 
     sodaCount = keyboardnext.Double(); 
     totalSoda = totalSoda + sodaCount * 1.09; 

     //Imput if user wants to end order 
     System.out.println("Do you want to end your order? (Enter no to add more items) "); 
     endOrder = keyboard.next(); 

     //Call 
     clacTotal(); 
     printReceipt(); 

     //Ask user if they want to end the program 
     System.out.println("Do you want to end the program? (Enter no to process a new order) "); 
     endProgram = keyboard.next(); 
    } 

    //Calculate the total amount 
    public static void calcTotal() { 
     double total = 0; 
     double tax = 0; 
     double subtotal = 0; 
     //Add statements to calc total with tax and call printReceipt 
     calcTotal = totalBurger + totalFry + totalSoda * .06; 
    } 

    public static void printReceipt() { 
     //Add statements to display the total as shown above 
     System.out.println("Your total is $" + calcTotal); 
    } 
} 
+0

Bitte formatieren Sie Ihren Code richtig, damit wir es leicht verstehen können. Ich nehme an, dass einige '{' und '}' fehlen (um die Körper herum) – Heri

Antwort

0

Ihr Code hat viele Probleme, einschließlich der folgenden:

  1. geschweiften Klammern am Anfang und Ende des if's und loops
  2. fehlt Erstellen von doppelten Variablen.
  3. mit keyboard.nextDouble() auf Ints

plus einige Rechtschreibfehler.

Der folgende Code sollte mindestens kompilieren und funktionieren, wie Sie wollten.

import java.util.Scanner; 

public class Lab4_5 { 
static Scanner keyboard = new Scanner(System.in); 

// Declare local variables 
static double totalBurger, totalFry, totalSoda, calcTotal; 
static int option, burgerCount, fryCount, sodaCount; 

// The main method 
public static void main(String[] args) { 

    //Add a loop to run program again 
    String endProgram = "no"; 
    while (endProgram.equals("no")){ 

     //Add a loop to take in order 
     String endOrder = "no"; 
     while (endOrder.equals("no")){ 

     // Add statements to display the menu, get the user choice, and assign it to option 
      System.out.println("Enter 1 for Yum Yum Burger"); 
      System.out.println("Enter 2 for Grease Yum Fires"); 
      System.out.println("Enter 3 for Soda Yum"); 

      //Add a Select Case statement based on the value of option 
      int option = 0; 
      option = keyboard.nextInt(); 

      //When option = 1 
      if (option == 1){ 
       totalBurger = 0; 
       burgerCount = 0; 
       System.out.println("Enter the number of burgers you want "); 
       burgerCount = keyboard.nextInt(); 
       totalBurger = totalBurger + burgerCount * .99; 
      } 
      //When option = 2 
      if (option == 2){ 
       System.out.println("Enter the number of fires you want "); 
       fryCount = keyboard.nextInt(); 
       totalFry = totalFry + fryCount * .79; 
      } 
      //When option = 3 
      if (option == 3){ 
       System.out.println("Enter the number of sodas you want "); 
       sodaCount = keyboard.nextInt(); 
       totalSoda = totalSoda + sodaCount * 1.09; 
      } 

      //Imput if user wants to end order 
      System.out.println("Do you want to end your order? (Enter no to add more items) "); 
      endOrder = keyboard.next(); 
     } 

     calcTotal(); 
     printReceipt(); 

     //Ask user if they want to end the program 
     System.out.println("Do you want to end the program? (Enter no to process a new order) "); 
     endProgram = keyboard.next(); 
    } 
} 

    //Calculate the total amount 
    public static void calcTotal() { 

     //Add statements to calc total with tax and call printReceipt 
     calcTotal = totalBurger + totalFry + totalSoda * .06; 
    } 

public static void printReceipt() { 
    //Add statements to display the total as shown above 
    System.out.println("Your total is $" + calcTotal); 
    } 
} 
Verwandte Themen