2つの数値の間の値の範囲を合計します。ユーザーに2つの数字を入力するように促し、最初の数字が大きい場合はループする前に2番目の数字に置き換えます。どうやってやるの?2つの数字の範囲の合計はいくらですか?
import java.util.Scanner;
public class Example {
public static void main (String []args) {
Scanner kb = new Scanner(System.in);
int n1 = 0, n2 = 0, count = 0;
System.out.print("Enter two limits: ");
n1 = kb.nextInt();
n2 = kb.nextInt();
while (n1 <= n2) {
count = count + n2;
n2--;
}
System.out.println("The sum from "+ n1 +" to "+ n2 +" is : " + count);
}
}
と私が欲しいの出力は、(私は6と10を入力した場合)私に語っ
the sum from 6 to 10 is 40
が、私のプログラムの出力は
the sum from 6 to 5 is 40
私が間違って何をやっているのですか?
申し訳ありませんが、 ' – mmmmmzdsdsd5555
低いですn2 - 'は入力値を減らしています – ti7