Hi there, im using a java program call Blue J and im now learning something new on java, but it quite complicated on my coding and having some problem with it or may be i have screw up with my looping and linking another new java call addIntegers with it, so i need some help from u guys,
1st, enter how many number that the user requires, the number must be positive and only accept alphabetic. If not it will prompt out "You must enter an integer" or "You must enter a positive integer"
2nd, enter the number an integer from 1 to 10, same as the 1st step must be positive and only accept alphabetic.
3rd, it will show message dialog on the number that the user have key in, for example if the number is 2, "the total of integers between 1 and 2 is 3". If it is 3, "the total of integers between 1 and 3 is 6".
Finally ask the user if they want to continue and if so repeat from the start.
below is my main coding,
import javax.swing.JOptionPane;
public class Assessment6b
{
public static void main( String args[] )
{
int number;
int[] range;
int again = 1;
int retry = 0;
int index = 0;
String numStr, rangeStr, redoStr;
while (again==1 && retry==0)
{
try
{
numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter");
number = Integer.parseInt(numStr);
if (number < 1)
{
JOptionPane.showMessageDialog(null, "You must enter a positive integer");
}
while (retry==0 || retry==1)
{
if (number >=1)
{
number++;
range = new int[number];
for (int count = 1; count < number; count++)
{
int store=0;
try
{
rangeStr = JOptionPane.showInputDialog("Enter integer " + count + " in the range 1-10");
store = Integer.parseInt(rangeStr);
if (store < 1 || store > 10)
{
JOptionPane.showMessageDialog(null, "You must enter an integer from 1 to 10");
retry = 1;
}
else
{
range[count] = store;
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "You must enter a integer.");
retry = 1;
}
}
for (int count = 1; count < number; count++)
{
int answer = 0;
answer = addIntegers.integers(range[count]);
JOptionPane.showMessageDialog(null, "The total of Integers between 1 and " + range[count] + " is " + answer);
}
}
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "You must enter a integer.");
}
}
do
{
redoStr = JOptionPane.showInputDialog("Do you want to continue (y/n)?");
}
while (redoStr.equalsIgnoreCase("y"));
}
}
this is the coding to link with my main,
public class addIntegers
{
public static int integers (int num)
{
int result = 0;
for (int counter=1; counter<=num; counter++)
{
result = result + counter;
}
return result;
}
}
hope you can understand my coding, sorry and thanks for the trouble.