Hi...
I am doing this simple Java program which displays a particular grade (A, B, C...) according to the mark entered.
However I cannot arrange it in such a way that it displays "Invalid" if the user eners a grade > 100... take a look... - System.out.print ("Enter mark: ");
-
int mark = Keyboard.readInt();
-
char grade =' ';
-
-
-
if (mark > 100) (System.out.println ("Invalid Entry"));
-
else if (mark >=80 && mark <= 100) grade = 'A';
-
else if (mark >= 60) grade = 'B';
-
else if (mark >= 55) grade = 'C';
-
else if (mark >= 40) grade = 'D';
-
else if (mark < 40) grade = 'F';
-
-
-
System.out.println ("Grade = " + grade);
after compiled it keeps telling me that this is not a statement: if (mark > 100) (System.out.println ("Invalid Entry"));
can't figure why ..
The program however runs, and if the user enters "101" for exmple, it keeps obeying the second 'else if'....
Any ideas pls....?
thanks
9 8304 @tiktik
It's right, that isn't a statement. Try - if (mark > 100) System.out.println ("Invalid Entry");
or - if (mark > 100) {System.out.println ("Invalid Entry");}
instead.
Greetings,
Nepomuk
Yes that's it thanks a lot...
but now i have another problem...
i would like that if the entry is invalid, it would not display the last line ("Grade =" ), and so i tried to insert the ' break;' but it seems as if it can be used only in loops or switch :( - public static void main(String[] args) {
-
-
System.out.print ("Enter mark: ");
-
int mark = Keyboard.readInt();
-
char grade =' ';
-
-
-
if (mark > 100) System.out.println ("Invalid Entry");
-
break;
-
if (mark >=80 && mark <= 100) grade = 'A';
-
else if (mark >= 60) grade = 'B';
-
else if (mark >= 55) grade = 'C';
-
else if (mark >= 40) grade = 'D';
-
else if (mark < 40) grade = 'F';
-
-
-
System.out.println ("Grade = " + grade);
when I run this program and input an invalid entry, it still displays "Grade = " at the end...
Never mind.... i solved the problem by inserting "System.out.println" after every if statement like this - public static void main(String[] args) {
-
-
System.out.print ("Enter mark: ");
-
int mark = Keyboard.readInt();
-
char grade =' ';
-
-
-
if (mark > 100) System.out.println ("Invalid Entry");
-
else if (mark >=80 && mark <= 100) {grade = 'A';
-
System.out.println ("Grade = " + grade); }
-
-
else if (mark >= 60) {grade = 'B';
-
System.out.println ("Grade = " + grade); }
-
-
else if (mark >= 55) {grade = 'C';
-
System.out.println ("Grade = " + grade); }
-
-
else if (mark >= 40) {grade = 'D';
-
System.out.println ("Grade = " + grade); }
-
-
else if (mark < 40) {grade = 'F';
-
System.out.println ("Grade = " + grade);}
Now the program works as it is supposed... but still I have the question question whether or not the ' break;' could be used elsewhere (not just in loops or switch() )?... It would be very useful I think - especially when one needs to terminate the program if a certain value is entered - one needs something which would get him out of the main() block...
Thanks
This is just programming: if a valid numbers was entered you want to do something, otherwise you don't want to do anything, so: -
if (valid number entered) {
-
do something;
-
}
-
Let's concentrate on the 'valid number entered'; the boolean expression
'number <= 100 && number >= 0' seems to express what you want. Simply plug it into your pseudo code: -
if (number <= 100 && number >= 0) {
-
do something;
-
}
-
You already have solved the 'do something' part so plug it into the pseudo code.
kind regards,
Jos
Yes, thanks for your reply...it works very well with it as well
Any idea about the break; question?
@tiktik
As you have already noticed a 'break' statement can only occur in a loop or in a case statement.
kind regards,
Jos
@tiktik
There's always System.exit(int Status) if you really want to leave your program, but that should be used with caution.
Other than that, you can always check if your grade variable has been assigned yet - setting it to a default value (e.g. 'X') would be one way of doing that. Or you can have a boolean value gradeWasSet. Or you can put all of those things in one nested if. Or I'm sure you can think of something else.
There is something similar to what you mean in many languages: the jump command goto. This command doesn't exist in Java (although there is continue, which is similar in some aspects), as it can make code very ugly very easily. That's also why it's seen as bad style by a great part of the programming world.
Here, I'll even give you a comic about goto:
(from http://xkcd.com/292/)
Greetings,
Nepomuk
Thanks for the detailed explanation.
good comic as well =)
tiktik
You're welcome of course. :-)
Greetings,
Nepomuk
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Don Low |
last post by:
Hi,
I'm studying the python tutorial at python.org. In introducing the
concept of *break* and *continue* Statements, and *else* Clauses on
Loops, there's an example that goes as follows:
...
|
by: Xah Lee |
last post by:
Here's the belated Java solution.
import java.util.List;
import java.util.ArrayList;
import java.lang.Math;
class math {
public static List range(double n) {
return range(1,n,1);
}
|
by: SplaTTer |
last post by:
I have a piece of code consisting of k nested for loops, for example:
int k = 5;
int *p = new int;
double x,y; //x is read from file
for(p=0;p<=6;p++)
for(p=0;p<=3;p++)
for(p=0;p<=3;p++)
|
by: Tcs |
last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't
loaded it yet. I'm still using MS Access. And no, I don't believe this is an
Access question. (But who knows? I...
|
by: nimmi_srivastav |
last post by:
Below you will see an example of a nested conditional expression that
this colleague of mine loves. He claims that it is more efficient that
a multi-level if-else-if structure. Moreover, our...
|
by: Jake Emerson |
last post by:
I'm attempting to build a process that helps me to evaluate the
performance of weather stations. The script below operates on an MS
Access database, brings back some data, and then loops through to...
|
by: Jyotirmoy Bhattacharya |
last post by:
I'm a newcomer to Python. I have just discovered nested list
comprehensions and I need help to understand how the if-clause
interacts with the multiple for-clauses. I have this small program:
...
|
by: koutoo |
last post by:
I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I'm hoping I won't have to...
|
by: Maric Michaud |
last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit :
This is a language limitation.
This is because nested scope is implemented for python function only since 2.3
allow late...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |