NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

For Loop

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
StealthyDead



Joined: 23 Jun 2010
Posts: 4

PostPosted: Wed Jun 23, 2010 5:29 am    Post subject: For Loop Reply with quote

Hello fellow NetBeans users.

I am using the step by step tutorial for using NetBeans here:

http://www.homeandlearn.co.uk/java/java_for_loops.html

I'm stuck here. In the exercise where it says to make the times table, I can but not in the way they say.

First, they say that the programme only needs two lines in between the curly brackets. I only used one, the System.out.println(); one.

Second, they say that the programme only needs two lines in between the curly brackets, AND one of them should be the user_input statement to find the times table they want. I used it before I introduced the loop.

I realized that the tablenumber, as I called the integer which needed the user_input.nextInt(), wasn't the thing that needed to have anything added. It was the constant. So I created an integer called "multi" to multiply it by and had IT increase by one each time in the loop until it wasn't less than the end value (11). Then I created the System.out.println() statement and had "multi * tablenumber" (without quotations) in there. And what surprised me was that it created the command and then displayed it.

Which means that if I had the command before the System.out.println() statement in the curly brackets, it would do it twice. How would I tell it to print out the answer to the multiplication without making it happen twice. I'd tried the multi = multi * tablenumber in the curly brackets after the loop but when I System.out.print.ln(... + multi); it ran through and it would say multi twice. Like "0 times 10 is 0" then it would run twice and store multi and the second time it printed, it'd say "10 times 10 is 10."

Here's the programme for you to see and evaluate. Please inform me how I got this to work against what they said and what I would have to do to do it their way, as they'll likely call it into play in the next lesson on loops and I did it some other way so I'd be lost.

Thanks for reading through all this. I'm 14 and started this 3 days ago, so I'm lost right now and I want to get my head around this before I move on and get in over my head (as if I'm not already LOL).

Here's the code I wrote:




package timestable;

import java.util.Scanner;

public class TimesTable {


public static void main(String[] args) {
Scanner userInput = new Scanner (System.in);

int tablenumber;
System.out.print("What times table do you want? ");
tablenumber = userInput.nextInt();

int end_value;
end_value = 11;

int multi = 0;

for(multi = 0; multi<end_value; multi++){

System.out.println(multi + " " + "times" + " " + tablenumber + " " + "is"+ " " + multi * tablenumber);
}


}

}





You can copy and paste it into your NetBeans and see that it works and fiddle with it and help me out easier. Thanks. Appreciate it.



Again, thanks and I'd really appreaciate ANY reply and/or help,
Nathan
Back to top
Bayless Kirtley
Posted via mailing list.





PostPosted: Wed Jun 23, 2010 1:11 pm    Post subject: [SPAM] Re: For Loop Reply with quote

Just a couple of pointers here. What you have written is good and compact
although it may be more obscure than using 2 lines thusly.

int ans;
for(multi = 0; multi<end_value; multi++){
ans = multi * tablenumber;
System.out.println(multi + " times " + tablenumber + " is "
+ ans);
}

Create a new int for the calculated value. If you change the value of multi,
the control variable of the for loop, within the loop then
you generally have defeated the purpose of the controlled loop. That is, the
changed value is retained at the next loop iteration.
That is rarely not what you want to happen. Also notice that I have cleaned
up the String portions of your println statement. There
is no need to put those spaces in as separate Strings and that makes it
harder to read. Hope this helps.

Bayless


----- Original Message -----
From: "StealthyDead" <address-removed>
To: <address-removed>
Sent: Wednesday, June 23, 2010 12:30 AM
Subject: [nbusers] For Loop


Quote:
Hello fellow NetBeans users.

I am using the step by step tutorial for using NetBeans here:

http://www.homeandlearn.co.uk/java/java_for_loops.html

I'm stuck here. In the exercise where it says to make the times table, I
can but not in the way they say.

First, they say that the programme only needs two lines in between the
curly brackets. I only used one, the System.out.println(); one.

Second, they say that the programme only needs two lines in between the
curly brackets, AND one of them should be the user_input statement to find
the times table they want. I used it before I introduced the loop.

I realized that the tablenumber, as I called the integer which needed the
user_input.nextInt(), wasn't the thing that needed to have anything added.
It was the constant. So I created an integer called "multi" to multiply it
by and had IT increase by one each time in the loop until it wasn't less
than the end value (11). Then I created the System.out.println() statement
and had "multi * tablenumber" (without quotations) in there. And what
surprised me was that it created the command and then displayed it.

Which means that if I had the command before the System.out.println()
statement in the curly brackets, it would do it twice. How would I tell it
to print out the answer to the multiplication without making it happen
twice. I'd tried the multi = multi * tablenumber in the curly brackets
after the loop but when I System.out.print.ln(... + multi); it ran through
and it would say multi twice. Like "0 times 10 is 0" then it would run
twice and store multi and the second time it printed, it'd say "10 times
10 is 10."

Here's the programme for you to see and evaluate. Please inform me how I
got this to work against what they said and what I would have to do to do
it their way, as they'll likely call it into play in the next lesson on
loops and I did it some other way so I'd be lost.

Thanks for reading through all this. I'm 14 and started this 3 days ago,
so I'm lost right now and I want to get my head around this before I move
on and get in over my head (as if I'm not already LOL).

Here's the code I wrote:




package timestable;

import java.util.Scanner;

public class TimesTable {


public static void main(String[] args) {
Scanner userInput = new Scanner (System.in);

int tablenumber;
System.out.print("What times table do you want? ");
tablenumber = userInput.nextInt();

int end_value;
end_value = 11;

int multi = 0;

for(multi = 0; multi<end_value; multi++){

System.out.println(multi + " " + "times" + " " + tablenumber + " " + "is"+
" " + multi * tablenumber);
}


}

}





You can copy and paste it into your NetBeans and see that it works and
fiddle with it and help me out easier. Thanks. Appreciate it.



Again, thanks and I'd really appreaciate ANY reply and/or help,
Nathan



Back to top
StealthyDead



Joined: 23 Jun 2010
Posts: 4

PostPosted: Wed Jun 23, 2010 4:52 pm    Post subject: Reply with quote

Thanks so much, man. Helped out a lot. I didn't thinnk about introducing a whole new integer into the equation. Thanks for taking the time to walk me through it. Like I said, I'm 14. You see, I see myself majoring as a Computer Programming Engineer at University of Illinois and I'd like to get some background to at least some of the sequences before Freshman year. And no, I didn't get held back. I have a really early birthday - about a week after school starts. I actually could have skipped a grade but didn't. Anyways, like I said, I just wanted to get some background early on so that when it's introduced in any other instance, I'll have at least a hint of what's going on and what's going to happen next.

Thanks again man for your time and reply. Helped me out a ton.

Thanks,
Nathan
Back to top
StealthyDead



Joined: 23 Jun 2010
Posts: 4

PostPosted: Wed Jun 23, 2010 11:45 pm    Post subject: Reply with quote

Here's the revised code using your recommendations. I've also added another user prompted value to the end value to extend the options and custimizable ability of the times table.

New Code:





package timestable;

import java.util.Scanner;

public class TimesTable {


public static void main(String[] args) {
Scanner userInput = new Scanner (System.in);

int tablenumber;
System.out.print("What times table do you want? ");
tablenumber = userInput.nextInt();

int end_value;
System.out.print("What number do you want the table to end at? ");
end_value = userInput.nextInt();

int multi = 0;

int answer;

for(multi = 0; multi<=end_value; multi++){
answer = multi * tablenumber;
System.out.println(multi + " times " + tablenumber + " is " + answer);
}
System.exit(0);


}

}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo