2012-10-10 26 views
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();  
} 

} //結束MenuDemo

+0

我在這裏沒有看到任何私人課程。你能解釋一下你的代碼有什麼問題嗎? –

+0

基本上,我所需要的只是當我運行程序時,我使用第二個選項關閉「菜單」,因爲它從第一個顯示總數。如果這有幫助的話。 – user1736004

+0

記得在前面用4個空格縮進所有代碼;它看起來像你的最後一行沒有適當縮進。 (「} //結束MenuDemo」) –

回答

1

這是提高你的代碼的算法:

  1. 在你的你的主要的開頭添加一個變量total並將其初始化爲0:double total=0;
  2. 更改enterProducts方法的返回類型爲double:private static double enterProducts()並且呼叫之後在端部由該方法返回本地變量totalpausereturn total;
  3. 在這種情況下爲1輸入添加回ED值從enterProductstotal電流值(它的total裏面你的主):total += enterProducts();
  4. 添加到方法displaySales的雙參數:private static void displaySales(double total)和改變調用它的主要的情況下2displaySales(total);
1

我想你的意思private方法。你可以在總傳球像這樣:

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

totalenterProducts定義,但不是在main方法,其中顯示的循環,所以你可以返回此:

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

,這樣就可以通過它到displaySales

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