Quellcode durchsuchen

Program prompts user to input an integer and then returns the sum of all integers between 1 and the input.

Jennifer Chao vor 6 Jahren
Ursprung
Commit
4ceeb4df58
4 geänderte Dateien mit 29 neuen und 9 gelöschten Zeilen
  1. BIN
      Main.class
  2. 5
    0
      Main.ctxt
  3. 16
    1
      Main.java
  4. 8
    8
      package.bluej

BIN
Main.class Datei anzeigen


+ 5
- 0
Main.ctxt Datei anzeigen

@@ -0,0 +1,5 @@
1
+#BlueJ class context
2
+comment0.target=Main
3
+comment1.params=
4
+comment1.target=void\ addIntegers()
5
+numComments=2

+ 16
- 1
Main.java Datei anzeigen

@@ -1,9 +1,24 @@
1 1
 /**
2 2
  * Created by iyasuwatts on 10/17/17.
3 3
  */
4
+import java.util.Scanner;
5
+
4 6
 public class Main {
5 7
 
6
-    public static void main(String[] args){
8
+    public static void addIntegers() 
9
+    {
10
+        Scanner input = new Scanner(System.in);
11
+
12
+        int userInput;
13
+        System.out.println("Please enter a positive integer: ");
14
+        userInput = input.nextInt();
7 15
 
16
+        int sumTotal = 0;
17
+        
18
+        for (int i = 1; i <= userInput; i++) { 
19
+           sumTotal += i;
20
+        }
21
+        
22
+        System.out.println("The sum of the integers between 1 and " + userInput + " equals " + sumTotal + ".");
8 23
     }
9 24
 }

+ 8
- 8
package.bluej Datei anzeigen

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=0
3
-editor.fx.0.width=0
4
-editor.fx.0.x=0
5
-editor.fx.0.y=0
2
+editor.fx.0.height=709
3
+editor.fx.0.width=800
4
+editor.fx.0.x=993
5
+editor.fx.0.y=191
6 6
 objectbench.height=164
7 7
 objectbench.width=776
8 8
 package.divider.horizontal=0.6
9 9
 package.divider.vertical=0.6845018450184502
10 10
 package.editor.height=364
11 11
 package.editor.width=674
12
-package.editor.x=-816
13
-package.editor.y=445
12
+package.editor.x=943
13
+package.editor.y=314
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0
@@ -28,5 +28,5 @@ target1.name=Main
28 28
 target1.showInterface=false
29 29
 target1.type=ClassTarget
30 30
 target1.width=80
31
-target1.x=70
32
-target1.y=10
31
+target1.x=170
32
+target1.y=120