2015-06-09 21 views
-1

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

您可以將返回類型更改爲'Boolea []'並使用'return new Boolean [] {e,f,g}'返回所有結果。你也可以使用'boolean'而不是'Boolean' – Titus

+0

'f'和'g'是什麼?他們似乎沒有任何用處。 – HyperNeutrino

+0

我已經使用布爾代替布爾值,以便我可以初始化爲null,這是一個壞主意嗎?感謝另一件事,但這非常有幫助。 – johnny

回答

2

對於每次運行,都有兩個輸入。
目標:檢查第一個輸入是否使用第二個輸入的倍數isMultiple()

要運行它3(或任何#)次,請將repeating code置於for循環中。
重複循環的次數存儲在常量NUM_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 
+0

@ElliottFrisch我的不好,應該是布爾值。從OP的代碼編輯它。 – Gosu

+0

不錯。另外,我建議在'for'主體中聲明'int'(s)。 –

+0

@ElliottFrisch在for循環中聲明int(s),即firstNumber&secondNumber有什麼好處?我的意思是,我可以這樣做,只是對我來說看起來是一樣的? – Gosu

0

我不知道有什麼用fg,但這裏有一個要求兩個數字,三次,並打印truefalse程序如果第二是第一的倍數:

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); 
    } 
} 

我也知道你的方法有什麼問題。您正在計算正確的值,但每次返回e時,這是第一個結果。所以,接下來的兩個輸入會給出第一個結果。

而不是設置更多的方法,或一種方法來查看返回哪個值,使用循環。這樣,你拿兩個值,看看n2n1的倍數。

0
/* 
* 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 

用戶輸入已被放置在迴路中。該程序將接受用戶輸入並檢查值。

相關問題