如何使用java计算器继续加法

如何使用java计算器继续加法,java,Java,我想从我以前的计算中添加一个数字。由于循环仍在继续,我目前无法完成。这是我的密码。例如,如果我的答案是11,我希望能够加上6。我也希望能够加上6+6,得到答案23。我怎么做,因为我现在被卡住了。 谢谢你的帮助 public static void main(String [] args){ scan = new Scanner(System.in); while(!done){ int numOne, numTwo, result; Syst

我想从我以前的计算中添加一个数字。由于循环仍在继续,我目前无法完成。这是我的密码。例如,如果我的答案是11,我希望能够加上6。我也希望能够加上6+6,得到答案23。我怎么做,因为我现在被卡住了。 谢谢你的帮助

public static void main(String [] args){

    scan = new Scanner(System.in);

    while(!done){
       int numOne, numTwo, result;



       System.out.print("Enter Two Numbers : ");
       numOne = scan.nextInt();
       scan.nextLine();
       numTwo = scan.nextInt();
       scan.nextLine();


       result = numOne + numTwo;
       System.out.println("Addition = " +result);

       result = numOne - numTwo;
       System.out.println("Subtraction = " +result);

       result = numOne * numTwo;
       System.out.println("Multiplication = " +result);

       result = numOne / numTwo;
       System.out.println("Division = " +result);



       System.out.println("Enter a number for a root : ");
       double number1 = scan.nextDouble();
       scan.nextLine();
       double number2 = scan.nextDouble();
       scan.nextLine();

       System.out.println(Math.pow(number1, (1/number2)));



       System.out.println("Enter the base: ");

       long n,p,r=1;


       System.out.println("enter number");

       n=scan.nextLong();
       scan.nextLine();

       System.out.println("enter power");


       p=scan.nextLong();
       scan.nextLine();

           if(n>=0&&p==0)
       {   
          r =1;
       }
       else if(n==0&&p>=1)
       {   
            r=0;
       } 
       else
       { 
           for(int i=1;i<=p;i++)
           { 
                   r=r *n;
           } 
       }    

       System.out.println(n+"^"+p+"="+r);


       System.out.println("Do you wish to continue?");
       String ans2 = scan.nextLine();
       System.out.println(ans2);

       if(ans2.contains("Yes")|| ans2.contains("yes")){
                  System.out.println("Do you wish to add on to the previous calculation?");
       }
       if(ans2.contains("No") || ans2.contains("no")){
                  System.out.println("You are done calculating!");
       }
       scan.nextLine();

   }

}
publicstaticvoidmain(字符串[]args){
扫描=新扫描仪(System.in);
而(!完成){
int numOne,numTwo,result;
System.out.print(“输入两个数字:”);
numOne=scan.nextInt();
scan.nextLine();
numTwo=scan.nextInt();
scan.nextLine();
结果=numOne+numTwo;
System.out.println(“加法=”+结果);
结果=numOne-numTwo;
System.out.println(“减法=”+结果);
结果=numOne*numTwo;
System.out.println(“乘法=”+结果);
结果=numOne/numTwo;
System.out.println(“除法=”+结果);
System.out.println(“为根输入一个数字:”);
double number1=scan.nextDouble();
scan.nextLine();
double number2=scan.nextDouble();
scan.nextLine();
系统输出打印LN(数学功率(数字1,(1/数字2));
System.out.println(“输入基:”);
长n,p,r=1;
System.out.println(“输入编号”);
n=scan.nextLong();
scan.nextLine();
System.out.println(“输入电源”);
p=scan.nextLong();
scan.nextLine();
如果(n>=0&&p==0)
{   
r=1;
}
如果(n==0&&p>=1),则为else
{   
r=0;
} 
其他的
{ 

对于(int i=1;i将答案存储在不同的变量中,而不是将所有内容存储在结果中。 假设“add”是用于加法的变量,然后使用表达式
add=add+numone+numtwo;
在循环之前初始化add变量。
您还没有将done变量设置为零。if中的set done=0检查选项是否为“否”。

将答案存储在不同的变量中,而不是将所有内容都存储在结果中。 假设“add”是用于加法的变量,然后使用表达式
add=add+numone+numtwo;
在循环之前初始化add变量。
您没有将done变量设置为零。在if中设置done=0,它检查选项是否为“否”。

您可以在while循环顶部初始化结果变量,这样它的值就不会在每次重复循环时重置。

您可以在while循环顶部初始化结果变量,以便它的值不会在每次循环重复时重置。

首先,循环会继续,因为没有退出条件。当您编写while循环时,您需要确保在达到目标后条件变为false。 要保持结果的持久性,请在while循环外部声明它

int result = 0;
    while (!done) {
并将结果添加到自身中

    result += numOne + numTwo;
通过将“完成”设置为“真”,在用户完成计算时使while条件退出。并添加另一个条件以在用户不再需要时重置结果

String ans3 = "";
if (ans2.contains("Yes") || ans2.contains("yes")) {
    System.out.println("Do you wish to add on to the previous calculation?");
    ans3 = scan.nextLine();
}
if (ans2.contains("No") || ans2.contains("no")) {
    System.out.println("You are done calculating!");
    done = true;
}
if (!ans3.toLowerCase().contains("y")) {
    result = 0;
}

希望这有帮助首先,循环继续进行,因为没有退出条件。当您编写while循环时,您需要确保在实现目标后条件变为false。 要保持结果的持久性,请在while循环外部声明它

int result = 0;
    while (!done) {
并将结果添加到自身中

    result += numOne + numTwo;
通过将“完成”设置为“真”,在用户完成计算时使while条件退出。并添加另一个条件以在用户不再需要时重置结果

String ans3 = "";
if (ans2.contains("Yes") || ans2.contains("yes")) {
    System.out.println("Do you wish to add on to the previous calculation?");
    ans3 = scan.nextLine();
}
if (ans2.contains("No") || ans2.contains("no")) {
    System.out.println("You are done calculating!");
    done = true;
}
if (!ans3.toLowerCase().contains("y")) {
    result = 0;
}
希望这有帮助