手记

JAVA学习

JAVA学习(一)

    JAVA 学习

        题目要求(A+B Problem)

        通过代码

        代码分析

        结语

JAVA 学习

最近学习java,使用浙江大学的online judge 系统作为练习,以后会不定期更新学习进度,希望自己能坚持下去,也希望大家多多指教。


题目要求(A+B Problem)

Calculate a + b

Output

For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.

Sample Input

1 5

Sample Output

6

Hint

Use + operator

通过代码

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

代码分析

import java.util.Scanner; //导入类

public class Main////源文件名必须和类名相同

 public static void main(String[] args)  //主方法入口

 { 

  Scanner scan=new Scanner(System.in);//Scanner实例化 获取在控制台输入的字符串,System.in是调入输入的字符串

  //使用while循环判断是否有下一个输入

     while(scan.hasNext()){  // 如果此扫描器的输入中有另一个标记,则返回 true。

        int a=scan.nextInt();  //此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 int 值,则返回 true。

        int b=scan.nextInt();  

        System.out.println(a+b);  //输出a+b的和

     }  

 }  

}

结语

部分资源来自网路,如有侵权或错误请及时联系更改。

————————————————

版权声明:本文为CSDN博主「hbyhgdyxgs」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

0人推荐
随时随地看视频
慕课网APP