Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. public class bt7TongN{
  3.  
  4.     public static int nhapInt(){
  5.     Scanner n=new Scanner(System.in);
  6.     boolean check =false;
  7.     int a=0;
  8.     while(!check){
  9.         try{
  10.             a=n.nextInt();
  11.             if (a>0){
  12.                 check=true;
  13.             }else{
  14.                 System.out.println("Du lieu vao khong dung, nhap lai: ");
  15.             }
  16.         }catch(Exception e){
  17.             System.out.println("Du lieu vao khong dung, nhap lai: ");
  18.             n.nextLine();
  19.         }
  20.     }
  21.     return(a);
  22. }
  23.     public static void main(String[] args)
  24.     {
  25.         int n=0,s=0;
  26.         System.out.print("Nhap vao gia tri n (nguyen): ");
  27.         n=nhapInt();
  28.         for(int i=1;i<=n;i++)
  29.         s=s+i;
  30.         System.out.println("Tong tu 1 den "+n+" la: "+s);
  31.        
  32.        
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement