While running this program I want to input numbers that have the following rules: (1) no negative numbers (2) smallValue < middleValue < largeValue. If either one of the rules are broken when I input a number into each one, then it will pop up a error message, and make them re-input a value.
The trouble that I am having with my program is that when I test the middle do/while loop. If I set my smallValue = 5, and I also set my middleValue = 5. It will pop the error message up to re-enter a value, but instead of letting me re-enter a new middleValue, it will go on to the next input message for largeValue. But when I test the middleValue against a negative number, then it will prompt the message, and make me reenter a valid number. The same things happen for my largeValue do/while loop.
I figured that I could go ' while (!(middleValue < 0 && middleValue <= smallValue)); ' it will switch my current situation around.
How can I fix my problem? Thanks in advance for peoples input!
public static void main(String args[])
{
String inputSmall, inputMiddle, inputLarge, inputDistance;
int smallValue = 0, middleValue = 0, largeValue = 0, distanceValue = 0;
// Test valid numbers inputed
// No negative numbers
// small < middle < large
// small
do {
inputSmall = JOptionPane.showInputDialog("Small step value?"
;
smallValue = Integer.parseInt(inputSmall);
if (smallValue >= 0) continue;
JOptionPane.showMessageDialog(null, "Re-enter small step value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (smallValue < 0);
// middle
do {
inputMiddle = JOptionPane.showInputDialog("Middle step value?"
;
middleValue = Integer.parseInt(inputMiddle);
if (middleValue >= 0 && middleValue > smallValue) continue;
JOptionPane.showMessageDialog(null, "Re-enter middle step value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (middleValue < 0 && middleValue <= smallValue);
// large
do {
inputLarge = JOptionPane.showInputDialog("Large step value?"
;
largeValue = Integer.parseInt(inputLarge);
if (largeValue >= 0 && largeValue > middleValue && middleValue > smallValue && largeValue > smallValue) continue;
JOptionPane.showMessageDialog(null, "Re-enter large step value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (largeValue < 0 && largeValue <= middleValue && middleValue <= smallValue && largeValue <= smallValue);
// distance
do {
inputDistance = JOptionPane.showInputDialog("Distance value?"
;
distanceValue = Integer.parseInt(inputDistance);
if (distanceValue >= 0 ) continue;
JOptionPane.showMessageDialog(null, "Re-enter distance value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (distanceValue < 0);
// Creating new Robot and printing it on a JOptionPane
JOptionPane.showMessageDialog(null, "Number of ways the robot can move a distance of " + distanceValue +
" taking steps of length " + smallValue + ", " + middleValue + ", " + largeValue + " is: " + now(smallValue, middleValue, largeValue, distanceValue),
"Number of ways", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
The trouble that I am having with my program is that when I test the middle do/while loop. If I set my smallValue = 5, and I also set my middleValue = 5. It will pop the error message up to re-enter a value, but instead of letting me re-enter a new middleValue, it will go on to the next input message for largeValue. But when I test the middleValue against a negative number, then it will prompt the message, and make me reenter a valid number. The same things happen for my largeValue do/while loop.
I figured that I could go ' while (!(middleValue < 0 && middleValue <= smallValue)); ' it will switch my current situation around.
How can I fix my problem? Thanks in advance for peoples input!
public static void main(String args[])
{
String inputSmall, inputMiddle, inputLarge, inputDistance;
int smallValue = 0, middleValue = 0, largeValue = 0, distanceValue = 0;
// Test valid numbers inputed
// No negative numbers
// small < middle < large
// small
do {
inputSmall = JOptionPane.showInputDialog("Small step value?"
smallValue = Integer.parseInt(inputSmall);
if (smallValue >= 0) continue;
JOptionPane.showMessageDialog(null, "Re-enter small step value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (smallValue < 0);
// middle
do {
inputMiddle = JOptionPane.showInputDialog("Middle step value?"
middleValue = Integer.parseInt(inputMiddle);
if (middleValue >= 0 && middleValue > smallValue) continue;
JOptionPane.showMessageDialog(null, "Re-enter middle step value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (middleValue < 0 && middleValue <= smallValue);
// large
do {
inputLarge = JOptionPane.showInputDialog("Large step value?"
largeValue = Integer.parseInt(inputLarge);
if (largeValue >= 0 && largeValue > middleValue && middleValue > smallValue && largeValue > smallValue) continue;
JOptionPane.showMessageDialog(null, "Re-enter large step value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (largeValue < 0 && largeValue <= middleValue && middleValue <= smallValue && largeValue <= smallValue);
// distance
do {
inputDistance = JOptionPane.showInputDialog("Distance value?"
distanceValue = Integer.parseInt(inputDistance);
if (distanceValue >= 0 ) continue;
JOptionPane.showMessageDialog(null, "Re-enter distance value", "Error", JOptionPane.ERROR_MESSAGE);
}
while (distanceValue < 0);
// Creating new Robot and printing it on a JOptionPane
JOptionPane.showMessageDialog(null, "Number of ways the robot can move a distance of " + distanceValue +
" taking steps of length " + smallValue + ", " + middleValue + ", " + largeValue + " is: " + now(smallValue, middleValue, largeValue, distanceValue),
"Number of ways", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}