public static void main(String[] args)
{
int[][] plusUnit = new int[1024][2];
Scanner jiafa = new Scanner(System.in);
int a = -1;
int b = -1 ;
int i = 0;
while(a!=0&&b!=0)
{
a = jiafa.nextInt();
b = jiafa.nextInt();
if(a==0&&b==0)
{
break;
}
plusUnit[i][0] = a;
plusUnit[i][1] = b;
i++;
}
for(int[] cell_plus : plusUnit)
{
if(cell_plus[0]==0&&cell_plus[1]==0)
{
break;
}
int c = cell_plus[0] + cell_plus[1];
System.out.println(c);
}
}
main 方法体修改如上,亲测可编译运行。你错误的地方在于每次输入后,你立即用int c = a + b;这条语句进行计算,按要求应该是先将每一行输入看作一个算式整体,先不进行计算而是保存起来,再在后面进行相应的计算(不管是加法还是减法,计算的主动权在你手里),然后输出。
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner jiafa = new Scanner(System.in);
int a, b;
do {
a = jiafa.nextInt();
b = jiafa.nextInt();
if (a == 0 && b == 0) {
break;
}
int c = a + b;
System.out.println(c);
} while (true);
}
}
public void test(int a, int b){
if(a!=0&&b!=0){
System.out.println(a+b);
}
}
JAVA 应该更容易才是
import java.io.*;
public class FileA_B {
public static void main(String[] args) throws java.io.IOException {
BufferedReader in_=new BufferedReader(new FileReader("FileA_B.txt"));
String line;
while((line=in_.readLine())!=null){
if(line.trim().length()<1){
continue;
}
String arr[]=line.split(" ");
int a=Integer.parseInt(arr[0]);
int b=Integer.parseInt(arr[1]);
if(a==0 && b==0){
continue;
}
//System.out.println(a+"+"+b+"="+(a+b));
System.out.println(a+b);
}
in_.close();
}
}