【备战蓝桥杯第5天】2015年12月16…
2016-03-15 21:28
274 查看
错误的代码如下
package com.mz;
import java.util.*;
package com.mz;
import java.util.*;
public class Main {
public static void main(String[] args)
throws Exception {
int n;
long sum=0l;
Scanner sc=new
Scanner(System.in);
n=sc.nextInt();
sum=n*(n+1)/2;
//问题所在,把n定义为int
但在赋给sum值时,导致sum也变为int
System.out.println(sum);
}
}
public class Main {
public static void main(String[] args) throws Exception
{
long n=0l;
//直接把n定义为int,注意定义时在0后写L,说明是长整型
long sum=0l; //同理
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
sum=n*(n+1)/2;
System.out.println(sum);
}
}
package com.mz;
import java.util.*;
package com.mz;
import java.util.*;
public class Main {
public static void main(String[] args)
throws Exception {
int n;
long sum=0l;
Scanner sc=new
Scanner(System.in);
n=sc.nextInt();
sum=n*(n+1)/2;
//问题所在,把n定义为int
但在赋给sum值时,导致sum也变为int
System.out.println(sum);
}
}
public class Main {
public static void main(String[] args) throws Exception
{
long n=0l;
//直接把n定义为int,注意定义时在0后写L,说明是长整型
long sum=0l; //同理
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
sum=n*(n+1)/2;
System.out.println(sum);
}
}
相关文章推荐
- 【备战蓝桥杯第6天】复习数组相关…
- 【备战蓝桥杯第6天】复习方法相关…
- 【备战蓝桥杯第6天】复习-方法调用…
- 【备战蓝桥杯第7天】复习面向对象…
- nav标签使用说明
- 使用R语言和XML包抓取网页数据-Scraping data from web pages in R with XML package
- Servlet监听器
- ASCII码表
- 自我总结(九)---
- linux 基础命令(1)
- react-native 在任意view fragment activity中集成react
- iOS:SVN出错 Description : The working copy is locked due to a previous error
- HUST 1010 The Minimum Length
- 搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
- TCP/IP协议三次握手与四次握手流程解析
- CodeVS 2597团伙(并查集)
- 前置++i和后置的i++效率探究
- 安卓状态栏+kitkat
- 系统MPMoviePlayerController点击全屏按钮横屏显示方法
- centos7.0 安装LNMP运行环境