Jump to content

Need help setting up Java comparison

Go to solution Solved by Mateyyy,

I've not personally worked with Java so for all I know I could be wrong, but isn't "luku == 0-49" incorrect in this context? 

For example if you change "if (luku == 0-49)" to "if (luku >= 0 && luku <= 49)", does it work as intended? 

image.png.7952e7cad078a30eb852e5c64f81ddad.png

 

Hey! I just started coding and need a bit of help. I need to make a program that reports the course grade according to the given table: image.png.55498c8ea3851b04d13a7ce533d56ca9.png 

 

So let me clear some things up.

luku = number

arvosana =grade

mahdotonta = impossible

hylätty = failed

uskomatonta = incredible

anna pisteet = give points

 

But everytime i try to run this program, the program doesnt print anything it is just blank: image.png.529bea19baf8e37ae8bdf9020bd5ec5c.png

 

so first i made the program to say: give pints between 0-100

then i made the program say different things depending of the points i give. but when i give anything under 0 it will work but otherwise it wont. I need little bit help.

I Use my knowledge as business owner and self taught technician aswell as an AI to help people. AI might be controversial but it actually works pretty well 90% of the time.

Link to comment
https://linustechtips.com/topic/1210636-need-help-setting-up-java-comparison/
Share on other sites

Link to post
Share on other sites

I've not personally worked with Java so for all I know I could be wrong, but isn't "luku == 0-49" incorrect in this context? 

For example if you change "if (luku == 0-49)" to "if (luku >= 0 && luku <= 49)", does it work as intended? 

Desktop: Intel Core i9-9900K | ASUS Strix Z390-F | G.Skill Trident Z Neo 2x16GB 3200MHz CL14 | EVGA GeForce RTX 2070 SUPER XC Ultra | Corsair RM650x | Fractal Design Define R6

Laptop: 2018 Apple MacBook Pro 13"  --  i5-8259U | 8GB LPDDR3 | 512GB NVMe

Peripherals: Leopold FC660C w/ Topre Silent 45g | Logitech MX Master 3 & Razer Basilisk X HyperSpeed | HIFIMAN HE400se & iFi ZEN DAC | Audio-Technica AT2020USB+

Display: Gigabyte G34WQC

Link to post
Share on other sites

Your problem is you aren't comparing to the correct things, if I were to guess I'd say this would work correctly for numbers under 0 and over 100, but fails for all the others.

 

The code you have written is being interpreted as such:

else if (luku == (50 - 59)) { ...

Currently you are testing if luku is equal to the beginning of the range minus the end of the range. In other words, they're all testing whether luku is equal to -9 (other than the first one, which is looking for -49).

 

Instead, you need to test if luku is greater than or equal to the lowest number in the range, and less than or equal to the highest number.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to post
Share on other sites

9 minutes ago, Mateyyy said:

I've not personally worked with Java so for all I know I could be wrong, but isn't "luku == 0-49" incorrect in this context? 

For example if you change "if (luku == 0-49)" to "if (luku >= 0 && luku <= 49)", does anything change? 

thank you! it solved it.

I Use my knowledge as business owner and self taught technician aswell as an AI to help people. AI might be controversial but it actually works pretty well 90% of the time.

Link to post
Share on other sites

You could simplify the comparison a bit by filtering out lower numbers early on.

if (luku < 0) {
    Arvosana: mahdotonta!

} else if (luku <= 49) {
    Arvosana: hylätty

} else if (luku <= 59) {
    Arvosana: 1

} else if (…) {
   

} else {
    Arvosana: uskomatonta!

}

This way you only need to compare to a single number each time and the final else will take care of the highest numbers. Should make things a bit easier to read.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×