zoukankan      html  css  js  c++  java
  • 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);
            }
        }    
    }

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

  • 相关阅读:
    修改silverlight DataGrid当前选中行及选中列的背景色
    转 Introduction to SharePoint Feature Stapling – Part 2
    File.ReadAllText方法,File.WriteAllText方法修改文件内容
    用xlst将xml转换html
    简单在线编辑器<转>
    随机数生成
    asp.net 2.0 的代码隐藏
    Microsoft .NET Pet Shop 3.x: .NET Pet Shop 的设计模式与体系结构
    并发术语解释
    如何在Render方法中修改替换HtmlTextWriter中的内容
  • 原文地址:https://www.cnblogs.com/andrew3/p/10038795.html
Copyright © 2011-2022 走看看