如果第二個數字是第一個數字的倍數,該程序應該返回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