Java 用于确定第二个数字是否为第一个数字的倍数的程序无法正常工作
如果第二个数字是第一个数字的倍数,则该程序应返回True。如果不是,则为False,并且应该执行三次。 输出只是给出第一个答案的正确答案。 如何得到包含变量f和g的返回 或者如果这不是正确的方法,那是什么?我需要让它们都来自同一种方法,否则我只会制作更多的方法,但事实上我被难倒了 非常感谢您的帮助。对不起,我没有生气Java 用于确定第二个数字是否为第一个数字的倍数的程序无法正常工作,java,Java,如果第二个数字是第一个数字的倍数,则该程序应返回True。如果不是,则为False,并且应该执行三次。 输出只是给出第一个答案的正确答案。 如何得到包含变量f和g的返回 或者如果这不是正确的方法,那是什么?我需要让它们都来自同一种方法,否则我只会制作更多的方法,但事实上我被难倒了 非常感谢您的帮助。对不起,我没有生气 import java.util.Scanner; public class Numbers3 { // starts execution of java applica
import java.util.Scanner;
public class Numbers3 {
// starts execution of java application
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int firstnumber = 0; // initialize integer first number
int secondnumber = 0; // initialize integer second number
int third = 0;
int fourth = 0;
int fifth = 0;
int sixth = 0;
// First input field
System.out.print("Input first number ");
firstnumber = input.nextInt();
// Second input field
System.out.print("Input second number ");
secondnumber = input.nextInt();
// makes result equal the Boolean output of isMultiple method
Boolean result = isMultiple(firstnumber, secondnumber, third, fourth,
fifth, sixth);
System.out.println("" + result);
System.out.println();
System.out.print("input first number ");
third = input.nextInt();
System.out.print("input second number ");
fourth = input.nextInt();
System.out.println("" + result);
System.out.println();
System.out.print("input first number ");
fifth = input.nextInt();
System.out.print("input second number ");
sixth = input.nextInt();
System.out.println("" + result);
}
// creates method using the user input
public static Boolean isMultiple(int a, int b, int w, int x, int y, int z) {
Boolean e = null; // initialize boolean
Boolean f = null;
Boolean g = null;
if (a % b != 0) // what the function does if the result is not 0
e = false;
// what the function will do if the function does result in 0
if (a % b == 0)
e = true;
if (w % x != 0)
f = false;
if (w % x == 0)
f = true;
if (y % z != 0)
g = false;
if (y % z == 0)
g = true;
return e;
// returns e as the result of this method.
} // end program
} // end class
我不确定
f
和g
是怎么回事,但这里有一个程序,它要求两个数字,三次,如果第二个是第一个的倍数,则打印true
或false
:
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 3; i++) {
System.out.print("Enter first number : ");
int first = Integer.parseInt(reader.
System.out.print("Enter second number : ");
int second = Integer.parseInt(reader.readLine());
boolean secondMultipleOfFirst = second % first == 0;
System.out.println(secondMultipleOfFirst);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
对于(int i=0;i<3;i++){
系统输出打印(“输入第一个数字:”);
int first=Integer.parseInt(读取器。
系统输出打印(“输入第二个数字:”);
int second=Integer.parseInt(reader.readLine());
布尔值secondMultipleOfFirst=second%first==0;
System.out.println(second multipleoffirst);
}
}
我也知道你的方法有什么问题。你计算的值是正确的,但是每次你返回e
,这是第一个结果。因此,接下来的两个输入给出了第一个结果
使用循环代替设置更多方法或查看返回哪个值的方法。这样,您可以获取两个值,然后查看
n2
是否是n1
的倍数。对于每次运行,有两个输入。目标:使用
isMultiple()
检查第一个输入是否是第二个输入的倍数
要运行3次(或任意次数),请将重复代码
放入for循环中。#重复循环的次数存储在常量
NUM\u RUNS
中
代码:
import java.util.Scanner;
public class Numbers3 {
private static final int NUM_RUNS = 3;
// starts execution of java application
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < NUM_RUNS; i++) {
// First input field
System.out.print("Input first number: ");
int firstNumber = input.nextInt();
// Second input field
System.out.print("Input second number: ");
int secondNumber = input.nextInt();
System.out.printf("%d is a multiple of %d: %s%n%n",
firstNumber, secondNumber,
isMultiple(firstNumber, secondNumber));
}
}
public static boolean isMultiple(int a, int b) {
return (a % b == 0);
}
} // end class
Input first number: 8
Input second number: 2
8 is a multiple of 2: true
Input first number: 7
Input second number: 3
7 is a multiple of 3: false
Input first number: 18
Input second number: 6
18 is a multiple of 6: true
用户输入已放入循环。此程序将接受用户输入并检查值。您可以将返回类型更改为a
Boolea[]
并使用return new Boolean[]{e,f,g}
返回所有结果。此外,您可能希望使用布尔值而不是布尔值如何处理f
和g
?它们似乎没有任何用处。我使用了布尔值而不是布尔值,这样我就可以初始化为null,这是个坏主意吗?不过,感谢另一件事,这非常有用。它不是这是最好的主意,但它确实有效。此外,使用if(){}else{}
@johnny是不必要的,因为您只需要一个二进制响应,true
或false
@elliotfrisch My bad应该是布尔值。从OP的code.Nice中编辑它。另外,我建议声明int
(s)在for
正文中。@ElliottFrisch在for循环中声明int(s),即firstNumber和secondNumber,有什么显著的好处吗?我的意思是,我可以这样做,只是对我来说似乎是一样的?限制变量可见性是一个相当基本的OOP原则。另外,少两行代码。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dump_me;
import java.util.Scanner;
public class Numbers3
{
// starts execution of java application
public static void main( String[] args)
{
Scanner input = new Scanner (System.in );
int firstnumber = 0; // initialize integer first number
int secondnumber = 0; // initialize integer second number
// makes result equal the Boolean output of isMultiple method
String loop = "N";
do{
// First input field
System.out.print("Input first number ");
firstnumber = input.nextInt();
// Second input field
System.out.print("Input second number ");
secondnumber = input.nextInt();
Boolean result = isMultiple(firstnumber, secondnumber);
System.out.println("" + result );
System.out.println();
System.out.println("Do you wan to continue? Press y to continue or n to exit" );
loop = input.next();
}while(loop.equalsIgnoreCase("y"));
}
// creates method using the user input
public static Boolean isMultiple( int a, int b)
{
if(a==0 || b==0)
{
return false;
}
else
{
if(b % a ==0)
{
return true;
}
else
{
return false;
}
}
// returns e as the result of this method.
} // end program
} // end class