我将如何着手对双方法程序实施 Sentinel 迭代?
How would I go about implementing Sentinel iteration to two-method program?
我创建了一个程序,提示用户输入两个整数并确定第二个整数是否是第一个整数的倍数,并且它适用于一对数字。但是,我想实现哨兵控制的迭代,以便程序重复对用户的查询并产生输出,直到用户想要终止程序。我曾多次尝试使用传统的实现方式来完成此操作,但我 运行 遇到了在重复整个循环时识别和合并退出条件的问题,换句话说,我不确定如何在不破坏程序的情况下将 continue/exit 条件加入程序。
这是程序正常工作的代码,但需要在它能工作的地方实现循环。提前致谢。
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
}
我添加了一个新的输入法并再次调用它,如图所示
我创建了一个程序,提示用户输入两个整数并确定第二个整数是否是第一个整数的倍数,并且它适用于一对数字。但是,我想实现哨兵控制的迭代,以便程序重复对用户的查询并产生输出,直到用户想要终止程序。我曾多次尝试使用传统的实现方式来完成此操作,但我 运行 遇到了在重复整个循环时识别和合并退出条件的问题,换句话说,我不确定如何在不破坏程序的情况下将 continue/exit 条件加入程序。
这是程序正常工作的代码,但需要在它能工作的地方实现循环。提前致谢。
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
}
我添加了一个新的输入法并再次调用它,如图所示