• Java输入输出入门 A+B


    描述

    求两个整数之和。

    输入 

    输入数据只包括两个整数A和B。

    输出 

    两个整数的和。

    样例输入

    1 2

    样例输出

    3

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int a=sc.nextInt(),b=sc.nextInt();
            System.out.println(a+b);
        }    
    }

    输入只有一组数据,比较简单

    描述

    计算A+B

    输入

    输入数据有多组。
    每组一行,为整数A, B。
    以EOF做结束。

    输出

    输出A+B的值。

    样例输入

    1 2

    3 4

    样例输出

    3

    7

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {    
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext())
            {
                int a=sc.nextInt(),b=sc.nextInt();            
                    System.out.println(a+b);
            }
        }    
    }

    输入是多组数据,并且以EOF结尾,类似于C或C++中的while(......!=EOF)/while(cin>>...),Java中可以用hasNext()表示,以CTRL+Z退出输入,目前只get到这一种办法

    描述

    计算A+B

    输入

    输入第1行为一个整数n(1≤n≤10),代表测试的组数。
    下面有n组测试数据,每组1行,为2个整数,为A, B。

    输出

    输出A+B的值。

    样例输入

    2

    1 2

    3 4

    样例输出

    3

    7

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {    
            Scanner sc=new Scanner(System.in);        
            int n=sc.nextInt();
            int t=0;
            while(true)
            {
                if(t==n)
                    break;
                int a=sc.nextInt(),b=sc.nextInt();
                System.out.println(a+b);
                t++;
            }
        }    
    }

    也是多组输入,不过这个是先输入一个n,后面再输入n组数据,目前也就只想到这一种方法,增设一个变量,当它的值从0一直加到n时,便结束输入,不再计算

    描述

    计算A+B

    输入

    输入数据有多组。
    每组一行,为两个整数A, B。
    输入以0 0结束。

    输出

    输出A+B的值。

    样例输入

    1 2

    0 0

    样例输出

    3

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {    
            Scanner sc=new Scanner(System.in);        
            while(true)
            {
                int a=sc.nextInt(),b=sc.nextInt();
                if(a==0&&b==0)
                    break;
                System.out.println(a+b);
            }
        }    
    }

    这个比较简单,输入以0 0结束,直接判断为0 0退出就ok 了

    描述

    计算A+B

    输入

    输入数据有多组。
    每组一行,为两个整数A, B。
    输入以0 0结束。

    输出

    输出A+B的值,每组数据之间保留一个空行。

    样例输入

    1 2

    3 4

    0 0

    样例输出

    3

     

    7

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {    
            Scanner sc=new Scanner(System.in);
            int t=0;
            while(true)
            {
                int a=sc.nextInt(),b=sc.nextInt();
                if(a==0&&b==0)
                    break;        
                if(t==1)
                    System.out.println();
                t=1;
                System.out.println(a+b);
            }
        }    
    }

    输出时每组数据之间有一个空行

  • 相关阅读:
    转载php在IIS中运行
    程序员必去的网站
    分享一下jQuery UI的地址
    dbcp相关配置
    shell学习第二弹-进阶
    shell学习第一弹-初识
    java servlet 3.0文件上传
    Junit使用第二弹
    各个数据库中,查询前n条记录的方法
    junit使用第一弹
  • 原文地址:https://www.cnblogs.com/andrew3/p/10038795.html
Copyright © 2020-2023  润新知