Jump to content

Another simple one!!!

- - - - -

  • Please log in to reply
10 replies to this topic

#1
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
The for loop is coded correctly but I cannot get it to run without error. Any help appreciated.

public static void main(String[] argv){

    

        int number1;

        int number2;


      Scanner input = new Scanner(System.in);

      System.out.println(" Multiplication\n");


      System.out.print("First : ");

      number1 = input.nextInt();


      System.out.print("Second: ");

      number2 = input.nextInt();


      // multiplication for loop

      int answer  = 0;

      for (number2 > 0; answer += number1; number2-- ){ 

      System.out.println("Multiplication result is " + answer);       

      }

    }

Edited by ZekeDragon, 12 March 2011 - 03:12 PM.
Please use [CODE] tags (the # button) when posting code.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
1)
public static void main(String[] argv){
should be
public static void main(String[] arg[B][COLOR="red"]s[/COLOR][/B]){

2)

for (number2 > 0; [B][SIZE="4"]answer += number1[/SIZE][/B]; number2-- ){

The bold part should be a boolean, like answer == number1.
+= just adds number1 to answer, it's no boolean.

Also, number2>0 doesn't have to be a boolean.

A for-loop's structure is like so:

for(DoOnceBeforeLoop ; condition ; doAfterEachLoop){ }


for(int i=0 ; i<5 ; i++){  }

Is the same as

int i=0;

while(i<5){

  ..

  ...

  i++;

}



#3
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Ok so here is my revised code and now instead of displaying the value of 100 because 10*10 is 100 it displays every single loop. I am sure this is an easy fix but I am stuck.

public static void main(String[] args){

    

        int n1, n2;


      Scanner input = new Scanner(System.in);

      System.out.println("Multiplication by repetitive addition\n");


      System.out.print("Please enter the first positive integer: ");

      n1 = input.nextInt();


      System.out.print("Please enter the second positive integer: ");

      n2 = input.nextInt();


      // multiplication through repetitive addition using a for loop

      int result  = 0;

      for (result += n1; n2 > 0;  n2--) 

      System.out.println("Multiplication result is " + result);       

      


      


    }

Edited by ZekeDragon, 12 March 2011 - 03:11 PM.
Please use [CODE] tags (the # button) when posting code.


#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
That's because result += n1, the first statement of the for loop, is only done 1 time, Before the loop. After that one time, it never gets done again. Just put it inside.

#5
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
What do you mean put it inside, I put brakets around System.out.println("Multiplication result is " + result); but that didnt change anything. I dont think I am following what you mean.

#6
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

wim DC said:

1)
public static void main(String[] argv){
should be
public static void main(String[] arg[B][COLOR="red"]s[/COLOR][/B]){

That's not true. You can name it String[] blargowertnw or String werwehl32[], it would be valid.

As for the problem,
 for (result += n1; n2 > 0;  n2--) 
should be
 for (result = n1; n2 > 0;  n2--)

But even with that correction, your logic isn't quite there yet.

#7
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Right because the output is still the same. That is where I am stuck getting the output to display the computation once not 10 times.

#8
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

      int result  = 0; //we initialize a variable to keep track of the final result

      for (int i = n2; i > 0; i--) //we loop n2 times

		result += n1; //every time we loop, we add the n1 number to the result

	System.out.println(result); //print out the result


The logic is, we loop n2 times, every loop we add n1, so if we have n1 = 3, n2 = 4, it would do something like this.
result = 3
result = 6
result = 9
result = 12
And our loop is done, we print the result, which will display 12.

result += n1 is the same as result = result + n1

#9
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Took me awhile but I think I am seeing it work now. the first statement int i = n2; tells us how many times we are to loop. The i >0; i-- //are the condition on the loop? Then the next statement result += n1; add n1 to the result? Lastly, we needed to initiate another variable which in this case was i to define the rules of the loop, correct? Thanks for the help so far.

#10
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
That's pretty much right, i is there to keep track of how many times we want to run the loop. The loop will run while i is greater than 0.

If we take the same example,
n2 = 4,
n1 = 3.

The loop will do something like this with "i".
i gets initialized to 4 (since n2 = 4)
the loop run, i is now 3
the loop run, i is now 2
the loop run, i is now 1
the loop run, i is now 0
the loop does not run now. Because i is not greater than 0 anymore.

The for loops are much easier to understand if we add to our variable instead of substracting.

The same code with an addition to "i" instead of a substraction.


int result  = 0; 

for (int i = 0; i < n2; i = i + 1) {

    result = result + n1; 

}

System.out.println(result); 


i starts at 0.
we run the loop, we check if i is smaller than 4, yes it is, we add 1, i is now 1.
we run the loop, we check if i is smaller than 4, yes it is, we add 1, i is now 2.
we run the loop, we check if i is smaller than 4, yes it is, we add 1, i is now 3.
we run the loop, we check if i is smaller than 4, yes it is, we add 1, i is now 4.
we run the loop, we check if i is smaller than 4, no it's not, we stop looping.

#11
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thanks, for some reason I wasn't seeing the fact that I needed to initiate another variable to hold the loop rules!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users