0

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
lkatiforis
  • 5,703
  • 2
  • 16
  • 35
Jake
  • 1
  • 3
  • A couple of notes. This isn't what sentinel is usually used to mean. And are you sure `x%y == 1` means that it's a multiple? `10 % 9 == 1` but 10 isn't a multiple of 9. – BeUndead Sep 19 '21 at 23:01

1 Answers1

0
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

DevZer0
  • 18
  • 3
  • if you would like a code small editing to your code, you may ask........I will edit my answer with the code – DevZer0 Sep 19 '21 at 22:22
  • That would be great if you could provide some code. Im still a beginner so im still not sure how exactly i would apply this without looking at the code itself – Jake Sep 19 '21 at 22:36
  • i have updated the answer. any issues in understanding please let me know – DevZer0 Sep 19 '21 at 22:39
  • is it working ? – DevZer0 Sep 19 '21 at 22:45
  • yes it is indeed working. Seeing your thought process in code was very valuable, now I understand how to go about this. Thanks so much. – Jake Sep 19 '21 at 22:48
  • I feel it's useful to note that java doesn't support [tail-call recursion](https://stackoverflow.com/a/67705002/2478398) <- link. So this will keep growing the stack. Instead, calling it in a `for` or `while`/`do`-`while` loop at the top level would be nicer. – BeUndead Sep 19 '21 at 22:58
  • @BeUndead thanks for improving my way of thinking ... I read about tail-call and why it's undesirable... – DevZer0 Sep 19 '21 at 23:41
  • @BeUndead I did attempt to do that in the beginning but I wasn't quite sure exactly how I could get it done. I'm sure it was a result of my lack of knowledge on it, but when I tried to do a for/while, I either had a problem with symbols not being recognized, or breaking the program entirely to where it didn't even display the text outputs or take inputs from the user. also- thank you for the correction on the remainder argument, that was an oversight and its now fixed. – Jake Sep 19 '21 at 23:45