好的,首先我对java非常非常新。对于这个项目,我需要设计一个程序,它接受一个产品编号,一个销售的amoutn,计算总数,然后显示它。但是,当我选择选项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 :(得分:1)
我认为您的意思是private
方法。你可以像这样传递总数:
private static void displaySales(double total) {
...
total
在enterProducts
中定义,但未在显示循环的main
方法中定义,因此您可以返回此内容:
double enterProducts() {
...
return total;
}
以便您可以将其传递给displaySales
。
答案 1 :(得分:1)
以下是改进代码的算法:
total
并将其初始化为0:double total=0;
enterProducts
方法的返回类型更改为double private static double enterProducts()
,并在调用total
之后从此方法返回本地变量pause
:{{1 }} return total;
,则将1
的返回值添加到当前值enterProducts
(它是您主要内部的total
):{{ 1}} total
添加双重参数:total += enterProducts();
并在displaySales
到private static void displaySales(double total)
答案 2 :(得分:0)
代码的问题在于您正在尝试访问静态enterSroducts()方法中静态displaySales()方法中声明的局部变量。
以下代码解决了“问题”。
话虽如此,我建议您通过一些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