Very new to Programming, need help...
I need codes that would make a user to try again if they inputed a wrong value...
the program that I am making will ask the user to enter two positive integer, then the program will compute the greatest common divisor of the two integer. I used JOptionPane as the interface to the user. I already have an idea on how to get the GCD, that my problem is only on How to make the program go back to asking the user if they inputed a wrong value, let us say, a negative integer, or a String, or character. Here's what I have done so far:
import javax.swing.JOp tionPane;
public class GreatestCommonD ivisor {
public static void main(String[] args) {
String s1 = JOptionPane.sho wInputDialog(nu ll,"Enter an integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
int num1 = Integer.parseIn t(s1);
String s2 = JOptionPane.sho wInputDialog(nu ll,"Enter another integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
int num2 = Integer.parseIn t(s2);
int gcd = 1;
int k = 1;
int x=num1;
int divisor[] = new int[num1+1];
System.out.prin tln("");
System.out.prin tln("The divisors of "+num1+" that do not divide "+num2+" are:");
System.out.prin tln("");
while(x>=1)
{
divisor[x] = x;
if(num1%x==0 && num2%x!=0)
System.out.prin tln(" "+divisor[x]);
x--;
}
while(k<=num1 && k<=num2)
{
if(num1%k==0 && num2%k==0)
gcd = k;
k++;
}
System.out.prin tln("");
System.out.prin tln("The GCD of "+num1+" and "+num2+" is "+gcd);
System.out.prin tln("");
}
}
I need codes that would make a user to try again if they inputed a wrong value...
the program that I am making will ask the user to enter two positive integer, then the program will compute the greatest common divisor of the two integer. I used JOptionPane as the interface to the user. I already have an idea on how to get the GCD, that my problem is only on How to make the program go back to asking the user if they inputed a wrong value, let us say, a negative integer, or a String, or character. Here's what I have done so far:
import javax.swing.JOp tionPane;
public class GreatestCommonD ivisor {
public static void main(String[] args) {
String s1 = JOptionPane.sho wInputDialog(nu ll,"Enter an integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
int num1 = Integer.parseIn t(s1);
String s2 = JOptionPane.sho wInputDialog(nu ll,"Enter another integer","Probl em 1: Greatest Common Divisor Input", JOptionPane.QUE STION_MESSAGE);
int num2 = Integer.parseIn t(s2);
int gcd = 1;
int k = 1;
int x=num1;
int divisor[] = new int[num1+1];
System.out.prin tln("");
System.out.prin tln("The divisors of "+num1+" that do not divide "+num2+" are:");
System.out.prin tln("");
while(x>=1)
{
divisor[x] = x;
if(num1%x==0 && num2%x!=0)
System.out.prin tln(" "+divisor[x]);
x--;
}
while(k<=num1 && k<=num2)
{
if(num1%k==0 && num2%k==0)
gcd = k;
k++;
}
System.out.prin tln("");
System.out.prin tln("The GCD of "+num1+" and "+num2+" is "+gcd);
System.out.prin tln("");
}
}
Comment