2017-10-30 3 views
-5

私はこのプログラムに少し問題があります。私はこれをCMDで実行すると、グローバル変数であるにもかかわらず、変数 "totalFry" cantを定義するなど、いくつかのエラーが表示されます。どんな助けも素晴らしいです、どうもありがとう! (注:これは、コードながら/ルーピング書い初めてです)Javaルーピングプログラムがカントを検出できない

  • アイデアは、ユーザ3つの選択肢を与えることである、ユーザー入力オプション (1-3)を持っているとオプションを促すように思われると述べています前記チームに希望する金額の を入力します。
  • ユーザーの入力した場合、ユーザーがプログラムを終了したい場合次に、彼らは秩序を終了したい場合は、プログラムが彼らの入力を想定し、ユーザー を聞いてきます「はい」プログラム は「はい」と聞いてきますプログラム意志行ってコストを合計して表示します。
  • プログラム

コードの終わり:

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

わかりやすいようにコードを正しくフォーマットしてください。私はいくつかの '{'と '}'が欠落していると仮定します(while bodyの周り) – Heri

答えて

0

あなたのコードは次のような多くの問題があります。

  1. 始まりとif'sloops
  2. の端にある中括弧が欠落重複する変数を作成します。
  3. int型

プラスいくつかのスペルミスにkeyboard.nextDouble()を使用しました。

次のコードは、少なくともコンパイルして、必要に応じて動作させる必要があります。

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); 
    } 
} 
関連する問題