Discuss / Java / m到n求和

m到n求和

Topic source

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("please input the number of m:");

int m = scanner.nextInt();

int m1 = m;

System.out.println("please input the number of n:");

int n = scanner.nextInt();

int sum = 0;

while(m<=n) {

sum += m;

m++;

}

System.out.printf("%d到%d的总和为:%d",m1,n,sum);

}

}

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("please input the number of m:");
		int m = scanner.nextInt();
		int m1 = m;
		System.out.println("please input the number of n:");
		int n = scanner.nextInt();
		int sum = 0;
		while(m<=n) {
			sum += m;
			m++;
		}
		System.out.printf("%d到%d的总和为:%d",m1,n,sum);
	}
}


  • 1

Reply