Pārlūkot izejas kodu

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

Connor Dunnigan 6 gadus atpakaļ
vecāks
revīzija
3fe72fb97f
2 mainītis faili ar 16 papildinājumiem un 6 dzēšanām
  1. Binārs
      Main.class
  2. 16
    6
      Main.java

Binārs
Main.class Parādīt failu


+ 16
- 6
Main.java Parādīt failu

@@ -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
 }