Java 如果可能,在变量中存储多个值

Java 如果可能,在变量中存储多个值,java,while-loop,do-while,Java,While Loop,Do While,不知道是否有人可以帮助我,我正在尝试创建一个程序,只要do/while循环继续循环,就可以将为每本书输入的所有价格的总额相加,但到目前为止,我所拥有的只是每次循环重新启动时,它都会删除先前指定的变量值!有人能把我引向正确的方向吗?我刚开始学习java,所以我的知识库很低 String surname, firstname, email, cfn, btitle, finished; double pno, nochild, cage, noboks, costbook=0, totalcost,

不知道是否有人可以帮助我,我正在尝试创建一个程序,只要do/while循环继续循环,就可以将为每本书输入的所有价格的总额相加,但到目前为止,我所拥有的只是每次循环重新启动时,它都会删除先前指定的变量值!有人能把我引向正确的方向吗?我刚开始学习java,所以我的知识库很低

String surname, firstname, email, cfn, btitle, finished;
double pno, nochild, cage, noboks, costbook=0, totalcost, averagecost, price;




            k = new Scanner(System.in);
             System.out.print("Welcome - What is your Family surname?");
              surname = k.next();
             System.out.print("What is your own first name?");
             firstname=k.next();
             System.out.print("What is your email address?");
              email=k.next();
              System.out.print("What is your phone number?");
             pno=k.nextInt();
             System.out.print("How many children do you have?");
             nochild=k.nextInt();
             System.out.print("You are Alan " + surname);
              System.out.println();





             int childcounter=1;
            do{
              System.out.print("What is your childs first name "+childcounter+" of " +nochild+"?");
              cfn=k.next();
              System.out.print("What is "+cfn+"'s age?");
              cage=k.nextInt();
              System.out.println();
              do {
              System.out.print("What is the title of the book "+cfn+" would like?");
              btitle=k.next();      
              System.out.print("Price of '"+btitle+"' ?");
             price=k.nextDouble();
              System.out.println();


              System.out.print("Do you want to finish? y/n ");
              finished=k.next();





              }
             while (finished.equalsIgnoreCase("n"));


            if (finished.equalsIgnoreCase("y"))
            {

            }
              childcounter++;
            }
        while (childcounter <= nochild);
            System.out.print("Heres the book price €"+price);
            }
字符串姓氏、名字、电子邮件、cfn、btitle、已完成;
double pno,nochild,cage,noboks,costbook=0,totalcost,averagecost,price;
k=新扫描仪(系统英寸);
System.out.print(“欢迎您-您的姓氏是什么?”);
姓氏=k.next();
System.out.print(“您自己的名字是什么?”);
firstname=k.next();
System.out.print(“您的电子邮件地址是什么?”);
email=k.next();
系统输出打印(“你的电话号码是多少?”);
pno=k.nextInt();
System.out.print(“你有多少孩子?”);
nochild=k.nextInt();
System.out.print(“你是艾伦”+姓氏);
System.out.println();
int childcounter=1;
做{
System.out.print(“您孩子的名字是什么”+childcounter+“+nochild+”?”);
cfn=k.next();
系统输出打印(“什么是“+cfn+”的年龄?”);
cage=k.nextInt();
System.out.println();
做{
System.out.print(“书的标题“+cfn+”想要什么?”);
btitle=k.next();
系统输出打印(“价格为“'+btitle+”?”);
价格=k.nextDouble();
System.out.println();
系统输出打印(“是否要完成?是/否”);
完成=k.下一步();
}
while(finished.equalsIgnoreCase(“n”));
如果(已完成。等效信号情况(“y”))
{
}
childcounter++;
}
而(childcounter这样使用

String[][] data = new String[numberOfLoops][numberOfVariable];
所以你可以用

data[nthIteration][variable] = scanner.next();

要使变量在循环中保持不变,您需要在循环开始之前定义它们。如果变量是在循环中定义的,则在循环完成后将删除该变量

    int test;
    for (int i=0; i < 10; i++)
    {
        test = test * i;
    }
    //test's value will persist 
int检验;
对于(int i=0;i<10;i++)
{
测试=测试*i;
}
//测试的值将保持不变
教程:


如果要将多个值存储到一个变量中,应该查看数组


教程:

您可能需要定义一个包含这些变量/字段的类:

public class MyInfo {
  String surname, firstname, email, cfn, btitle, finished;
  double pno, nochild, cage, noboks, costbook=0, totalcost, averagecost, price;
}

然后在while循环的每次迭代中创建该类的新实例,并将每个新对象存储在列表/ArrayList中

您需要使用数组。您确实需要正确格式化代码(使用IDE,它可以为您格式化代码…)。特别是如果你是一个初学者,你不应该试图通过混乱的代码来让事情变得更难…特别是当格式良好的代码只需点击几下鼠标或按键。我想做的是有一个变量,我可以不断添加,直到我想打印出合并的总数,这是可能的吗?我现在明白了。你的问题是事实上,你把
childcounter++;
放在do-while循环之外。把它放在里面,你的int将正确地递增。编辑:我错了,只是因为没有正确的格式,所以很难读取你的代码。