English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am making a new question b/c i didnt specify that i was using java. ur supposed to take the inetger that the user inputs and use a for loop so it starts from 2 keeps adding until it get to the intege rinputed. for example if the user inputs 5, the program should print out 14. (2+3+4+5) how would u write that using for loop? thanks in advance.

here is what i have so far

import java.util.Scanner;
public class Basic {

public static void main(String[] args) {
// Create application frame.
BasicFrame frame = new BasicFrame();
Scanner input = new Scanner(System.in);
int x;


System.out.print("Enter a number: ");
x = input.nextInt();

for (int i = 2; i <= x; i++)
{
System.out.println(i);

}

2007-10-04 09:59:33 · 3 answers · asked by sniper88gt 1 in Computers & Internet Programming & Design

3 answers

Replace your for loop with this:

int j = 0;
for (int i = 2; i <= x; i++)
{
j+=i;
}
System.out.println("" + j);


(appending an int to a blank string is a good way to make it play nice, since ints don't have toStrings)

Two other things you should be aware of:

* if your Scanner doesn't find an int, it may throw an exception.
* if someone inputs an integer less then 2, it'll never finish (since starting with two and adding one each time will never get to that number). you'll get an integer overflow (where the number keeps growing until it's too big to fit into an integer's space)

2007-10-04 10:03:44 · answer #1 · answered by Yanni Depp 6 · 0 0

i haven't touched Java in a protracted time, yet I nevertheless remember loops extremely. For loops are count type-controlled loops - which means the loop stops as quickly because it fairly is been by it a definite sort of circumstances (which you establish). For (int x=0; x=9; x++) i think of the beginning up of a for loop regarded some thing like that. the 1st section, x=0, tells you the place the counter variable starts off. the 2d section, x=9, tells you whilst the loop desires to cease (whilst x equals 9), and the 0.33 section, x++, tells you approaches lots desires to be extra to the counter variable whenever you flow throughout the time of the loops (x++ is a shortcut for x = x+a million) on a similar time as and do on a similar time as loops are adventure controlled (i think of that's what they have been stated as) variables. which means the loops do not cease till an adventure occurs. as an occasion: on a similar time as x > 9 (do not remember what on a similar time as loops gave the impact of in Java) That fact says that the loop won't cease till x is larger than 9. So someplace in the loop coding you're able to upload to x. on a similar time as x > 9 { y++ x = x + 2 } Do { y++ x = x + 2 on a similar time as x > 9 on a similar time as loops and do on a similar time as loops are diverse because of the fact a on a similar time as loop is a pre-attempt (pre-some thing, do not remember) loop and a do on a similar time as loop is a placed up-attempt (placed up-some thing, do not remember) loop. this means that for the period of a on a similar time as loop, the computing gadget assessments if x>9 till now it is going throughout the time of the loop. And in a do on a similar time as loop, the computing gadget does not verify till after it has long gone throughout the time of the loop a minimum of as quickly as. int x = 10 on a similar time as x > 9 { y++ x = x + 3 } in this, x is already extra advantageous than 9, so it does not flow throughout the time of the loop in any respect. int x = 10 do { y++ x = x + 3 on a similar time as x > 9 in this, it does not verify for x till the top, so it is going throughout the time of the loop one time. i'm hoping that helped!

2016-11-07 06:39:48 · answer #2 · answered by cauley 4 · 0 0

int sum = 0;

for (int i = 2; i <= x; i++ ) {
if ( i != 2 ) {
System.out.print(" + ");
}
System.out.print(i);
sum += i;
}

System.out.println(" = " + sum);

2007-10-04 10:06:14 · answer #3 · answered by JD 2 · 0 0

fedest.com, questions and answers