この質問の一部を行うことができましたが、キューブ メソッドに問題があります。キューブの結果を返すには、cube メソッド内から square メソッドを呼び出す必要があります。例: 数値 5 を 2 乗すると、結果は 25 になります。次に、このメソッドをキューブ メソッドに呼び出して、答え 125 を返します。どこが間違っているのか教えてください。
これが私のコードです:
import java.util.*;
public class ExamPaper2011
{
public static void main(String [] args){
int totalSquared = 0;
int totalCubed = 0;
cubedNumber(totalSquared, totalCubed);
}
public static int squaredNumber(int totalSquared){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a number to square: ");
int numSquare = in.nextInt();
System.out.println("You entered " + numSquare);
totalSquared = (int) Math.pow (numSquare, 2);
System.out.println("The number squared is " + totalSquared);
return totalSquared;
}
public static int cubedNumber(int totalSquared, int totalCubed){
squaredNumber(totalSquared);
totalSquared = (int) Math.sqrt(totalSquared * totalSquared);
System.out.println(totalSquared);
totalCubed = totalSquared;
totalCubed = (int) Math.pow (numSquare, 3);
return totalCubed;
}
}
メソッド cubedNumber は 0 を返すようです。私の基本的なコードを許してください。クラス会です。
これが答えです。ありがとうございました。
import java.util.*;
public class ExamPaper2011
{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a number to square and cube: ");
int n = in.nextInt();
cubedNumber(n);
}
public static int squaredNumber(int n){//Question 4
System.out.println("You entered " + n);
n = n * n;
System.out.println("Squared = " + n);
return n;
}
public static int cubedNumber(int n){
squaredNumber(n);
n = n * squaredNumber(n);
System.out.println("Cubed = " + n);
return n;
}
}
この素晴らしいフィードバックに感謝します。本当に助かります。皆さん、ありがとうございました。