用户输入数字不加在一起

编程入门 行业动态 更新时间:2024-10-28 08:30:26
本文介绍了用户输入数字不加在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试编写一个从用户(通过键盘)读取整数的程序,向其添加100并显示结果。我所能做的就是让它们像2个字符串一样连接,而不是将数字加在一起。我无法理解为什么它不会添加它们。

I'm trying to write a program that reads an integer from the user (through the keyboard), adds 100 to it and displays the result. All I can do is get them to concatenate like 2 strings, instead of adding the numbers together. I can't understand why it won't add them.

import java.io.*; public class Program { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.print("Enter some text: "); String text = br.readLine(); int number = Integer.parseInt(text); System.out.println(" Your value + 100 is " + ( 100 + text)); } }

是我正在使用的代码,并且:

is the code I am using and:

Enter some text: 66 Your value + 100 is 10066

是在屏幕上打印的内容。

is what is printed on the screen.

推荐答案

你是添加错误的变量。改为使用:

You are adding the wrong variable. Use this instead:

System.out.println(" Your value + 100 is " + ( 100 + number));

更多推荐

用户输入数字不加在一起

本文发布于:2023-10-19 20:37:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在一   不加   数字   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!