2016-08-04 64 views
0

我正在嘗試製作一個時間表計數器。它應該輸出帶計數器的乘數,顯示答案爲零

1×5是5 2×5是10 3×5是15 高達10×5 50

的輸入是5,計數器從第i取在for循環中。

這是通過數字計算,但我無法得到它來計算結果,我看不到我失蹤。任何幫助將低於

import java.util.Scanner; 
public class Program { 

public static void main(String[] args) { 
    Scanner kb = new Scanner(System.in); 
    int input = kb.nextInt(); 
    Math math1 = new Math(0,0); 
    for(int i = 0; i <= 10; i++){ 
     math1.setNum2(i); 
     math1.multiplier(); 
     System.out.println(input + " times " + i + " is " + math1.getResult()); 
    } 
} //main 

} // class Program 

public class Math { 

private int num; 
private int num2; 
private int result; 
//constructor// 
public Math(int num, int num2){ 
    this.num = num; 
    this.num2 = num2; 
    this.result = result; 
} 

//get// 
public int getNum(){ 
    return this.num; 
} 

public int getNum2(){ 
    return this.num2; 
} 

public int getResult(){ 
    return this.result; 
} 
//set// 
public void setNum(int value){ 
    this.num = value; 
} 

public void setNum2(int value){ 
    this.num2 = value; 
} 
//other// 
public void multiplier(){ 
    this.num = num; 
    result = num * num2; 
} 
} // class Math 
+0

你似乎將所有東西都乘以零。再次檢查參數到數學課。 – markspace

+4

從不使用'input'。嘗試'數學math1 =新數學(輸入,0)'。 – Arthur

+1

'this.result = result'(在構造函數中)沒有結果參數...看起來有點簡單... – OliPro007

回答

1

理解

代碼你似乎乘次零不管你在做什麼。 Math math1 = new Math(0,0);意味着某些* 0。你需要在代碼中使用你的輸入。正如阿瑟提到的使用,Math math1 = new Math(input, 0)

1

你總是乘以0.因此你的結果。

更改您的代碼像在main方法如下:

// use the input that you took 
//let's take 5 
Math math1 = new Math(0,0); 
math1.setNum(input); 

之後,請務必在Math類使用它。

更新的構造:

public Math(int num, int num2){ 
    this.num = num; 
    this.num2 = num2; 
} 

result無關,在這裏做。

但後來的問題來了,如何獲得result

對於改變乘數方法如下:

public void multiplier(){ 
    this.result = num * num2; 
} 
0

你被隊友0乘以一切。您會看到您傳遞給Math類對象的那些參數。

試試以下行:

Scanner in = new Scanner(System.in); 
int number = in.nextInt(); // I suppose this is where the user enters the numbers say 5. 
Math math = new Math(number, 0); 
for(int i=1; i<=10; i++){ 
    math.setNum2(i); 
    math.multiplier(); 
    System.out.println(input + " times " + i + " is " + : math.getResult()); 
} 

這應該讓你通過。