Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com how to define multiple variables within a loop

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-09-2013, 06:01 AM PM User | #1 uswhovian New to the CF scene   Join Date: Mar 2013 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts how to define multiple variables within a loop I'm having trouble with a homework assignment. I'll post the broad objective of it below so you understand what the end result should be. I understand how to use if loops to compare numeric values and tell the user if a number is greater lesser than or equal to another. I understand the basic concepts of while loops and how to set parameters on how many times they run. Yet I have spent hours trying to figure out how I can compare two user values within a loop. My only thought of how to perhaps accomplish this is to get user input twice during the while loop(meaning I would only loop5 times instead of 10) yet I don't know if this will work or if this is the best way to tackle the problem. It only needs to be written in pseudocode at the moment which helps. But i am just at a loss of how to define and compare user inputted values that are constantly changing within a loop. I've attached a picture of the assignment objective, if my question does not make sense out of context. I would appreciate any help with this aspect of my assignment. I am almost at my wits end! Once I understand this I can easily write the pseduocode and draw the flowchart by myself. Yet I am at a standstill without knowing how to define the variables in this particular situation. In this assignment, you will write an algorithm that asks the user for 10 different numbers. Aﬁer each number, you should print whether this number is bigger than, smaller than, or the same as the one that came right before it. (Note that you won’t print anything after the ﬁrst number, as nothing came before it.) Below is an example. I have put in BLACK the information that your algorithm should print, and in RED information that the user is providing. (The red is just the parts that says x is bigger/smaller than y) Enter numberi 14 Enter numberi 20 20 is bigger than 14 Enter numberi 5 5 is smaller than 20 Enter numberi 5 5 is the same as 5 Enter numberi 10 10 is bigger than 5 Enter numberi -100 -100 is smaller than 10 Enter numberi 0 0 is bigger than -100 Enter numberi 0 0 is the same as 0 Enter numberi 57 57 is bigger than 0 Enter numberi 25 25 is smaller than 57 You should type your algorithm Your solution should include: 0 The steps to solving the problem (your steps should look similar to the examples in the Designing Loops lecture notes). You should use a loop to get the 10 numbers from the students, and you should use appropriate variables for storing the current number entered by the user and the previous number entered by theuser Sorry for the weird spelling things I used an OCR translator to get the info in txt form
03-09-2013, 05:03 PM   PM User | #3
uswhovian
New to the CF scene

Join Date: Mar 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
defining ints?

Quote:
 Originally Posted by Philip M Please have a look at forum rule #1.5. This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work. The idea is that you compare each number entered by the user with the previous number. Initially the previous number is 14, the user then enters the first comparison which is 20. 20 is compared with 14 and the result given. 20 then becomes the previous number, and the user enters the new number to be compared with that, which is 5. And so on. OK? So your comparisons are simple:- if (numEntered > previous) { // greater if (numEntered < previous) { // smaller if (numEntered == previous) { // same You need to ensure that the user only enters numbers (not ? or Mickey Mouse). Also, be aware that values entered by the user are always strings and not numbers unless/they are converted to numbers using one of several methods. Here you can not bother with that (luckily) as Javascript will perform the type conversion automatically for you. But in some situations that will cause bad problems. You will also need some way of terminating the loop when the user gets tired of it (while loop) or specify that the thing goes on for a fixed 10 numbers (for... loop where final loop count = 10). So you need to inform the user in some way that "This is comparison number x out of 10". Please be aware that prompt(), alert() and document.write() are all obsolete. If you are being taught modern Javascript (and not a History of Javascript) you should use DOM methods to obtain data from the user and display messages/results. It is your responsibility to die() if necessary….. - PHP Manual

Phillip- I know this is not a free coding forum. That is not what I am seeking. I should have posted the pseudocode I had last night but I was getting very upset with it. I thought I could explain what I was struggling with just in words. I can't.

So below is my current pseudocode with the part I am struggling with questioning pointed out. Thanks for your help. The part I am specifically struggling with is how to get both the current and the previous user input saved( defined) into the program and how to have them update continuously throughout the loop. I hope this makes a little more sense now. If not I can try to explain again. I've never been good at explaining things even when I understand them. lol

import.java.util.*;
public static void main(String[] args){

int count = 0
set up new scaner

while (count<10)

count++

get user input
(this below is the part I'm not sure is translatable to java, if it is how would i accomplish this)
int num1 = previous user entered value
int num2 = current user entered value

if num1< num2
print num1 "is less than" num2
end if
if num1>num2
print num1 "is greater than" num2
end if
if num1=num2
print num1 "is equal to" num2
end if
end while
}
Thanks

Last edited by uswhovian; 03-09-2013 at 05:04 PM.. Reason: space in int declaration

 03-09-2013, 05:49 PM PM User | #4 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,478 Thanks: 200 Thanked 2,470 Times in 2,448 Posts Well, to start with This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum. And use [CODE] tags. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
03-09-2013, 06:09 PM   PM User | #5
uswhovian
New to the CF scene

Join Date: Mar 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Philip M Well, to start with This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum. And use [CODE] tags.
ah. thanks for the clarification phillip. i will get that fixed

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:28 PM.