Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Program works, but not exactly the way it is 'supposed' to
New Posts  All Forums:Forum Nav:

Program works, but not exactly the way it is 'supposed' to

post #1 of 6
Thread Starter 
My program works almost just fine, I just need help with my if and else if expressions. a



Code:

My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #2 of 6
Are you forced to use if-else for this exercise? If not you should probably consider writing a switch block instead...
Psylem Seeker
(23 items)
 
  
CPUMotherboardGraphicsRAM
i5-760 @ 4.0 Ghz / 1.28v Asus P7P55D-E Premium Powercolor HD7870 Corsair Vengeance LP 1600Mhz (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Seagate Barracuda 500GB Toshiba 2.5in External 500GB Toshiba 1TB StorE Alu2 External Western Digital WD Blue 1TB 
Hard DriveOptical DriveCoolingOS
Western Digital WD Blue 1TB External DVD Writer Thermalright Silver Arrow OpenSUSE Leap 42.1 
OSMonitorKeyboardPower
Windows 7 Ultimate x64 SyncMaster P2350 @ 1920x1080 (Dead: Using 15in ... Microsoft All-In-One + Cheapie Logitech kb Seasonic M12II 750 
CaseMouseAudioAudio
Coolermaster 690-II Advanced Cheapie Logitech Ozaki WoW WR580 2.2ch Beyerdynamic DT770 Pro 
OtherOtherOther
UMVC3 Hori Fight Stick Launchpad S + Launch Control Focusrite Scarlett 2i4 (incoming) 
  hide details  
Reply
Psylem Seeker
(23 items)
 
  
CPUMotherboardGraphicsRAM
i5-760 @ 4.0 Ghz / 1.28v Asus P7P55D-E Premium Powercolor HD7870 Corsair Vengeance LP 1600Mhz (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Seagate Barracuda 500GB Toshiba 2.5in External 500GB Toshiba 1TB StorE Alu2 External Western Digital WD Blue 1TB 
Hard DriveOptical DriveCoolingOS
Western Digital WD Blue 1TB External DVD Writer Thermalright Silver Arrow OpenSUSE Leap 42.1 
OSMonitorKeyboardPower
Windows 7 Ultimate x64 SyncMaster P2350 @ 1920x1080 (Dead: Using 15in ... Microsoft All-In-One + Cheapie Logitech kb Seasonic M12II 750 
CaseMouseAudioAudio
Coolermaster 690-II Advanced Cheapie Logitech Ozaki WoW WR580 2.2ch Beyerdynamic DT770 Pro 
OtherOtherOther
UMVC3 Hori Fight Stick Launchpad S + Launch Control Focusrite Scarlett 2i4 (incoming) 
  hide details  
Reply
post #3 of 6
Thread Starter 
No idea what a switch block is :/ this is a 'intro' course but this project is a pain
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #4 of 6
http://www.homeandlearn.co.uk/java/java_switch_statements.html

the tl;dr version: Using switch is a cleaner way of writing long winded if-else if-else blocks.

Personally i'm not a fan of else-if's with comparison operators because they sometimes have a tendency of not doing what you want them to do, and i prefer writing things out the long way. Like this:
Code:
if (guess > (secretNumber + 30)) {
  print("Way Too High"); 
}

if (guess > (secretNumber + 10) && (guess < (secretNumber + 30))) {
  print("High");
}

if (guess > (secretNumber + 1) && guess < (secretNumber + 10)) {
  print("A Little High");
}

if (guess < (secretNumber - 30)) {
  print("Way Too Low");
}

if (guess < (secretNumber - 10) && (guess > (secretNumber - 30))) {
  print("Low");
}

if (guess < (secretNumber - 1) && guess > (secretNumber - 10)) {
  print("A Little Low");
}

if (guess == secretNumber) {
  print("Correct");
}
Psylem Seeker
(23 items)
 
  
CPUMotherboardGraphicsRAM
i5-760 @ 4.0 Ghz / 1.28v Asus P7P55D-E Premium Powercolor HD7870 Corsair Vengeance LP 1600Mhz (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Seagate Barracuda 500GB Toshiba 2.5in External 500GB Toshiba 1TB StorE Alu2 External Western Digital WD Blue 1TB 
Hard DriveOptical DriveCoolingOS
Western Digital WD Blue 1TB External DVD Writer Thermalright Silver Arrow OpenSUSE Leap 42.1 
OSMonitorKeyboardPower
Windows 7 Ultimate x64 SyncMaster P2350 @ 1920x1080 (Dead: Using 15in ... Microsoft All-In-One + Cheapie Logitech kb Seasonic M12II 750 
CaseMouseAudioAudio
Coolermaster 690-II Advanced Cheapie Logitech Ozaki WoW WR580 2.2ch Beyerdynamic DT770 Pro 
OtherOtherOther
UMVC3 Hori Fight Stick Launchpad S + Launch Control Focusrite Scarlett 2i4 (incoming) 
  hide details  
Reply
Psylem Seeker
(23 items)
 
  
CPUMotherboardGraphicsRAM
i5-760 @ 4.0 Ghz / 1.28v Asus P7P55D-E Premium Powercolor HD7870 Corsair Vengeance LP 1600Mhz (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Seagate Barracuda 500GB Toshiba 2.5in External 500GB Toshiba 1TB StorE Alu2 External Western Digital WD Blue 1TB 
Hard DriveOptical DriveCoolingOS
Western Digital WD Blue 1TB External DVD Writer Thermalright Silver Arrow OpenSUSE Leap 42.1 
OSMonitorKeyboardPower
Windows 7 Ultimate x64 SyncMaster P2350 @ 1920x1080 (Dead: Using 15in ... Microsoft All-In-One + Cheapie Logitech kb Seasonic M12II 750 
CaseMouseAudioAudio
Coolermaster 690-II Advanced Cheapie Logitech Ozaki WoW WR580 2.2ch Beyerdynamic DT770 Pro 
OtherOtherOther
UMVC3 Hori Fight Stick Launchpad S + Launch Control Focusrite Scarlett 2i4 (incoming) 
  hide details  
Reply
post #5 of 6
You didn't provide enough information to help with this. You didn't really ask a question. You need to state why the if-else ifs aren't working correctly. What is it that is going wrong. It is like taking you car to a mechanic and saying fix it.

In both code examples above the cases of 30, 10, and 1 will not be captured in the results.That is one problem, and that is all I will point out until you say what is going wrong with your code.

Good Luck.
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #6 of 6
Thread Starter 
I switched some signs around and fixed it.
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Program works, but not exactly the way it is 'supposed' to