posted
If you remember from a few months back I decided to take a Java course this year, I learned how to do some basic counting programs but I was wondering how I might go about doing a few things.
Like is there an easy way to count by twos or threes?
Also is there an easy way to skip over numbers, like no number 42 or anythign divisible by eight?
Finally is there any websites that work as a good source for simple questions.
Posts: 549 | Registered: Feb 2008
| IP: Logged |
for (int i = 2; i <= 1000; i+=2){ if ((i != 42) && (i % 8 != 0)){ System.out.println(i); } }
This would print 2, 4, 6, ..., 996, 998. Would skip 42 and anything by 8.
You'd need to know the basics of for loops, the += operator, if statements, and the % (mod) operator to understand this fully. You'd also need to put it in the main method of a program.
Edit: As for websites, I'm happy to answer any Java Q's, so feel free to bump this thread. Big Moose Saloon is a site for Java questions if you need something, though I don't know how good they are at basic questions.
Posts: 5656 | Registered: Oct 1999
| IP: Logged |
quote:Originally posted by TomDavidson: Surprisingly, the answer to all your questions is "yes."
Even better it was in two lines of code! Haha.
I didn't know about how ! works but I think that will help me solve a bunch of those little things I have been wondering about.
Posts: 549 | Registered: Feb 2008
| IP: Logged |
posted
The exclamation point (!) is a common way of saying "NOT" in computer code. Thus, != means "NOT EQUAL".
Posts: 1813 | Registered: Apr 2001
| IP: Logged |
posted
It might be useful to understand that the ! isn't a separate operator, here. Instead != is the "not equals" operator.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
So guess who has spent the past three hours fiddleing!
So anyway I am trying to now make a thing that tells me even numbers skips 8's and goes up to 100. but then asks me if i want to continue and get more numbers.
The 2 & 8 thing isn't really important, i just want to get the other part down. I have taken two tries at it.
code:
for (int i = 2; i <= 100; i+=2){ if (i % 8 != 0){ System.out.println(i);
try { System.out.print("Would you like more numbers? yes/no"); boolean numbers; String Continue; do{ System.out.println("Play again?"); Continue = in.nextLine(); if(Continue.equals("yes")){ numbers = true; } if(Continue.equals("no")){ numbers=false; } }while(numbers==true);
posted
Here's a suggestion: instead of writing code like this, first write what you want to happen in steps as comments. Then, replace the comments with code, and make sure you understand exactly what each piece of code is doing.
So, for instance, you might have written for the first questions:
code:
//go through numbers from 2 to 100 by 2s //if the number is divisible by eight or is 42, skip it //print all other numbers
The reason I say this is it seems you have a pretty good idea what to do . . . except you're getting how it makes sense to put the bits together all mixed up in your head.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Xann, you seem to have quite a few compile errors in your code snippets.
Like fugu, I'd strongly suggest starting small and working your way up to what you are trying to do. Get each step to work before trying to complicate it.
Here I've fixed the compile errors in your first program:
code:
Scanner in = new Scanner(System.in); for (int i = 2; i <= 100; i += 2) { if (i % 8 != 0) { System.out.println(i); } }
System.out.print("Would you like more numbers? yes/no"); boolean numbers = false; String Continue; do { System.out.println("Play again?"); Continue = in.nextLine(); if (Continue.equals("yes")) { numbers = true; } if (Continue.equals("no")) { numbers = false; } } while (numbers == true);
The things I needed to do:
1) Add a closing curly brace to your if and for blocks. Any left brace will need a right brace in order for your program to compile.
2) Removed the "try". Without a "catch" or "finally" this is meaningless. I'd save learning about exceptions and how they are caught until you get a little further into your studies.
2) Created an "in" variable to reference. Tje in.readLine() will break unless the compiler knows what "in" refers to. I was assuming by the way you were using it that this was a reference to the java.util.Scanner class. Make sure to import this class if you were using it.
3) Initialized the "numbers" variable to false, since it needs a guaranteed initial value in order for it to be compared in the do/while block.
Now it should be able to run. Notice, however, that it doesn't do what you wanted it to do. The input/output will look something like:
code:
Would you like more numbers? yes/noPlay again? yes Play again? yes Play again? yes Play again? yes Play again? yes
If the (numbers == true) evaluates to true, it won't go back to the very beginning, it will just go back to the start of the "do" block.
You could move the for loop down to be inside of the do block, but then it will start back at 2. Will let you play with it now to get it how you want it.
A couple of style things to advise, so you don't develop bad habits:
1) Don't capitalize your methods or variables in Java. This convention is so you can tell what's a variable and what's a class. In your program, it looks like you capitalized "Continue" because "continue" is a reserved word in Java. I'd advise calling it "continueInput" or just "input".
2) In your do/while loop, the condition you use is while (numbers == true). Since "numbers" is of type boolean, you could just as easily write this as while (numbers). Comparing a boolean value to true or false is something every beginner does, but you'll want to get out of the habit eventually.
posted
Yea my program was in shambles haha, I think with the help I can get it running to what I want. I capitalize on basic instinct now, but i am sure ill stop soon enough.
Also my lack of organization seems to be my biggest handicap so far, but this is my third week of playing around so I figure I have time to improve.
Posts: 549 | Registered: Feb 2008
| IP: Logged |
posted
Ah, depression strikes. We had a pop quiz in my class today to make a program that takes the first how ever many odd numbers then adds them up and returns the sum. Also the number must be able to be imputted by the user.
I felt proud to do make organized presice and working program.
code:
import java.util.Scanner; public class SumofOdds { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); System.out.print("How many numbers would you like to add? "); int number; int Continue; Continue = in.nextInt(); number = Continue; System.out.println("The sum is " + number * number); } }
After I was told that I "didn't add them up" but instead used a mathmatical shortcut. Bah. I feel ripped off.
quote: Write a program that prints the sum of first N odd numbers. The value of N is obtained from the user as an iuput.
For example, if the user gives "10" as an input, the program prints the sum of first 10 odd numbers.
thats the exact assignment, I feel like I should not be given points off. I did what it told me.
Posts: 549 | Registered: Feb 2008
| IP: Logged |
posted
I would think the language "sum of first 10 odd numbers" would have been a clue. True, the instructions weren't explicit, but you did learn about loops and stuff, right? I mean, your previous homework assignments seem to have made extensive use of them. Quizzes like these are supposed to test your knowledge of language syntax, not your cleverness with mathematical concepts.
Posts: 339 | Registered: Apr 2008
| IP: Logged |
posted
I'd have taken points off too. The assignment is (edit: not) completely explicit, but it's pretty strongly implied that you have to sum the numbers, not rely on a mathematical trick.
That's sort of besides the point though. If you are taking this class to try to learn how to program, you should be focusing on learning the concepts.
No one cares about getting the sum of the first n odd integers. Being able to do that is pretty much meaningless. But properly setting up a loop, using conditional statements, and keeping a running total are all important basic concepts that are essential for knowing how to program.
---
As an aside, in Java - and most other languages, really - the style is usually to have class names start with capital letters and variable names start with lower-case. You certainly don't have to do it this way, but it's a near universal convention and it does actually have beneficial effects, so I'd recommend it. (I'm talking about the Continue variable you're using.)
quote:As an aside, in Java - and most other languages, really - the style is usually to have class names start with capital letters and variable names start with lower-case. You certainly don't have to do it this way, but it's a near universal convention and it does actually have beneficial effects, so I'd recommend it. (I'm talking about the Continue variable you're using.)
In addition, it's a very bad habit to use variable names that are the same as language key words except for the capitalization. I assume the reason that you capitalized Continue was because 'continue' is a key word, and you couldn't use it that way. The solution is to use a different word, not to capitalize it.
This particular program doesn't make it all that confusing, but you will generally obfuscate your code if you use variable names that are too similar to key words.
Posts: 4287 | Registered: Mar 2005
| IP: Logged |
posted
Oh yeah, I didn't even think of that. I was wondering why he cap'd the one and not the other.
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
quote:Oh yeah, I didn't even think of that. I was wondering why he cap'd the one and not the other.
quote:1) Don't capitalize your methods or variables in Java. This convention is so you can tell what's a variable and what's a class. In your program, it looks like you capitalized "Continue" because "continue" is a reserved word in Java. I'd advise calling it "continueInput" or just "input".
I'm glad at least Xann is reading my posts .
Posts: 5656 | Registered: Oct 1999
| IP: Logged |
posted
Oops. I was just looking at the latest thing, because it looked like his earlier stuff was taken care of. Mea culpa.
Posts: 10177 | Registered: Apr 2001
| IP: Logged |
posted
FWIW, Xavier, I knew you had already pointed it out, but since Xann posted a new program with the same problem, I just tried pointing it out again. I should have put it like "As Xavier mentioned earlier..."
Posts: 4287 | Registered: Mar 2005
| IP: Logged |