[英]Write a method to work out the sum of the first n odd numbers
首先讓我說我對編程很陌生,這是我開始以來的第二周,所以如果你發現代碼中有任何不好的做法或錯誤,請接受我的道歉。
我想打印前 n 個奇數的總和。 但到目前為止,我只能將奇數的總和到給定的數字。 請幫助。
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
System.out.print("Please enter the number : ");
int num1 = userInput.nextInt();
int sum = sumOfOdd(num1);
System.out.println("sum of first " +num1 + " odd numbers is " + sum);
userInput.close();
}
static int sumOfOdd(int num)
{
int sum = 0;
for (int i = 0; i <= num; i++)
{
if(i % 2 != 0)
{
sum += i;
}
}
return sum;
}
}
您根本不必使用循環
static int sumOfOdd(int num) {
return num*num;
}
對於任何算術級數,數字的總和由下式給出,
Sn=1/2×n[2a+(n-1)×d]
在哪里,
Sn= n 個數字的總和
n = n 個數字
a = AP 的第一個學期
d= AP 中的共同差異
使用上面的公式,我們可以得出這個快速公式來計算前 n 個奇數的總和,
Sn(奇數)= n²
試試這個,它使用一個遞增 2 的for 循環,只考慮奇數。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
System.out.println("The sum of the first " + n + " odd numbers is: " + sumOfOddNumbers(n));
}
public static int sumOfOddNumbers(int n) {
int sum = 0;
for(int i = 1; i < n*2; i+=2) {
sum += i;
}
return sum;
}
}
示例用法:
Enter the value of n: 5
The sum of the first 5 odd numbers is: 25
嘗試這個:
static int sumOfOdd(int num) {
int sum = 0;
for (int i = 0; i < num; i++){
sum += i*2+1;
}
return sum;
}
它將所有奇數相加,直到達到限制。
用i*2+1
你得到下一個奇數。 然后你把它加到總和中。
用System.out.println(sumOfOdd(4));
測試得到了預期的結果16
(1+3+5+7)
將計數器更改為將奇數添加到總和值的次數...
static int sumOfOdd(int num) {
int sum = 0;
int i = 0;
int count = 0;
do {
if(i % 2 != 0) {
sum += i;
count++;
}
i++;
} while (count < num);
return sum;
}
甚至更清潔:
static int sumOfOdd(int num) {
int sum=0;
for (int i=1;i<num*2;i+=2) {
sum=sum+i;
}
return sum;
}
您應該計算您添加了多少數字,並在條件下檢查您求和的數字計數是否小於或等於您的 n。 只需在 for 循環中添加計數器並將條件設置為: count <= num
,它應該可以工作。
每次將數字添加到總和時,計數都會增加count++
。
代碼假設如下所示:
static int sumOfOdd(int num)
{
int sum = 0;
int count = 0;
for (int i = 0, count= 0; count <= num; i++)
{
if(i % 2 != 0)
{
sum += i;
count++;
}
}
return sum;
}
我沒有檢查過,但應該是正確的
由於您不知道需要多少個循環周期,您必須更改for循環的退出條件。 或者,您可以使用利用相同退出條件的while循環。
static int sumOfOdd(int num){
int sum = 0;
int counter = 0;
int currentNumber = 0;
while (counter<num){
if(currentNumber % 2 != 0){
sum += currentNumber;
counter++;
}
currentNumber++;
}
return sum;
}
這是您將使用的完整代碼:
public class YourClass {
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
System.out.print("Please enter the number : ");
int num1 = userInput.nextInt();
int sum = sumOfOdd(num1);
System.out.println("sum of first " +num1 + " odd numbers is " + sum);
userInput.close();
}
static int sumOfOdd(int num)
{
int counter = 0;
for (int i = 0;; i++)
{
int sum = 0;
if(i % 2 != 0)
{
counter++;
sum += i;
}
if(counter == num) return sum;
}
}
}
另一種選擇。
static int sumOfOdd(int num) {
int sum = 0;
int last = 2*num-1;
for (int i = 1; i <= last; i+=2){
sum += i;
}
return sum;
}
顯然return num*num;
是最有效的,但如果你不得不使用循環,那么這種方法會避免循環內的*
。
這將比:
for (int i = 0; i < num; ++i){
sum += 2*i+1;
}
import java.util.*;
class karan{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = n;
int sumOddNumber = n * i;
System.out.println(n*i);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.