0

好的,首先我对java非常非常陌生。对于这个项目,我需要设计一个程序,它需要一个产品编号、一个销量,计算总数,然后显示它。但是,当我选择选项 2 时,我需要显示,这是一个单独的私人课程,老实说,我什至不知道从哪里开始。任何帮助,将不胜感激。

import java.util.Scanner;

public class Attempt1
{
//method to pause until a key is pressed
public static void pause() 
{ 
    try 
    { 
        System.out.print("Press <Enter> to continue..."); 
        System.in.read(); 
    } 
    catch (Exception e)
    {
        System.err.printf("Error %s%c\n",e.getMessage(),7);
    }
}//end pause

public static void main(String[] args)
{
    //variables to capture keyboard input
    Scanner keyBd = new Scanner( System.in );
    char selection;
    //int selection;

    do{//display menu
        System.out.println("\n--------------");
        System.out.println("Retail Sales\n");
        System.out.println("1. Enter Products Sold");
        System.out.println("2. Display Total Retail Sales");
        System.out.println("3. Exit\n");
        System.out.print  ("Selection: ");

        //get menu selection
        selection = keyBd.next().charAt(0);
        //selection = keyBd.nextInt();

        //process menu selection
        switch (selection){
            case '1':
                enterProducts();
                break;
            case '2':
                displaySales();
                break;
            case '3':               
                //recognize as valid selection but do nothing
                break;
            default :
                //System.out.printf("%c\n",7);
                System.out.println("Invalid Selection");
        }//end switch

    }while( selection != '3');

}//end main()

private static void enterProducts()
{
    Scanner inp = new Scanner(System.in);

    int product,quantity;
    double total = 0.00;

    System.out.print("Enter product #(1-5)(0 to stop): ");
    product=inp.nextInt();

    while(product !=0)
        {
        System.out.print("Enter quantity: ");
        quantity=inp.nextInt();
    switch( product ){
    case 1:total+=quantity*2.98;
    break;
    case 2:total+=quantity*4.50;
    break;
    case 3:total+=quantity*3.98;
    break;
    case 4:total+=quantity*4.49;
    break;
    case 5:total+=quantity*6.87;
    break;
    default:System.out.println("Invalid Product Number");
    System.out.println("Product Number Does not Exist");

    if(product<0 && product>=6)
      {
    System.out.print("Enter product #(1-5)(0 to stop): ");
    product=inp.nextInt();
    System.out.print("Enter quantity: ");
    quantity=inp.nextInt();
      }
    break;
    }
    System.out.print("Enter product #(1-5)(0 to stop): ");
    product=inp.nextInt(); 
    }

    pause();    
    }
private static void displaySales()
{
    System.out.println( "The total retail value was: " + total );
    pause();    
}

}//结束菜单演示

4

3 回答 3

1

我认为你的意思是private 方法。你可以像这样传递总数:

private static void displaySales(double total) {
...

total在显示循环的方法中定义enterProducts但不在main显示循环的方法中,因此您可以返回:

double enterProducts() {
   ...
   return total;
}

这样您就可以将其传递给displaySales.

于 2012-10-10T19:42:49.927 回答
1

这是改进代码的算法:

  1. 在您的 main 开头添加一个变量total并将其初始化为 0:double total=0;
  2. enterProducts将方法的返回类型更改为 double: 并在调用后从该方法private static double enterProducts()返回最后的局部变量:totalpausereturn total;
  3. 在输入的情况下,1将返回的值从添加enterProducts到当前值total(它total在你的 main 内部):total += enterProducts();
  4. 在方法中添加displaySales一个双参数:并在 main 的情况下更改private static void displaySales(double total)对它的调用2displaySales(total);
于 2012-10-10T19:52:39.303 回答
0

代码的问题在于您试图访问在静态 displaySales() 方法内部的静态 enterProducts() 方法中声明的局部变量。

下面的代码解决了这个“问题”。

话虽如此,我建议您阅读一些 Java 教程以了解代码现在可以工作的原因……看看Variable Scope

public class Attempt1
{
//use a static variable to store the total  
static double total = 0.00;

//method to pause until a key is pressed
public static void pause() 
{ 
    try 
    { 
        System.out.print("Press <Enter> to continue..."); 
        System.in.read(); 
    } 
    catch (Exception e)
    {
        System.err.printf("Error %s%c\n",e.getMessage(),7);
    }
}//end pause

public static void main(String[] args)
{
    //variables to capture keyboard input
    Scanner keyBd = new Scanner( System.in );
    char selection;
    //int selection;

    do{//display menu
        System.out.println("\n--------------");
        System.out.println("Retail Sales\n");
        System.out.println("1. Enter Products Sold");
        System.out.println("2. Display Total Retail Sales");
        System.out.println("3. Exit\n");
        System.out.print  ("Selection: ");

        //get menu selection
        selection = keyBd.next().charAt(0);
        //selection = keyBd.nextInt();

        //process menu selection
        switch (selection){
            case '1':
                enterProducts();
                break;
            case '2':
                displaySales();
                break;
            case '3':               
                //recognize as valid selection but do nothing
                break;
            default :
                //System.out.printf("%c\n",7);
                System.out.println("Invalid Selection");
        }//end switch

    }while( selection != '3');

}//end main()

private static void enterProducts()
{
    Scanner inp = new Scanner(System.in);

    int product,quantity;

    System.out.print("Enter product #(1-5)(0 to stop): ");
    product=inp.nextInt();

    while(product !=0)
        {
        System.out.print("Enter quantity: ");
        quantity=inp.nextInt();
    switch( product ){
    case 1:total+=quantity*2.98;
    break;
    case 2:total+=quantity*4.50;
    break;
    case 3:total+=quantity*3.98;
    break;
    case 4:total+=quantity*4.49;
    break;
    case 5:total+=quantity*6.87;
    break;
    default:System.out.println("Invalid Product Number");
    System.out.println("Product Number Does not Exist");

    if(product<0 && product>=6)
      {
    System.out.print("Enter product #(1-5)(0 to stop): ");
    product=inp.nextInt();
    System.out.print("Enter quantity: ");
    quantity=inp.nextInt();
      }
    break;
    }
    System.out.print("Enter product #(1-5)(0 to stop): ");
    product=inp.nextInt(); 
    }

    pause();    
    }
private static void displaySales()
{
    System.out.println( "The total retail value was: " + total );
    pause();    
}
}//end MenuDemo
于 2012-10-10T20:02:31.643 回答