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

コードを改善するためのアルゴリズムは次のとおりです。

  1. メインの先頭に変数を追加しtotal、0 で初期化します。double total=0;
  2. enterProductsメソッドの戻り値の型を double: に変更し、への呼び出しの後、このメソッドから最後private static double enterProducts()にローカル変数を返します。totalpausereturn total;
  3. 1からの戻り値をenterProducts現在の値に追加する入力の場合totaltotalメインの内部にあります):total += enterProducts();
  4. displaySalesメソッドに double の引数を追加しますprivate static void displaySales(double total)2displaySales(total);
于 2012-10-10T19:52:39.303 に答える
1

private メソッドのことだと思います。次のように合計を渡すことができます。

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

totalで定義されていますが、ループが表示されるメソッドでは定義されてenterProductsいないmainため、これを返すことができます。

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

に渡すことができるようにしますdisplaySales

于 2012-10-10T19:42:49.927 に答える
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 に答える