Tuesday, October 16, 2007

What choices did you face in developing your solution, and how did you choose among them?

Today, I learned howto get input from the user in java. I also learned about the if, else if, and else decision structures. The way we use these structures may affect the way our program is going to turn out. if is used when there is a specification for the result expected. else if is used to do the same thing, but it avoids any specifications made previously, and else is used to include everything except things mentioned above. Look ate the following example for reference:

import javax.swing.*;
public class InputPractice {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int x;
String s;
s = JOptionPane.showInputDialog("What did you get on your test?");
x = Integer.valueOf(s);
if (x >= 90)
{
System.out.println("That's an A! You did an Excellent job!");
}
else if (x >= 85)
{
System.out.println("That's a high B! Very Good job!");
}
else if (x >= 85)
{
System.out.println("That's a B! Good job!");
}
else if (x >= 70)
{
System.out.println("That's a C! That is an OK score. Try to do better next time.");
}
else
{
System.out.println("You should study much harder next time.");
}
}
}


The places where else, else if, and if were used should be chosen wisely.

No comments: