1

これが私のコードです。

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

以下は (願わくば) より正確な方法で、 maximumCommonFactor および 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;
}

ランナークラス追加

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

1 に答える 1

3

変数を宣言および初期化せずに使用しています。

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";
}
于 2012-11-26T22:16:05.647 に答える