Java coding help
Hey I'm making a program that outputs a beginning and ending number by 2 I'm just about done but I'm missing a line or I forgot somthing could anyone look at my code and check me? Eample if you type in 1 for the beginning and 10 for the ending 2 4 6 8 should be the output anyone can help I'm stuck.
import javax.swing.*;
public class DivisibleByTwo {
public static void main (String[] args) {
int input;
int end;
int count;
input = Integer.parseInt(JOptionPane.showInputDialog
(null,"Enter starting integer"));
end = Integer.parseInt(JOptionPane.showInputDialog
(null,"Enter ending integer"));
if (input < end){
count = input;
input = end;
end = count;
}
System.out.printf("The integers divisible by 2 in the rage");
while (input < end); {
if(input/ 2 * 2 == input) {
System.out.println("\tinput = " + input + "\tinput++ = ");
}
input = input + 1;
{
}}
} }
Message was edited by:
thehurricane
So you need someone to read your mind as to what the alleged problem is, and magically fix it for you to your alleged satisfaction?Try again, thinking about what it might be like on the other end of the communication pipe.
Why don't you just tell us your problem? "I'm stuck" is hopelessly lame.
Does it compile? No? Tell us about the error messages.
Does it crash when you run it? Yes? Tell us about the error messages.
Does it not do what you want it to do? Then tell us what you want it to do and what it actually does.
That's your work, not ours.
>}}> }On top of what was said above when posting code highlight it and then press that code button, as the above is an example of something that I won't try even look at.
NO I ROCKED YOU LIKE A HURRICANE
import javax.swing.*;
public class ByTwo{
public static void main(String[] args){
new ByTwo();
}
public ByTwo(){
int start;
int stop;
start = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter starting integer"));
stop = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter ending integer"));
System.out.println("The Integers Divisible By 2 In The Range " + start + " to " + stop + ":");
for(int i = ((start % 2 == 0) ? start : (start + 1)); i <= stop; i += 2){
System.out.println("" + i);
}
}
}
for(int i = ((start % 2 == 0) ? start : (start + 1)); i <= stop; i += 2)
I would love to see a teacher's reaction if this code was handed in. At first they would look at it in shock, wondering if it would work, then either a) curse the student for writing such unmaintainable code or b) wonder who wrote the code for him.
my problem is that when I put in 2 numbers like 10 and 50 my code doesnt output the numbers inbetween 10 and 50 by 2 like its suppose to any idea's what I'm missing to loop my code so that I can make it output numbers by 2
> curse the student for writing such unmaintainable
> code or b) wonder who wrote the code for him.
not everything in these forums needs to be a cockfest about who
can write the most "maintanable" code.
its not my f'n homework.
im going for snappiness. : )
> my problem is that when I put in 2 numbers like 10
> and 50 my code doesnt output the numbers inbetween 10
> and 50 by 2 like its suppose to any idea's what I'm
> missing to loop my code so that I can make it output
> numbers by 2
The problem is, i tried to decipher your code but this:
if (input < end){
count = input;
input = end;
end = count;
}
gave me a headache and i quit.
It might help if you told us what process youre using in english and
we could help you code it.
> not everything in these forums needs to be a cockfest
> about who can write the most "maintanable" code.
> its not my f'n homework.
>
> im going for snappiness. : )
I could care less if it's maintainable. In fact I'm glad you came up with this way. It gives him something to look at that he can't just hand in. He either has to learn how yours works, or code it himself, either way I love it. If I had the time and the inclination I would do that more often for all the people begging for homework to be done.
> my problem is that when I put in 2 numbers like 10
> and 50 my code doesnt output the numbers inbetween 10
> and 50 by 2 like its suppose to any idea's what I'm
> missing to loop my code so that I can make it output
> numbers by 2
Well for starters, what do you suppose the semicolon means in this line:
> while (input < end); {
> I could care less if it's maintainable. In fact I'm
> glad you came up with this way. It gives him
> something to look at that he can't just hand in.
Ohhh, lol. Sorry i misunderstood.
People can get a little pedantic and critical here sometimes.
The way I see it... if im not getting paid - then its all freestyle, lol.
Ok so I screwed up on a semi colon all I really need to know is using this code right here is their any way to loop the beginning number = input and the ending number =end in a loop that will make all the numbers in the middle come out divisible by 2
so if I enter 10 for input and 20 for end how can I make every number in between thoses 2 numbers pop up on the command prompt by 2 like this
4
6
8
and I know my code is alittle messed up somone give the rookie a break would ya
This is snappy, too:for(int i = start + Math.abs(start % 2); i <= stop; i += 2)
??Did you even look at my code?for(int i = start; i <= end; i += 2)
Dr you son of a...Shouldnt you be hitting them corners in them low lows? http://sites.gizoogle.com/index2.php?url=http%3A%2F%2Fforum.java.sun.com%2Fthread.jspa%3FthreadID%3D5150795%26start%3D15%26tstart%3D0
Yes I looked at your code but I have no idea where your getting int i from are you saying I should add that to my code and do I need to use a for statement to make it output
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
Some people really do need to be spoonfed. If you are trying to print out every second number then why in hell are you increasing your value by 1?loop i <= j {print iincrease i by 2}
import javax.swing.*;
public class DivisibleByTwo {
public static void main (String[] args) {
int input;
int end;
int count;
int i;
input = Integer.parseInt(JOptionPane.showInputDialog
(null,"Enter starting integer"));
end = Integer.parseInt(JOptionPane.showInputDialog
(null,"Enter ending integer"));
if (input < end){
count = input;
input = end;
end = count;
}
System.out.printf("The integers divisible by 2 in the rage");
while (input < end); {
if(input/ 2 * 2 == input) {
for(input = 2; end > input; input++) {
System.out.println("\tinput = " + input );
}
input = input + 1;
{
}}
} } }
Ok I added a for statement in and it outputs numbers does anyone know how I can make theses numbers go by 2
> dded a for statement in and it outputs numbers does> anyone know how I can make theses numbers go by 2you're dead to me.
> input = input + 1;> {> > }}> } } } I just threw up in my mouth a little.
> > input = input + 1;> > {> > > > }}> > } } } > > I just threw up in my mouth a little.are we being Punk'd?
I think the code is incomplete.
{
// I love pointless curly brackets and refuse to listen to advice given to me.
}}
} } }
it is a free forum, that doesn't mean one has to be rude.
> it is a free forum, that doesn't mean one has to be> rude.It doesn't mean he should be stupid either, but...
It's nice of everyone to help the OP write a more clear for-loop, but does anyone actually understanf what the program is supposed to do?"Hey I'm making a program that outputs a beginning and ending number by 2"?
> It's nice of everyone to help the OP write a more
> clear for-loop
You mean write a for loop in general?
> but does anyone actually understanf
> what the program is supposed to do?
Yes.
@thehurricane
REEEEEEAAAAAAAAD. You have been given solutions and are just neglecting them or twisting them.
This reminds me of... http://forum.java.sun.com/thread.jspa?threadID=5150687&start=0&tstart=0