java循环程序无法检测符号

时间:2017-10-30 17:29:09

标签: java loops while-loop

我写的这个程序有点问题。当我使用CMD运行它时,它会给我一些错误,例如变量" totalFry"即使它是全局变量也无法定义。任何帮助都会很棒,非常感谢! (注意:这是我第一次编写循环/代码时)

  • 这个想法是给用户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);
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码存在许多问题,包括:

  1. if'sloops
  2. 的开头和结尾缺少大括号
  3. 创建重复变量。
  4. 在ints上使用keyboard.nextDouble()
  5. 加上一些拼写错误。

    以下代码至少应该按照您的需要编译和工作。

    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);
      }
    }