在同一个 class 中打印另一个方法的值
Printing a value from another method in the same class
我现在有这段代码,它打印一个整数是否为 positive/negative、odd/even、prime/composite。我只想在调用 kCheck 方法后将 kctr 的值打印到打印函数。 for 循环是正确的,但在调用 kCheck 后它只打印 0.
import javax.swing.JOptionPane;
public class FirstClass
{
public int kCheck(int num, int k, int kctr)
{
for(int l=1;l<num+1;l++){
if(( num%l) == 0){
kctr++;
//System.out.println("" +kctr); Just to check if the loop is corrrect
}
}
return kctr;
}
public static void main (String args[])
{
CheckClass checker = new CheckClass();
FirstClass checker2 = new FirstClass();
int num = 0;
int even = 0;
int odd = 0;
int negeven = 0;
int negodd = 0;
int k = 0;
int kctr = 0;
num = Integer.parseInt(JOptionPane.showInputDialog (null, "Input an integer.", JOptionPane.QUESTION_MESSAGE));
boolean ch=true;
while(ch){
if(num%2 == 0 && num>0){
ch=false;
even = 1;
}
else if(num%2 == 1 && num>0){
checker2.kCheck(num, k, kctr);
odd = 1;
System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here.
It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}
else if(num%2 == 0 && num < 0){
negeven = 1;
}
else if(num%2 == 1 && num < 0){
negodd = 1;
}
break;
}
if(even == 1){
checker.posEvenCheck(num);
}
if(odd == 1){
checker.posOddCheck(num, kctr);
}
if(negeven == 1){
checker.negEvenCheck(num);
}
if(negodd == 1){
checker.negOddCheck(num);
}
}
}
更改这部分代码:
else if(num%2 == 1 && num>0){
checker2.kCheck(num, k, kctr);
odd = 1;
System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here.
It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}
收件人:
else if(num%2 == 1 && num>0){
kctr = checker2.kCheck(num, k, kctr); // <- Assign the return value of the method to kctr
odd = 1;
System.out.println("" +kctr);
}
我现在有这段代码,它打印一个整数是否为 positive/negative、odd/even、prime/composite。我只想在调用 kCheck 方法后将 kctr 的值打印到打印函数。 for 循环是正确的,但在调用 kCheck 后它只打印 0.
import javax.swing.JOptionPane;
public class FirstClass
{
public int kCheck(int num, int k, int kctr)
{
for(int l=1;l<num+1;l++){
if(( num%l) == 0){
kctr++;
//System.out.println("" +kctr); Just to check if the loop is corrrect
}
}
return kctr;
}
public static void main (String args[])
{
CheckClass checker = new CheckClass();
FirstClass checker2 = new FirstClass();
int num = 0;
int even = 0;
int odd = 0;
int negeven = 0;
int negodd = 0;
int k = 0;
int kctr = 0;
num = Integer.parseInt(JOptionPane.showInputDialog (null, "Input an integer.", JOptionPane.QUESTION_MESSAGE));
boolean ch=true;
while(ch){
if(num%2 == 0 && num>0){
ch=false;
even = 1;
}
else if(num%2 == 1 && num>0){
checker2.kCheck(num, k, kctr);
odd = 1;
System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here.
It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}
else if(num%2 == 0 && num < 0){
negeven = 1;
}
else if(num%2 == 1 && num < 0){
negodd = 1;
}
break;
}
if(even == 1){
checker.posEvenCheck(num);
}
if(odd == 1){
checker.posOddCheck(num, kctr);
}
if(negeven == 1){
checker.negEvenCheck(num);
}
if(negodd == 1){
checker.negOddCheck(num);
}
}
}
更改这部分代码:
else if(num%2 == 1 && num>0){
checker2.kCheck(num, k, kctr);
odd = 1;
System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here.
It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}
收件人:
else if(num%2 == 1 && num>0){
kctr = checker2.kCheck(num, k, kctr); // <- Assign the return value of the method to kctr
odd = 1;
System.out.println("" +kctr);
}