I've created a program that prompts the user to enter two integers and determine if the second integer is a multiple of the first and it works fine for one pair of numbers. However, I want to implement sentinel-controlled iteration so that the program repeats its inquiry to the user and produces output until the user wants to terminate the program. I have tried multiple times to accomplish this using the traditional way of implementing it but I run into problems with getting the exit condition to be recognized and incorporated while repeating the entire loop, in other words, I'm not sure how to get the continue/exit conditions into the program without breaking it.
Here is the code for the program that works properly, but needs the loop implemented somewhere where it will work. Thanks in advance.
import java.util.Scanner;
public class Multiples {
public static void main(String[] args) {
//import scanner
Scanner input = new Scanner(System.in);
System.out.printf ("Enter two integers separated by a space. %n ");
int number2 = input.nextInt();
int number1 = input.nextInt();
//ask user for input of two integers, to determine if the first integer is -
//- a multiple of the second
//begin method call for ismultiple
if (ismultiple(number1, number2)==true)
System.out.println (number1+" is a multiple of "+number2);
else
System.out.println (number1+" is not a multiple of "+number2);
//end main method
//begin execution of method ismultiple using boolean output for true
}
public static boolean ismultiple(int x, int y) {
if(x%y==0)
return true;
if (x%y==1)
return true;
else
return false;
}
//terminate ismultiple
}
//terminate program
import java.util.Scanner;
class Multiples {
public static void main(String[] args) {
//import scanner
input();
}
public static void input()
{
Scanner input = new Scanner(System.in);
System.out.printf ("Enter two integers separated by a space. %n ");
int number2 = input.nextInt();
int number1 = input.nextInt();
if (ismultiple(number1, number2)==true)
System.out.println (number1+" is a multiple of "+number2);
else
System.out.println (number1+" is not a multiple of "+number2);
//recursion
System.out.println("want to perform operations again?\n yes-y no-n");
char choice=input.next().charAt(0);
switch(choice)
{
case 'y': input();
break;
}
}
public static boolean ismultiple(int x, int y) {
if(x%y==0)
return true;
if (x%y==1)
return true;
else
return false;
}
//terminate ismultiple
}
i added a new input method and called it again as depicted