Java:存储选定的值以供以后使用

时间:2012-01-14 17:13:19

标签: java

我需要帮助的是,我正在通过创建以下简单的销售计算器来学习Jave。我在此代码中有一个部分如下:

System.out.println ("Please make a selection:");
System.out.println ("1. Octopus Tentacles, 2. Rat Droppings");
System.out.println ("3. Sharks teeth, 4. Mountain Oysters");
System.out.println (" ");
// Since we don't have a primary database to pull from we use a dummy DB
while ( foodCount <= 1 )
{
System.out.print( "Enter selection (1 , 2, 3, 4): ");
result = input.nextInt();

if ( result == 1)
    System.out.println( "You selected Octopus tentacles, price $4.99" );
else if ( result == 2)
    System.out.println( "You selected Rat droppings, price $3.49" );
else if ( result == 3)
    System.out.println( "You selected Sharks teeth, price $6.73" );
else if ( result == 4)
    System.out.println( "You selected Mountain Oysters, price $9.95" );
break;
}

System.out.println (" ");

它允许我做出选择,我想要做的是将该值存储起来,以便在价格输入部分中使用。有谁可以帮助我吗?我是Java的新手,通过自己阅读互联网阅读书籍的方式学习,但在这种情况下找不到任何可以帮助我的东西。对此有任何帮助将不胜感激。


import java.io.*;
import java.text.*;
import java.util.*;
import java.util.Scanner;
import java.math.BigDecimal;
import java.text.NumberFormat;

class priceCalc {

public static void main (String[] args) throws IOException {
  BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
  Scanner input = new Scanner(System.in);
  int Quantity;
  int result = 0;
  int foodCount = 1;
  double Price, Subtotal, SalesTax, TotalSale;
  NumberFormat curForm = NumberFormat.getCurrencyInstance();
// Here is a simple display of available products
  System.out.println (" ");
  System.out.println ("Welcome to the IP2 part 2!");
  System.out.println (" ");

  System.out.println ("Please make a selection:");
  System.out.println ("1. Octopus Tentacles, 2. Rat Droppings");
  System.out.println ("3. Sharks teeth, 4. Mountain Oysters");
  System.out.println (" ");
// Since we don't have a primary database to pull from we use a dummy DB
  while ( foodCount <= 1 )
  {
  System.out.print( "Enter selection (1 , 2, 3, 4): ");
  result = input.nextInt();

  if ( result == 1)
    System.out.println( "You selected Octopus tentacles, price $4.99" );
  else if ( result == 2)
    System.out.println( "You selected Rat droppings, price $3.49" );
  else if ( result == 3)
    System.out.println( "You selected Sharks teeth, price $6.73" );
  else if ( result == 4)
    System.out.println( "You selected Mountain Oysters, price $9.95" );
  break;
  }

  System.out.println (" ");
    // Create Scanner for input
  Scanner key = new Scanner(System.in);

    //Quantity
  System.out.print ("Please enter quantity: ");
  Quantity = key.nextInt();
  System.out.println (" ");

    //Price    
  System.out.print ("Please input price (price amount w/no $): ");
  Price = key.nextDouble();
  System.out.println (" ");

    //Subtotal
  Subtotal = Price * Quantity;
  System.out.println ("Your subtotal is: " +curForm.format(Subtotal));

    //Sales Tax
  SalesTax = Subtotal * .065;
  System.out.println ("Your sales tax is: " +curForm.format(SalesTax));

    //Total Sale
  TotalSale = Subtotal+SalesTax;
  System.out.println ("Your total sale price is: " +curForm.format(TotalSale));
  System.out.println (" ");
}
}

2 个答案:

答案 0 :(得分:1)

我做了相关的修改并给出了理由。


class priceCalc {
    public static void main (String[] args) throws IOException {
        /*
        You don't need a BufferedReader and also a Scanner, any one will do.
        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
        */

        Scanner input = new Scanner(System.in);

        /* 
        Changed variable names to camelCase
        http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java
        */
        int quantity;
        int result = 0;
        double price[] = new double[4];
        double subTotal, salesTax, totalSale;
        NumberFormat curForm = NumberFormat.getCurrencyInstance();
        // Here is a simple display of available products
        System.out.println (" ");
        System.out.println ("Welcome to the IP2 part 2!");
        System.out.println (" ");
        System.out.println("Enter prices for each product");
        System.out.println("Octopus tentacles: ");
        price[0] = input.nextDouble();
        System.out.println("Rat Droppings: ");
        price[1] = input.nextDouble();
        System.out.println("Sharks teeth: ");
        price[2] = input.nextDouble();
        System.out.println("Mountain Oysters: ");
        price[3] = input.nextDouble();

        System.out.println ("Please make a selection:");
        System.out.println ("1. Octopus Tentacles, 2. Rat Droppings");
        System.out.println ("3. Sharks teeth, 4. Mountain Oysters");
        System.out.println (" ");
        // Changed while into an infinite loop that will break on any input other than 1, 2, 3, and 4.
        while (true)
        {
            System.out.print( "Enter selection (1 , 2, 3, 4): ");
            result = input.nextInt();
            if ( result == 1) 
            {
                System.out.println( "You selected Octopus tentacles, "+price[0]);
                subTotal = price[0]*getQuantity();
            }
            else if ( result == 2)
            {
                System.out.println( "You selected Rat droppings, "+price[1] );
                subTotal = price[1]*getQuantity();
            }
            else if ( result == 3)
            {
                System.out.println( "You selected Sharks teeth, "+price[2] );
                subTotal = price[2]*getQuantity();
            }
            else if ( result == 4)
            {
                System.out.println( "You selected Mountain Oysters, "+price[3] );
                subTotal = price[3]*getQuantity();
            }
            else
            {
                break;
            }
            System.out.println ("Your subTotal is: " +curForm.format(subTotal));
            salesTax = subTotal * .065;
            System.out.println ("Your sales tax is: " +curForm.format(salesTax));
            totalSale = subTotal+salesTax;
            System.out.println ("Your total sale price is: " +curForm.format(totalSale));
            System.out.println (" ");
        }

        System.out.println (" ");
        /*
        You don't need a second Scanner within the same scope
        Scanner key = new Scanner(System.in);
        */
    }
    public static int getQuantity()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter quantity: ");
        int q = input.nextInt();
        return q;
    }
}

答案 1 :(得分:0)

通常情况下,我认为初学者错误是result的不正确范围 - 您可以在其中存储内容,但无法从父范围访问它。但是,此处result的范围是适当的。

你的例子看起来不完整,因为你试图循环(在foodCount <= 1上) - 但你总是在打破,所以你永远不会实际循环。因此,您应该只有一个选择 - result的值可以在以后的代码中使用,以便在您的计算中使用 - 就在PriceSubtotal,{{1}旁边}和SalesTax。 (遵循Java约定,这些变量名称都应以小写字母开头。)但是,在用户已选择具有相关价格的项目之后,我很困惑为什么要提示输入价格 - 除非这是你的我正试图纠正。

约翰的建议/答案+1。在此之后,您可以删除价格提示。但是,您需要首先在上面声明TotalSale以及其他变量才能生效。