我想把两个变量的输入作为整数,而不是两个变量的加法。 ans将存储在另一个变量中。程序将在每次添加结束后重复,并要求用户输入varibles并再次添加。
我的问题是如何再次添加所有请求:
EXM:
Input a= 5
Input b=5
ans=10
Agin计划将要求
Input a= 6
Input b= 6
ans=12
现在我怎样才能将所有“ans”值与程序一起使用并添加所有“ans”
Final Ans=10+12=22
代码:
import java.io.*;
import java.util.Scanner;
public class math{
public void add()
{
Scanner keyboard = new Scanner(System.in);
int a;
int b;
System.out.print("\nEnter a : ");
a = keyboard.nextInt();
System.out.print("Enter b : ");
b = keyboard.nextInt();
int c=a+b;
System.out.println("\nans is :"+c);
math ob_m=new math();
ob_m.add();
}
public static void main(String args[])
{
math ob_main=new math();
ob_main.add();
}
}
代码只是一个接一个地添加,但我希望它会再做一个......
这一切都添加了所有的adals reasuts。我怎么能这样做?
答案 0 :(得分:1)
import java.io.*;
import java.util.Scanner;
public class Test {
int a;
int b;
int sum = 0;
public void add() {
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter a : ");
a = keyboard.nextInt();
System.out.print("Enter b : ");
b = keyboard.nextInt();
sum = sum + (a + b);
System.out.println("\nans is :" + (a + b));
}
public static void main(String args[]) {
Test ob_main = new Test();
while (true) {
ob_main.add();
}
}
}
答案 1 :(得分:0)
只需将中间总和添加到另一个变量,最后得到最终总数。
另一个替代方案(虽然在提供简单解决方案时不是优选的)是将这些总和放在一个列表中,最后遍历列表并计算最终总数。
另外,不要使用ob_m.add(); - 只需调用add();
答案 2 :(得分:0)
将每个答案存储在一个附加变量中。然后,当你完成后,总结所有的答案变量
答案 3 :(得分:0)
您可以使用循环重复您的操作,并将每个添加内容存储在一个总和中吗?
答案 4 :(得分:0)
有一个字段GrandSUM,
GrandSum = 0;
并在每次添加后添加ans。
GrandSum += ans;
最后,GrandSum会得到你想要的结果。
编辑:
import java.io.*;
import java.util.Scanner;
public class math{
int GrandSum = 0;//added
public void add()
{
Scanner keyboard = new Scanner(System.in);
int a;
int b;
System.out.print("\nEnter a : ");
a = keyboard.nextInt();
System.out.print("Enter b : ");
b = keyboard.nextInt();
int c=a+b;
GrandSum += c;//added
System.out.println("\nans is :"+c);
}
public static void main(String args[])
{
math ob_main=new math();
ob_main.add();
//.....repeat as many times you want
ob_main.add();
System.out.println("grandsum: " + ob_main.GrandSum);
}
}
答案 5 :(得分:0)
更改
public void add() {
Scanner keyboard = new Scanner(System.in);
int a;
int b;
int total = 0;
for(int i = 0; i < 2; i++) {
System.out.print("\nEnter a : ");
a = keyboard.nextInt();
System.out.print("Enter b : ");
b = keyboard.nextInt();
int c = a+b;
total += c;
}
System.out.println("\nans is :"+total);
}
答案 6 :(得分:0)
有一个total
变量,您只需将c
添加到。
我还将您不需要的递归更改为while循环。
public class math
{
public static void main(String args[])
{
int total = 0;
Scanner keyboard = new Scanner(System.in);
while (true)
{
System.out.print("\nEnter a (-999 to quit): ");
int a = keyboard.nextInt();
// terminating condition, modify appropriately
if (a == -999)
break; // break out of the while-loop
System.out.print("Enter b: ");
int b = keyboard.nextInt();
int c = a + b;
total += c;
System.out.println("\nans is: " + c);
}
System.out.println("total is: " + total);
}
}
答案 7 :(得分:0)
package farzi;
import java.util.ArrayList;
import java.util.Scanner;
public class dummy {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
ArrayList<Integer> sum = new ArrayList<Integer>();
String choice = "";
do{
System.out.println("enter the first number");
int a = keyboard.nextInt();
System.out.println("enter the second number");
int b = keyboard.nextInt();
int tempSum = a+b;
list1.add(a);
list2.add(b);
sum.add(tempSum);
System.out.println("Do you want to continue : type yes or no");
choice = keyboard.next();
}while(choice.toLowerCase().charAt(0)=='y');
System.out.println("here are the inputs with theri sum");
System.out.println("num1\t num2\t sum");
for(int i=0;i<list1.size();i++)
{
System.out.println(list1.get(i)+"\t"+list2.get(i)+"\t"+sum.get(i));
}
}
}