瀏覽代碼

Modified Main.java file to print sum of numbers 1 through an inputted number

Connor Dunnigan 6 年之前
父節點
當前提交
3fe72fb97f
共有 2 個檔案被更改,包括 16 行新增6 行删除
  1. 二進制
      Main.class
  2. 16
    6
      Main.java

二進制
Main.class 查看文件


+ 16
- 6
Main.java 查看文件

@@ -1,9 +1,19 @@
1
-/**
2
- * Created by iyasuwatts on 10/17/17.
3
- */
4
-public class Main {
1
+import java.util.Scanner;
5 2
 
6
-    public static void main(String[] args){
3
+public class Main{
7 4
 
8
-    }
5
+public static void main(String[] args){
6
+
7
+  Scanner stdin = new Scanner(System.in);
8
+
9
+  int num, sum = 0;
10
+
11
+  System.out.println("Please enter a number");
12
+  num = stdin.nextInt();
13
+
14
+  for(int i=1; i<=num; i++){
15
+    sum+=i;
16
+  }
17
+  System.out.println("The sum of the numbers 1 to " + num + " = " + sum);
18
+ }
9 19
 }