help with java code make sure you.?
Problem by keren875: help with java code be sure to.?
Can someone inform me what the issue with this code is?
When i operate it, after the user puts in the electricity, the output is each can not operate and enough energy. It should only print 1 of them… i think there is a dilemma with my if/elseif conditioning…. aid?
System.out.println (“Form the Energy you have on that technique:”)
electrical power = sc.nextInt ()
if (electrical power < 0)
System.out.println ("Invalid Entry")
System.exit(0)
else if (apps == 1)
if (system.equals ("Windows XP"))
if (power < 300)
System.out.println ("Can Not Run")
else if (system.equals("Vista"))
if (power < 800)
System.out.println ("Can Not Run")
else if (system.equals ("Windows 7"))
if (power < 500)
System.out.println ("Can Not Run")
if (apps == 2)
if (system.equals ("Windows XP"))
if (power < 500)
System.out.println ("Can Not Run")
else if (system.equals ("Vista"))
if (power < 1000)
System.out.println ("Can Not Run")
else if (system.equals("Windows 7"))
if (power < 800)
System.out.println ("Can Not Run")
if (apps> two)
if (technique.equals(“Windows XP”))
if (power < (500+(apps*50)))
System.out.println ("Can Not Run")
if (system.equals("Vista"))
if (power < (1000+(apps*50)))
System.out.println ("Can Not Run")
if (system.equals ("Windows 7"))
if (power < (800+(apps*50)))
System.out.println ("Can Not Run")
System.out.println ("Enough Power")
System.exit(0)
}}
Best answer:
Solution by MichaelInScarborough
It is really hard to examine your code:
attempt the adhering to:
Method.out.println (“Sort the Energy you have on that system:”)
energy = sc.nextInt ()
if (energy < 0)
System.out.println ("Invalid Entry")
System.exit(0)
if (apps < 1)
System.out.println ("Invalid Entry")
System.exit(0)
String strPrompt[] = new String[]
"Windows XP",
"Windows 7",
"Vista",
"Can Not Run"
switch(apps)
case 1:
if (system.equals (strPrompt[0]) && (power < 300))
System.out.println(strPrompt[3])
else if (system.equals(strPrompt[1]) && (power < 800))
System.out.println(strPrompt[3])
else if (system.equals(strPrompt[2]) && (power < 500))
System.out.println(strPrompt[3])
else
System.out.println ("Invalid Entry")
break
case 2:
if (system.equals (strPrompt[0]) && (power < 500))
System.out.println(strPrompt[3])
else if (system.equals(strPrompt[1]) && (power < 1000))
System.out.println(strPrompt[3])
else if (system.equals(strPrompt[2]) && (power < 800))
System.out.println(strPrompt[3])
else
System.out.println ("Invalid Entry")
break
default:
if (system.equals (strPrompt[0]) && (power < (500 + apps * 50)))
System.out.println(strPrompt[3])
else if (system.equals (strPrompt[0]) && (power < (1000 + apps * 50)))
System.out.println(strPrompt[3])
else if (system.equals (strPrompt[0]) && (power < (800 + apps * 50)))
System.out.println(strPrompt[3])
else
System.out.println ("Invalid Entry")
break
In this way, you find your error far easier.
You could go one step further and pack those three statements for xp, vista, and windows 7 into a function. Then you just had 3 function calls which would set up the logic of your program. A lot of code and options to do mistakes would be rationalized away.
I hope this was helpful.
What do you believe? Answer underneath!

