反转数的总和 [英] Sum of reversed numbers

查看:90
本文介绍了反转数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是java的新手,想要学习它。我有一个问题我无法解决:



反转的前x个数字的总和。



我可以反转一个数字,但我不知道如何用for循环播放多个exmp。可以帮助吗?或解释一下? im newb sry



我的代码到目前为止:



Hello,

I'm new in java and want to learn it.I have a problem what i cant solve:

Sum of the reversed first x numbers.

I can reverse a number but i dont get it how to do it with multiple for exmp with a for loop. Can any1 help?Or explain it? im newb sry

My code so far:

int input;
	static int revers = 0;
	static int sum;
	static int i;
	
	public static void main (String [] args){
		
		System.out.println("Enter the max number :");
		int input = new Scanner(System.in).nextInt();
		
		 for(int i = 1;i <= input; i++){
			
			
			
			
	        System.out.print(" "+ i);
		 }
////////////**	 I dont get this part where i should make the reverse for the i numbers till input...


		 i = i * 10;
         i = i + input%10;
         input = input/10;
         sum = i + input;
		
*/////////////

		  System.out.println("\n Reverse of input number is: "+ revers);
		  System.out.println("\n And the sum : "+ sum);

推荐答案

这闻起来很像家庭作业,所以没有代码!但它很简单。



首先写一个函数来反转一个数字 - 你传递它1234然后它返回4321.

然后你可以单独测试它,以确保它在所有情况下都能正常工作(编写起来非常简单:带有模数10的循环来提取数字。然后将当前总数乘以10并加上数字。除以你的数字从10开始,你离开循环,直到输入数字为零)。



然后你需要的是从1循环到x加上反转x的值。

打印总数,你就完成了!
This smells a lot like homework, so no code! But it's pretty simple.

Start by writing a function to "reverse a number" - you pass it 1234 and it returns 4321.
You can then test that in isolation to make sure it works in all cases (and it's pretty simple to write: a loop with a modulous 10 to extract a digit. Then multiply a current total by ten and add the digit. Divide the number you started with by ten and you're away round the loop until the input number is zero).

Then all you need is to loop from 1 to x adding the reversed values of x.
Print the total, and you're done!


这篇关于反转数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆