Java 如何将案例1的结果用于另一个案例
我有输入货物数量的程序,还有switch-case语句 案例1:输入新商品数据Java 如何将案例1的结果用于另一个案例,java,Java,我有输入货物数量的程序,还有switch-case语句 案例1:输入新商品数据 案例2:打印所有数据的报告 在情况1中,程序输入新数据。代码是这样的 import java.util.Scanner; public class inputData { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int add = 0;
案例2:打印所有数据的报告 在情况1中,程序输入新数据。代码是这样的
import java.util.Scanner;
public class inputData {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int add = 0;
System.out.println("1. Create a new goods data\n2. Print the data");
System.out.print("Type the option by typing sc: ");
int menu = sc.nextInt();
switch (menu) {
case 1:
String finalResult[][] = inputAgainConfirmation(createFirstRow(add), add);
break;
case 2: //the code is not written yet
break;
}
}
static String[] information() {
String info[] = {"Code", "Name", "Purchase Price", "Selling Price", "Incoming Goods", "Outgoing Goods", "Damaged Goods", "Total Goods"};
return info;
}
static String[][] createFirstRow(int add) {
String info[] = information();
String create[][] = new String[1][8];
sc.nextLine();
for (String[] create1 : create) {
for (int j = 0; j < create[0].length; j++) {
System.out.print("Input the " + info[j] + ": ");
String input = sc.nextLine();
create1[j] = input;
}
}
return create;
}
static String[][] InputNewDataAgain(String[][] result, int add) {
sc.nextLine();
String info[] = information();
String backup[][] = result;
result = new String[result.length + 1][result[0].length];
System.arraycopy(backup, 0, result, 0, backup.length);
for (int i = backup.length; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print("Input the " + info[j] + ": ");
String input = sc.nextLine();
result[i][j] = input;
}
}
return result;
}
static String[][] inputAgainConfirmation(String[][] result, int add) {
System.out.print("Create again? 1 = yes, 0 = no: ");
int in = sc.nextInt();
if (in == 1) {
String createAgain[][] = InputNewDataAgain(result, ++add);
return inputAgainConfirmation(createAgain, add);
} else {
return result;
}
}
}
import java.util.Scanner;
公共类输入数据{
静态扫描仪sc=新扫描仪(System.in);
公共静态void main(字符串[]args){
int add=0;
System.out.println(“1.创建新商品数据\n2.打印数据”);
System.out.print(“通过键入sc:”,键入选项);
int menu=sc.nextInt();
开关(菜单){
案例1:
字符串finalResult[][]=输入确认(createFirstRow(add),add);
打破
案例2://代码尚未编写
打破
}
}
静态字符串[]信息(){
字符串信息[]={“代码”、“名称”、“采购价格”、“销售价格”、“进货”、“出库商品”、“损坏商品”、“总商品”};
退货信息;
}
静态字符串[][]createFirstRow(int-add){
字符串信息[]=信息();
字符串创建[][]=新字符串[1][8];
sc.nextLine();
对于(字符串[]create1:create){
对于(int j=0;j
我想在案例2中打印finalResult[]的值
如何保存case1语句的finalResult[]],这样当我想调用结果时,程序将不再填充。在代码中finalResult是局部变量,即此变量的范围在开关盒块(case1)内要在案例2中打印此变量结果,请将该变量设置为全局变量,请参阅以下代码:
public static void main(String[] args) {
int add = 0;
String finalResult[][] = new String[10][10]; // declare finalResult variable
System.out.println("1. Create a new goods data\n2. Print the data");
System.out.print("Type the option by typing sc: ");
int menu = sc.nextInt();
switch (menu) {
case 1:
finalResult[][] = inputAgainConfirmation(createFirstRow(add), add); // Assign value
break;
case 2: //the code is not written yet
// Print finalResult value
break;
}
}
几乎完全是从了解范围开始的自我删除问题的复述。在您的示例中,该局部变量
finalResult
仅存在于该开关盒块中。它在程序的其他任何地方都不可见。因此,第一步:将变量声明上移,使变量在以下所有代码中都可见。但是,当然:您的案例2代码必须检查变量是否已被赋值。或者,您可以阅读类中的字段,并使用它来代替局部变量来保存结果。