2015-06-09 19 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 

用户输入已被放置在回路中。该程序将接受用户输入并检查值。

相关问题