Solved

SIMPLE ROUTINE

Posted on 1998-02-05
2
207 Views
Last Modified: 2010-04-16
I have written a simple java program which does not appear to work, can anybody help...

class cat {
  public static void main(String args[])
     {
     char a,b;
     int d,e;
     d =(int)(Math.random()*3+65);
     e =(int)(Math.random()*3+65);
     a=(char)(d);
     b=(char)(e);
     System.out.println(a);
     System.out.println(b);
     if ((a=='c') && (b=='a'))
     System.out.println("OK");
     else
     System.out.println("NOT OK");
     }
  }

This generates two random characters and then prints a message accordingly, however if the characters are 'c' and 'a' then the display does not show the 'OK' message as expected.

Any suggestions.
0
Comment
Question by:RICHARDH
2 Comments
 
LVL 4

Accepted Solution

by:
russgold earned 20 total points
ID: 1232886
Your code generates upper case letters, but you are testing for lower case letters.

Instead of:

if ((a=='c') && (b=='a'))

you need:

if ((a=='C') && (b=='A'))
0
 

Author Comment

by:RICHARDH
ID: 1232887
Thanks, it was so obvious.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
recursion example 16 124
Java Timer (static) 9 43
nextBoolean(double p) for Random class 3 40
Running JavaFX on JDeveloper 12C 1 31
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question