minor for loop help

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

minor for loop help

Post by dawodus on Sat Oct 13, 2012 1:27 pm
([msg=70114]see minor for loop help[/msg])

hello guys i need a little bit of help. i have a java program that is suppose to be an assignment on learning for loops, this is my coding iv done (below) , it is suppose to accumulate 4 integers and add them together and if the value = below 34 it gives a certain message and vice versa if above 34 it gives another message. unfortunetly when i run the program it doesnt give me the correct accumulated value any help would be great.

import javax.swing.JOptionPane;


public class assessment4 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String disabilityclass;
int legality = 0;


for (int i = 1; i <=4; i++)
{
disabilityclass= JOptionPane.showInputDialog("What is the disability class of swimmer on leg " + i +"?");
legality= Integer.parseInt(disabilityclass);
legality = legality + i;
}


if (legality <= (34))
{
JOptionPane.showMessageDialog(null, "That is a legal team with " + legality + " point");
}
else
JOptionPane.showMessageDialog(null, "That is a illegal team with " + legality + " point");
}
}

-- Sat Oct 13, 2012 8:29 pm --

found the problem myself guys i simply needed to change
legality+= Integer.parseInt(disabilityclass);
dawodus
New User
New User
 
Posts: 8
Joined: Sat Oct 13, 2012 1:22 pm
Blog: View Blog (0)


Re: minor for loop help

Post by weekend hacker on Sat Oct 13, 2012 4:46 pm
([msg=70117]see Re: minor for loop help[/msg])

change
Code: Select all
legality= Integer.parseInt(disabilityclass);
legality = legality + i;

to
Code: Select all
legality+= Integer.parseInt(disabilityclass);


the for () part handles the counting, compairing, increasing to handle your loop, you don't touch the i unless if you're doing something special(read: wrong). what you need to be doing was taking the 4 inputs and adding them together(the loop does things 4 times, so you ask things 4 times and with the above code it ads it to whatever was in legality)
<Yoda> if someone says something i don't like, i ban him, ban whoever defends him, and then ban the witnesses...
User avatar
weekend hacker
Administrator
Administrator
 
Posts: 191
Joined: Sun Apr 13, 2008 2:39 pm
Location: 127.0.0.1
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests