我需要找到并显示前n个自然数的立方体的总和,以及我目前使用的代码,我只能打印到我想要的范围。
最大值是20,它打印我的数字的立方体,最多为20个。我为总和写的循环不起作用,通常会使程序崩溃。
任何有关如何制作它的帮助或想法,以便程序只能累加到输入的数字,然后总结这些立方体将不胜感激,请保持简单。
public class MyIntNumberr
{ // construct a myintnumber with one instance field n
public MyIntNumberr(int n)
{
number = n;
}
public void calcCubeAndSum(int num)
{
if (num < 0)
{
System.out.println("that integer is invalid please try again");
System.exit(0);
}
while (num >= 1 && num <= 20)
{
System.out.println(" " + num + " " + Math.pow(num, 3));
num = num + 1;
}
}
// instance fields
private int number;
}
import javax.swing.JOptionPane;
public class MyIntNumberTestt
{
public static void main(String[] args)
{
int number, num;
String input = JOptionPane.showInputDialog("enter a value to cube and gather the sum of");
num = Integer.parseInt(input);
MyIntNumberr richard = new MyIntNumberr(num);
richard.calcCubeAndSum(num);
System.exit(0);
}
}
答案 0 :(得分:0)
我没有在你的代码中看到你累积多维数据集的总和......
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws Exception {
System.out.print("Enter a value to cube and gather the sum of: ");
int num = input.nextInt();
calcCubeAndSum(num);
}
private static void calcCubeAndSum(int num) {
if (1 <= num && num <= 20) {
int sum = 0;
while (num <= 20) {
// Calculate the cube
int cube = num * num * num;
// Accumulate the cube into a sum
sum += cube;
// Display current result
System.out.println("Current number: " + num + "\tCubed: " + cube + "\tCurrent Sum: " + sum);
// Go to the next number
num++;
}
// Display Total Sum
System.out.println("Total Sum: " + sum);
} else {
System.out.println("That integer is invalid please try again");
}
}
结果:
答案 1 :(得分:0)
使用PYTHON可以轻松找到N个自然数 希望大家都喜欢。...
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptFader : MonoBehaviour
{
// attached game object for fading
public GameObject Sphere;
// fade speed length
public float fadeSpeed;
//Pause length between fades
public int fadePause;
void Awake()
{
StartCoroutine(FadeOut(fadeSpeed));
}
//Fade Out Coroutine
public IEnumerator FadeOut(float fadeSpeed)
{
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color matColor = rend.material.color;
float alphaValue = rend.material.color.a;
//while loop to deincrement Alpha value until object is invisible
while (rend.material.color.a > 0f)
{
alphaValue -= Time.deltaTime / fadeSpeed;
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, alphaValue);
yield return new WaitForSeconds(fadePause);
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 0f);
StartCoroutine(FadeIn(fadeSpeed));
}
//Fade In Coroutine
public IEnumerator FadeIn(float fadeSpeed)
{
//waits for the return value of FadeOut coroutine to commence
yield return FadeOut;
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color matColor = rend.material.color;
float alphaValue = rend.material.color.a;
//while loop to increment object Alpha value until object is opaque
while(rend.material.color.a < 1f)
{
alphaValue += Time.deltaTime / fadeSpeed;
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, alphaValue);
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 1f);
StartCoroutine(FadeOut(fadeSpeed));
}
}
答案
n = int(input("Enter the value of n:"))
for i in range (1,n+1):
print(i)