Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31439
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Simple if-exercises |
| Date | Thu, 02 Apr 2015 08:58:17 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 40 |
| Message-ID | <mfjebm$i5s$1@dont-email.me> (permalink) |
| References | <if-20150402020010@ram.dialup.fu-berlin.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Thu, 2 Apr 2015 12:57:26 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="18620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DuJ/x9UBHWPZDAg++8OtZ" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 |
| In-Reply-To | <if-20150402020010@ram.dialup.fu-berlin.de> |
| Cancel-Lock | sha1:6tpVGJkY9Ja6bKGq4CwwNlloc1k= |
| Xref | csiph.com comp.lang.java.programmer:31439 |
Show key headers only | View raw
On 4/1/2015 8:10 PM, Stefan Ram wrote:
> I was looking for very simple if-exercises.
>
> I usually found exercises like this:
>
> »Print "positive" if x > 0, "non-positive", otherwise.«
>
> The expected answer would be
>
> if( x > 0 )
> System.out.println( "positive" );
> else
> System.out.println( "non-positive" );
>
> However, I already have taught »?:«, so the better
> (shorter) solution would be:
>
> System.out.println( ( x > 0 ? "" : "non-" )+ "positive" );
// "Better" is in the eye of the beholder:
System.out.println(x > 0 ? "positive" : "non-positive");
> . Do you know a very simple if-exercise whose solution
> cannot be written more tersely with the ternary operator
> or where the formulation with »if« is clearly better than
> with the ternary operator?
Anything requiring more than one "action" on at least one
branch of the test would suffice. Or, anything involving more
than one test. Perhaps
Given x,y from "Scissors", "Paper", "Stone", print "Scissors
cut Paper" or "Paper covers Stone" or "Stone breaks Scissors",
as appropriate.
would suit.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread
Re: Simple if-exercises Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-02 08:58 -0400
csiph-web