如果第二个数字是第一个数字的倍数,该程序应该返回True。如果不是,应该做三次。 输出只是给出第一个答案是正确的。 如何获得包含变量f和g的回报?决定第二个数字是否是第一个数字的倍数的程序
或者,如果这不是正确的方式去了解它是什么?我需要让他们都来自相同的方法,否则我只是做更多的方法,但因为它我是难倒。
任何帮助,非常感谢。对不起我的不愉快。
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
您可以将返回类型更改为'Boolea []'并使用'return new Boolean [] {e,f,g}'返回所有结果。你也可以使用'boolean'而不是'Boolean' – Titus
'f'和'g'是什么?他们似乎没有任何用处。 – HyperNeutrino
我已经使用布尔代替布尔值,以便我可以初始化为null,这是一个坏主意吗?感谢另一件事,但这非常有帮助。 – johnny