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
Re: Using if to print words
Quote:
Originally Posted by
Norm
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
Would you be able to write me an example of such code as I am relatively new to Java and I am unsure how to do what you are saying.
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 String
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.
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:
Code:
int speedUp(int increment) {
speed = speed + increment; // compute the new speed
return speed; // return the new speed
}
Re: Using if to print words
Quote:
Originally Posted by
TANKDS
Could you possibly give me an example as I am not sure how to use this in my situation.
Do you not understand if-statements, or do you not understand how to define a method?
Re: Using if to print words
Quote:
Originally Posted by
Norm
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:
Code:
int speedUp(int increment) {
speed = speed + increment; // compute the new speed
return speed; // return the new speed
}
Why would I need to get a return of speed?
Re: Using if to print words
Quote:
Originally Posted by
TANKDS
Why would I need to get a return of speed?
I think he was just demonstrating how to return a value.
Can you clarify what you don't understand?
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.
Re: Using if to print words
Quote:
Originally Posted by
TANKDS
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.
Then this is what you should read: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Using if to print words
Quote:
it will return closed or open.
What method in the CarDemo class is returning a value? I don't see any methods in the posted code that return a value.
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.html
Re: Using if to print words
I use System.out.println to get a value back in the CarDemo.
Re: Using if to print words
Quote:
to get a value back
It's a vocabulary problem.
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.
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.
Re: Using if to print words
Quote:
Originally Posted by
TANKDS
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.
Ok, so you want to use an if-else statement.
Re: Using if to print words
Quote:
Originally Posted by
awinston
Ok, so you want to use an if-else statement.
I have tried to implement an if else statement but I keep getting a lot of errors, 20 at the moment. I was wondering if you could give me an example of what code I should use where because this seems to not currently be working correctly. :(
Re: Using if to print words
you can use an inline if:
Code:
" Right-Front Door:" + ((RFdoor == 0) ? "Closed" : "Open") +
Re: Using if to print words
What errors are you getting when you try the if-else statement?
Re: Using if to print words
Quote:
Originally Posted by
.paul.
you can use an inline if:
Code:
" Right-Front Door:" + ((RFdoor == 0) ? "Closed" : "Open") +
You Sir, are an absolute genius! Could you briefly explain the code possibly. It worked and did everything that I wanted it to. I just need to know how it works please. :)