开始用java刷题

计算a+b的和

每行包行两个整数a和b
对于每行输入对应输出一行a和b的和
输入
1 5
输出

6

题目解答

import java.util.*;
public class Main
{
public static void main(String srgs[])
{
int a,b;
Scanner input = new Scanner(System.in);
        while(input.hasNext()) //检查输入的是否是数字  不是则跳过循环
        {
a= input.nextInt();
b= input.nextInt();
System.out.println(a+b);
        }
}
}



查找到网上的另一个例子

public  class  Test {
     public  static  void  main(String args[]){
         Scanner input =  new  Scanner(System.in);
         System.out.println( "请输入一个数" ); //1.输入非数字abc
         int  age =  0 ;
         while  ( true ) {
         if  (input.hasNextInt() ==  true ) {/ 2 ./检查是否是数字,结果不是,跳过 if 语句块
         age = input.nextInt();
         System.out.println( "true" );
         } else  { //3.执行打印操作,关键问题来,系统把false当做System.in读进来了,所以就跟1一样咯
         System.out.println( "false" );
 
         }
         }
     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值