Results 1 to 20 of 34
Thread: Using if to print words
- 08-10-2012, 07:32 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Using if to print words
I am trying to make it so that 0 is the equivalent to a closed door and 1 is the equivalent to open. My problem is that I have it set at the moment to print 0 or 1 depending on which Demo I run but I am looking for a way for it to print Closed if the value is 0, and open if the value is 1. Can some help me out please. :D
Java Code:class Car { int gear = 0; int speed = 0; int RFdoor = 0; int LFdoor = 0; int boot = 0; int bonet = 0; void changeGear(int newValue){ gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void openRFdoor(int increment) { RFdoor = RFdoor + increment; } void closeRFdoor(int decrement) { RFdoor = RFdoor - decrement; } void openLFdoor(int increment) { LFdoor = LFdoor + increment; } void closeLFdoor(int decrement) { LFdoor = LFdoor - decrement; } void openBoot(int increment) { boot = boot + increment; } void closeBoot(int decrement) { boot = boot - decrement; } void openBonet(int increment) { bonet = boot + increment; } void closeBonet(int decrement) { bonet = bonet - decrement; } void PrintStates() { System.out.println("Gear:" + gear + " Speed:" + speed + " Right-Front Door:" + RFdoor + " Left-Front Door:" + LFdoor + " Boot:" + boot + " Bonet:" + bonet); } }
- 08-10-2012, 08:30 PM #2
Re: Using if to print words
Write a small method that takes the value as an argument and returns a String: "Closed" if the value is 0, and "open" if the value is 1
If you don't understand my response, don't ignore it, ask a question.
- 08-10-2012, 09:11 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
- 08-10-2012, 09:31 PM #4
Re: Using if to print words
Take a look at the tutorial:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
What part don't you understand:
write a method that returns a String
pass it the value as an argument
test the value of the argument and chose a String to return
return a StringIf you don't understand my response, don't ignore it, ask a question.
- 08-10-2012, 09:57 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Re: Using if to print words
Could you possibly give me an example as I am not sure how to use this in my situation.
- 08-10-2012, 10:06 PM #6
Re: Using if to print words
Do you know what a method is? Have you called a method in any of your code?
The posted code has many methods defined in it. None of them return a value.
Here's how you'd have one return a value:
Java Code:int speedUp(int increment) { speed = speed + increment; // compute the new speed return speed; // return the new speed }If you don't understand my response, don't ignore it, ask a question.
- 08-10-2012, 10:06 PM #7
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-10-2012, 10:33 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
- 08-10-2012, 10:36 PM #9
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-10-2012, 10:48 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Re: Using if to print words
All i want is to implement the if statement so that when I run my CarDemo.class file rather than returning a 0 or 1 it will return closed or open.
- 08-10-2012, 10:50 PM #11
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Using if to print words
Then this is what you should read: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-10-2012, 10:59 PM #12
Re: Using if to print words
What method in the CarDemo class is returning a value? I don't see any methods in the posted code that return a value.it will return closed or open.
Do you mean prints the value?
How much of the tutorial at the link I posted did you read? There is a lot of material there on methods. For example:
http://docs.oracle.com/javase/tutori...turnvalue.htmlIf you don't understand my response, don't ignore it, ask a question.
- 08-10-2012, 11:29 PM #13
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Re: Using if to print words
I use System.out.println to get a value back in the CarDemo.
- 08-10-2012, 11:34 PM #14
Re: Using if to print words
It's a vocabulary problem.to get a value back
The println method does NOT return any value. It writes a String to the output target which is the console.
A method uses the return statement to return a value.If you don't understand my response, don't ignore it, ask a question.
- 08-10-2012, 11:49 PM #15
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Re: Using if to print words
I may have confused you, I use println to show me the value of the gears etc once the Demo had ran. My problem is that Right-Front Door etc are being showed as 0 or 1 and I want to the print to show them as open or closed, I am trying to use the if and else if to help my println show closed and not 0.
- 08-11-2012, 12:10 AM #16
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-11-2012, 12:16 AM #17
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
-
Re: Using if to print words
you can use an inline if:
Java Code:" Right-Front Door:" + ((RFdoor == 0) ? "Closed" : "Open") +
- 08-11-2012, 12:47 AM #19
Re: Using if to print words
What errors are you getting when you try the if-else statement?
- 08-11-2012, 12:53 AM #20
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Similar Threads
-
Javax Print Attribute for Selection Print Range
By rsawatzky in forum AWT / SwingReplies: 0Last Post: 04-26-2012, 12:14 AM -
Need to find a way to print line spacing by words
By technoreject in forum New To JavaReplies: 1Last Post: 07-07-2011, 02:43 PM -
About how to just print last 20 words from a input file
By Holic in forum New To JavaReplies: 3Last Post: 02-13-2011, 08:54 PM -
How to print words in different colours?
By veen in forum New To JavaReplies: 7Last Post: 06-10-2010, 04:54 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


6Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks