java 从一加到任意值
答案:6 悬赏:70 手机版
解决时间 2021-03-01 21:44
- 提问者网友:相思似海深
- 2021-03-01 16:40
java 从一加到任意值
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-03-01 18:04
import java.util.Scanner;
public class TestScan {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个整数:");
int n=sc.nextInt();
int count=0;
for(int i=1;i<=n;i++){
count=count+i;
}
System.out.println("1—"+n+"的和是: "+count);
}
}
public class TestScan {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个整数:");
int n=sc.nextInt();
int count=0;
for(int i=1;i<=n;i++){
count=count+i;
}
System.out.println("1—"+n+"的和是: "+count);
}
}
全部回答
- 1楼网友:话散在刀尖上
- 2021-03-01 23:41
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("从1加到"+n+"的结果是:"+((1+n)*n)/2);
}
// TODO Auto-generated method stub
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("从1加到"+n+"的结果是:"+((1+n)*n)/2);
}
- 2楼网友:玩家
- 2021-03-01 22:20
int sum = 0;
sum = (1+n)*n/2;
sum = (1+n)*n/2;
- 3楼网友:蓝房子
- 2021-03-01 21:00
public static void main(String[] args) {
// 输入 n 的值
int n = 500;
long total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
System.out.println("总计:=" + total);
}
// 输入 n 的值
int n = 500;
long total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
System.out.println("总计:=" + total);
}
- 4楼网友:一叶十三刺
- 2021-03-01 19:29
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.print("请输入一个整数:");
int n = new Scanner(System.in).nextInt();
int sum = 0;
for(int i = 1; i <= n; i++){
sum += i;
}
System.out.println("从1加到" + n + "和为" + sum );
}
}
-----------测试结果
请输入一个整数:8
从1加到8和为36
public class Test {
public static void main(String[] args) {
System.out.print("请输入一个整数:");
int n = new Scanner(System.in).nextInt();
int sum = 0;
for(int i = 1; i <= n; i++){
sum += i;
}
System.out.println("从1加到" + n + "和为" + sum );
}
}
-----------测试结果
请输入一个整数:8
从1加到8和为36
- 5楼网友:纵马山川剑自提
- 2021-03-01 18:53
import java.util.Scanner;
public class TestIn {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个整数:");
int n=scanner.nextInt();
int sum=0;
for (int i=1;i<=n;i++){
sum+=i;
}
System.out.println("从1到"+n+"的和为:"+sum);
}
}
public class TestIn {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个整数:");
int n=scanner.nextInt();
int sum=0;
for (int i=1;i<=n;i++){
sum+=i;
}
System.out.println("从1到"+n+"的和为:"+sum);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯