【问题标题】:How can I return multiple variables and/or use a private int method?如何返回多个变量和/或使用私有 int 方法?
【发布时间】:2012-11-14 11:17:39
【问题描述】:

这是我的代码:

import static java.lang.System.*;

public class Triples
{
 private int number;

public Triples()
{
    this(0);
}

public Triples(int num)
{
    number = num;
}

public void setNum(int num)
{
    number = num;
}

private int greatestCommonFactor(int a, int b, int c)
{
    for(int n = 0; n <= number; n++)
    {
    int max = number;
    for(a = 1; a <= max; a++)
    {
        a = n;
        for(b = a +1; b <= max; b++)
        {
            b =n;
            for(c = b + 1; c <= max; c++)
            {
                c = n;
                if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
                {
                    if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
                    {
                        if(a%2<=1 && b%2<=1 && c%2<=1)
                        {
                            return 1;
                        }
                    }
                }
            }
        }
    }
    }

    return 1;
}

public String toString()
{
    String output="";
    output = greatestCommonFactor(a, b, c);


    return output+"\n";
}
}

我需要让它打印出变量 a、b 和 c,但我不知道如何做到这一点。我目前收到的错误消息是“a 无法解析为变量 b 无法解析为变量 c 无法解析为变量”

如果有帮助,这里是相关实验表的链接:https://docs.google.com/open?id=0B_ifaCiEZgtcX08tbW1jNThZZmM

更新 这是我更新的 toString 方法:

public String toString()
{
    int a = 0;
    int b = 0;
    int c = 0;
    String output="";
    output += greatestCommonFactor(a, b , c) + "\n";


    return output;
}

当我在编辑时,我最棒的CommonFactor 方法:

private int greatestCommonFactor(int a, int b, int c)
{
    for(int n = 0; n <= number; n++)
    {
    int max = number;
    for(a = 1; a <= max; a++)
    {
        a = n;
        for(b = a +1; b <= max; b++)
        {
            b =n;
            for(c = b + 1; c <= max; c++)
            {
                c = n;
                if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
                {
                    if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
                    {
                        if(a%2<=1 && b%2<=1 && c%2<=1)
                        {
                            return greatestCommonFactor(a, b, c);
                        }
                    }
                }
            }
        }
    }
    }

    //return 1;
}

更新 #2

这是(希望)为最伟大的CommonFactor 和 toString 方法编写代码的更正确的方法:

private int greatestCommonFactor(int a, int b, int c)
{
    a = 0;
    b = 0;
    c = 0;
    for(int n = 0; n <= number; n++)
    {
    int max = number;
    for(a = 1; a <= max; a++)
    {
        a = n;
        for(b = a +1; b <= max; b++)
        {
            b =n;
            for(c = b + 1; c <= max; c++)
            {
                c = n;
                if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
                {
                    if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
                    {
                        if(a%2<=1 && b%2<=1 && c%2<=1)
                        {
                            return a;
                        }
                    }
                }
            }
        }
    }
    }

    return greatestCommonFactor(a, b, c);
}

public String toString()
{

    String output="";
    output += greatestCommonFactor(a, b , c) + "\n";


    return output;
}

Runner 类添加

import static java.lang.System.*;

import java.util.Scanner;

public class Lab11j
{
 public static void main(String args[])
 {
       Scanner keyboard = new Scanner(System.in);
        String choice="";
            do{
                out.print("Enter the max number to use : ");
                int big = keyboard.nextInt();


                    //instantiate a TriangleThree object
             Triples trip = new Triples( big);
                //call the toString method to print the triangle
                System.out.println( trip );

                System.out.print("Do you want to enter more data? ");
                choice=keyboard.next();
            }while(choice.equals("Y")||choice.equals("y"));
    }
}

【问题讨论】:

    标签: java variables int private-methods


    【解决方案1】:

    您在使用变量时没有声明和初始化它们:

    output = greatestCommonFactor(a, b, c); // where are a, b and c decleared in this method?
    

    此外,您的 greatestCommonFactor() 方法接受参数,但除了重新初始化它们之外什么都不做,所以它很可能根本不接受任何参数:

    private int greatestCommonFactor(int a, int b, int c) {
        for (int n = 0; n <= number; n++) {
            int max = number;
            for (a = 1; a <= max; a++)
            // here you're setting a to 1. So why pass its value as an argument to the method,
            // since you don't care about the passed in value? 
    

    编辑:

    而不是拥有

    private int greatestCommonFactor(int a, int b, int c) { // these are arguments
        a = 0;
        b = 0;
        c = 0;
        ...
    }
    

    你应该有

    private int greatestCommonFactor() {
        int a = 0;
        int b = 0;
        int c = 0; // these are local variables
        ...
    }
    

    而不是拥有

    public String toString() {
        String output="";
        output += greatestCommonFactor(a, b , c) + "\n";
        return output;
    }
    

    你应该有

    // yes, this method name is ugly, but it says what the method does
    // I would simply make greatestCommonFactor a public method, and remove this method,
    // since it doesn't do anything useful.
    public String computeGreatestCommonFactorAndReturnItAsAStringWithANewLine() {
        return greatestCommonFactor() + "\n";
    }
    

    【讨论】:

    • 我跟踪您对变量的看法,但如果您查看我在我的 OP 中链接到的 Google 文档文件,这可能有意义吗?
    • 不,它没有。参数是方法的输入。 a、b 和 c 不是输入,因为您从程序的描述中知道,它们总是从 1 开始。
    • 你仍然在做同样的事情,你正在使用 toString() 来计算一些值,这不是 toString() 方法的目标。
    • 我想我这次修好了
    • 不。仅传递参数以重置参数的值是没有意义的。该方法应该接受任何参数。 a、b 和 c 应该是方法的局部变量。而且你还在滥用 toString()。将 toString() 方法命名为其他名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多