新手程序员试图创建一个Java餐厅菜单

时间:2013-05-10 13:21:03

标签: java menu project

我正在尝试为我的计算机科学课程做一个家庭作业的菜单。我以为我把所有东西都搞砸了,但是我一直遇到一些我似乎无法解决的错误。另外我只能使用Java 1.4.2,所以我不能使用Scanner。这是代码:

更新代码

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class JavaBurger
{
    static ArrayList MenuItems = new ArrayList();
    public static void main(String[] args)
    {
        InputStreamReader inp = null;
        BufferedReader input = null;
        int nOption  = 0;
    double price = 0;
    double amount;
    double amount1 = 0;
    double amount2 = 0;
    double amount3 = 0;
    double amount4 = 0;
    double amount5 = 0;
    double amount6 = 0;
    double amount7 = 0;
    double amount8 = 0;
    double amount9 = 0;
    double amount10 = 0;
    amount = (amount1 + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10);
        try
        {
            inp = new InputStreamReader(System.in);
            input = new BufferedReader(inp);
            while(true)
            {
        System.out.println("Choose a Menu Option");
                System.out.println("1. Burger - 13.49");
                System.out.println("2. Pasta - 16.79");
                System.out.println("3. Salad - 13.49");
                System.out.println("4. Salmon - 18.99");
                System.out.println("5. Chicken - 16.99");
                System.out.println("6. Nachos - 13.99");
        System.out.println("7. Soup - 6.99");
        System.out.println("8. Fajitas - 18.49");
        System.out.println("9. Ribs - 23.99");
        System.out.println("10. Calamari-9.99");
                System.out.println("\nChoose an option(1-10) >> ");
        System.out.println("Subtotal: $" + amount);
        System.out.println("Total: $" + (amount * 1.13));
                nOption = Integer.parseInt(input.readLine());

                switch(nOption)
                {
                    case 1:
                        Burger(input);
                        break;
            case 2:
                        Pasta(input);
                        break;
            case 3:
                        Salad(input);
                        break;
            case 4:
                        Salmon(input);
                        break;
            case 5:
                        Chicken(input);
                        break;
                    case 6:
                        Nachos(input);
                        break;
                    case 7:
                        Soup(input);
                        break;
                    case 8:
                        Fajitas(input);
                        break;
                    case 9:
                        Ribs(input);
                        break;
                    case 10:
                        Calamari(input);
                        break;

                }
            }
        }
        catch(Exception exp)
        {
        }
    }

    public static void Burger(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many Burgers would you like? ");
        int a = Integer.parseInt(input.readLine());
        int aa = a.nextInt();
        double aaa = Math.pow(1 + a, aa);
            amount1 = (aaa * 13.49);


                break;
        }
    }

     public static void Pasta(BufferedReader input) throws IOException
    {
        while(true)
        {

            System.out.println("How many orders of Pasta would you like? ");
        int b = Integer.parseInt(input.readLine());
        int bb = b.nextInt();
        double bbb = Math.pow(1 + b, bb);
            amount2 = (bbb * 16.79);


                break;
        }
    } public static void Salad(BufferedReader input) throws IOException
    {
        while(true)
        {

            System.out.println("How many Salads would you like? ");
        int c = Integer.parseInt(input.readLine());
        int cc = c.nextInt();
        double ccc = Math.pow(1 + c, cc);
            amount3 = (ccc * 13.49);


                break;
        }
    } public static void Salmon(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many orders of Salmon would you like? ");
        int d = Integer.parseInt(input.readLine());
        int dd = d.nextInt();
        double ddd = Math.pow(1 + d, dd);
            amount4 = (ddd * 18.99);


                break;
        }
    } public static void Chicken(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many orders of Chicken would you like? ");
        int e = Integer.parseInt(input.readLine());
        int ee = e.nextInt();
        double eee = Math.pow(1 + e, ee);
            amount5 = (eee * 16.99);


                break;
        }
    } public static void Nachos(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many orders of Nachos would you like? ");
        int f = Integer.parseInt(input.readLine());
        int ff = f.nextInt();
        double fff = Math.pow(1 + f, ff);
            amount6 = (fff * 13.99);


                break;
        }
    } public static void Soup(BufferedReader input) throws IOException
    {
        while(true)
        {

            System.out.println("How many orders of Soup would you like? ");
        int g = Integer.parseInt(input.readLine());
        int gg = g.nextInt();
        double ggg = Math.pow(1 + g, gg);
            amount7 = (ggg * 6.99);


                break;
        }
    } public static void Fajitas(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many orders of 2 Fajitas would you like? ");
        int h = Integer.parseInt(input.readLine());
        int hh = h.nextInt();
        double hhh = Math.pow(1 + h, hh);
            amount8 = (hhh * 18.49);


                break;
        }
    } public static void Ribs(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many racks of Ribs would you like? ");
        int i = Integer.parseInt(input.readLine());
        int ii = i.nextInt();
        double iii = Math.pow(1 + i, ii);
            amount9 = (iii * 23.99);


                break;
        }
    } public static void Calamari(BufferedReader input) throws IOException
    {

        while(true)
        {

            System.out.println("How many orders of Calamari would you like? ");
        int j = Integer.parseInt(input.readLine());
        int jj = j.nextInt();
        double jjj = Math.pow(1 + j, jj);
            amount10 = (jjj * 9.99);


                break;
        }
    }
    }

更新错误:

C:\Java\bin>javac JavaBurger.java
JavaBurger.java:98: error: int cannot be dereferenced
            int aa = a.nextInt();
                      ^
JavaBurger.java:100: error: cannot find symbol
            amount1 = (aaa * 13.49);
            ^
  symbol:   variable amount1
  location: class JavaBurger
JavaBurger.java:114: error: int cannot be dereferenced
            int bb = b.nextInt();
                      ^
JavaBurger.java:116: error: cannot find symbol
            amount2 = (bbb * 16.79);
            ^
  symbol:   variable amount2
  location: class JavaBurger
JavaBurger.java:128: error: int cannot be dereferenced
            int cc = c.nextInt();
                      ^
JavaBurger.java:130: error: cannot find symbol
            amount3 = (ccc * 13.49);
            ^
  symbol:   variable amount3
  location: class JavaBurger
JavaBurger.java:143: error: int cannot be dereferenced
            int dd = d.nextInt();
                      ^
JavaBurger.java:145: error: cannot find symbol
            amount4 = (ddd * 18.99);
            ^
  symbol:   variable amount4
  location: class JavaBurger
JavaBurger.java:158: error: int cannot be dereferenced
            int ee = e.nextInt();
                      ^
JavaBurger.java:160: error: cannot find symbol
            amount5 = (eee * 16.99);
            ^
  symbol:   variable amount5
  location: class JavaBurger
JavaBurger.java:173: error: int cannot be dereferenced
            int ff = f.nextInt();
                      ^
JavaBurger.java:175: error: cannot find symbol
            amount6 = (fff * 13.99);
            ^
  symbol:   variable amount6
  location: class JavaBurger
JavaBurger.java:187: error: int cannot be dereferenced
            int gg = g.nextInt();
                      ^
JavaBurger.java:189: error: cannot find symbol
            amount7 = (ggg * 6.99);
            ^
  symbol:   variable amount7
  location: class JavaBurger
JavaBurger.java:202: error: int cannot be dereferenced
            int hh = h.nextInt();
                      ^
JavaBurger.java:204: error: cannot find symbol
            amount8 = (hhh * 18.49);
            ^
  symbol:   variable amount8
  location: class JavaBurger
JavaBurger.java:217: error: int cannot be dereferenced
            int ii = i.nextInt();
                      ^
JavaBurger.java:219: error: cannot find symbol
            amount9 = (iii * 23.99);
            ^
  symbol:   variable amount9
  location: class JavaBurger
JavaBurger.java:232: error: int cannot be dereferenced
            int jj = j.nextInt();
                      ^
JavaBurger.java:234: error: cannot find symbol
            amount10 = (jjj * 9.99);
            ^
  symbol:   variable amount10
  location: class JavaBurger
20 errors

3 个答案:

答案 0 :(得分:1)

错误:

您正在使用null变量:

double amount;
double amount1;

这些都没有用任何值初始化。它们都是null。您可以内联定义并为它们提供所有正确的值。

double amount = 0, amount1 = 2, amount3 = 5;//etc

您正在定义金额两次:

double amount; 
double amount = (amount1 + amount2 + amount3 + amount4 + amount5 + amoun
t6 + amount7 + amount8 + amount9 + amount10);

从第二个中删除double

amount = (amount1 + amount2 + amount3 + amount4 + amount5 + amoun
t6 + amount7 + amount8 + amount9 + amount10);

接下来,您始终需要从String投射到Double

(input.readLine() * 18.49); // should be
Double.parseDouble(input.readLine()) * 18.49;

readLine()方法不返回数值。

答案 1 :(得分:0)

您尝试两次声明amount

double amount;
double amount1;
[...]
double amount = (amount1 + amount[...]

从最后一行删除double,使其只是一项作业。

amount1 = (input.readInt * 13.49);

BufferedReader没有readInt方法。您可能希望将其替换为

amount1 = (Integer.parseInt(input.readLine) * 13.49);

答案 2 :(得分:0)

尝试将您的金额声明为主要方法之外的全局变量。 cannot find symbol错误的含义是程序无法访问您告诉它访问的变量。

此外,您可能希望在更新每种方法中的原始金额后尝试更新总金额。