有什么方法可以为 java 中当前声明的变量分离两个不同对象的输入吗?

is there any way to seperate input for two different objects for a currently declared variable in java?

所以我想做的是从用户那里获取购买两种不同产品的意见。我需要将这些数据输入到 table 中,我相信我创建的是正确的。然而,scanner 方法只是保留信息,而不是将它们输出到 table。我的问题真的是你能为先前声明的变量创建子变量,例如变量 productName 的 p1 和 p2 或数量的 qty1 和 qty2。我知道为什么它不将数据输出到 table,因为我错误地标记了占位符,例如 %f。我还没有找到任何这样说的信息,从我发现的所有内容来看,密切相关的是为所有需要的输入创建一个数组。我不是最好的,但我会很感激任何帮助。谢谢

import java.util.Scanner;
import java.text.NumberFormat;

public class Project2
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int quantity;
        double unitPrice;
        String productName;

        System.out.println("Enter name for Product 1:");
        productName = scan.nextLine();

        System.out.println("Enter the amount desired for Product 1:");
        quantity = scan.nextInt();

        System.out.println("Please enter the price of Product 1:");
        unitPrice = scan.nextDouble();

        double total = unitPrice * quantity;

        System.out.println("Enter name for Product 2:");
        productName = scan.nextLine();

        System.out.println("Enter the amount desired for Product 2:");
        quantity = scan.nextInt();

        System.out.println("Please enter the price of Product 1:");
        unitPrice = scan.nextDouble();

        System.out.println("--------------------------------------------");
        System.out.println("|   Product |    Qty |   Price |   Total   |");
        System.out.println("--------------------------------------------");
        System.out.printf("| %c        |  %f    |   %d    |   %.2f    |\n",productName, quantity,unitPrice, total);
        System.out.printf("| %c        |  %f    |   %d    |   %.2f    |\n",productName, quantity, unitPrice, total);
        System.out.println("--------------------------------------------    ");
    }
}

您应该为每个 属性 创建两个变量。如果您为每个 属性 使用一个变量,您最终会丢失产品一的值。