本文介绍了用户输入数字不加在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个从用户(通过键盘)读取整数的程序,向其添加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));更多推荐
用户输入数字不加在一起
发布评论