Java 需要辅助显示来自不同私有类的值
好的,首先我对java非常非常陌生。对于这个项目,我需要设计一个程序,需要一个产品编号,一个销售量,计算总数,然后显示它。然而,当我选择选项2时,我需要显示,这是一个单独的私有类,老实说,我甚至不知道从哪里开始。任何帮助都将不胜感激Java 需要辅助显示来自不同私有类的值,java,Java,好的,首先我对java非常非常陌生。对于这个项目,我需要设计一个程序,需要一个产品编号,一个销售量,计算总数,然后显示它。然而,当我选择选项2时,我需要显示,这是一个单独的私有类,老实说,我甚至不知道从哪里开始。任何帮助都将不胜感激 import java.util.Scanner; public class Attempt1 { //method to pause until a key is pressed public static void pause() { try
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();
}
import java.util.Scanner;
公共类尝试1
{
//方法暂停,直到按下某个键
公共静态无效暂停()
{
尝试
{
系统输出打印(“按继续…”);
System.in.read();
}
捕获(例外e)
{
System.err.printf(“错误%s%c\n”,e.getMessage(),7);
}
}//结束暂停
公共静态void main(字符串[]args)
{
//用于捕获键盘输入的变量
Scanner keyBd=新扫描仪(System.in);
字符选择;
//int选择;
do{//显示菜单
System.out.println(“\n----------------”;
System.out.println(“零售额”);
System.out.println(“1.输入售出的产品”);
System.out.println(“2.显示零售总额”);
System.out.println(“3.Exit\n”);
System.out.print(“选择:”);
//获取菜单选择
selection=keyBd.next().charAt(0);
//selection=keyBd.nextInt();
//过程菜单选择
开关(选择){
案例“1”:
肠产品();
打破
案例“2”:
显示销售();
打破
案例“3”:
//识别为有效选择,但不执行任何操作
打破
违约:
//System.out.printf(“%c\n”,7);
System.out.println(“无效选择”);
}//终端开关
}while(选择!=“3”);
}//尾干管()
私有静态产品()
{
扫描仪inp=新扫描仪(System.in);
int产品,数量;
双倍合计=0.00;
系统输出打印(“输入产品(1-5)(0停止):”;
product=inp.nextInt();
while(产品!=0)
{
系统输出打印(“输入数量:”);
数量=inp.nextInt();
交换机(产品){
案例1:总量+=数量*2.98;
打破
案例2:总量+=数量*4.50;
打破
案例3:总量+=数量*3.98;
打破
案例4:总量+=数量*4.49;
打破
案例5:总量+=数量*6.87;
打破
默认值:System.out.println(“无效产品编号”);
System.out.println(“产品编号不存在”);
如果(产品=6)
{
系统输出打印(“输入产品(1-5)(0停止):”;
product=inp.nextInt();
系统输出打印(“输入数量:”);
数量=inp.nextInt();
}
打破
}
系统输出打印(“输入产品(1-5)(0停止):”;
product=inp.nextInt();
}
暂停();
}
私有静态void displaySales()
{
System.out.println(“零售总额为:“+总额”);
暂停();
}
}//结束菜单我想你指的是
private
方法。你可以这样传递总数:
private static void displaySales(double total) {
...
total
在enterProducts
中定义,但不在显示循环的main
方法中定义,因此您可以返回以下内容:
double enterProducts() {
...
return total;
}
因此,您可以将其传递给
displaySales
以下是一个改进代码的算法:
total
,并用0初始化它:double total=0代码>
enterProducts
方法的返回类型更改为double:私有静态双enterProducts()
并在调用pause
:return total后从此方法的末尾返回局部变量total
代码>
1
的情况下,将enterProducts
返回的值添加到total
的当前值中(它是主目录中的total
):total+=enterProducts()代码>
displaySales
中添加一个双参数:private static void displaySales(double total)
并在main的案例中将2
的调用更改为displaySales(total)代码>
代码的问题在于,您试图访问一个局部变量,该变量在静态enterProducts()方法中声明,位于静态displaySales()方法内部 下面的代码解决了这个“问题” 话虽如此,我建议您阅读一些Java教程,以了解代码为什么现在可以工作。。。看一看
公共类尝试1
{
//使用静态变量存储总计
静态双总=0.00;
//方法暂停,直到按下某个键
公共静态无效暂停()
{
尝试
{
系统输出打印(“按继续…”);
System.in.read();
}
捕获(例外e)
{
System.err.printf(“错误%s%c\n”,e.getMessage(),7);
}
}//结束暂停
公共静态void main(字符串[]args)
{
//用于捕获键盘输入的变量
Scanner keyBd=新扫描仪(System.in);
字符选择;
//int选择;
do{//显示菜单
System.out.println(“\n----------------”;
System.out.println(“零售额”);
System.out.println(“1.输入售出的产品”);
System.out.println(“2.显示零售总额”);
System.out.println(“3.Exit\n”);
System.out.print(“选择:”);
//获取菜单选择
selection=keyBd.next().charAt(0);
//selection=keyBd.nextInt();
//过程菜单选择
开关(选择){
案例“1”:
肠产品();
打破
案例“2”:
显示销售();
打破
案例“3”:
//识别为有效选择,但不执行任何操作
打破
违约:
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