用户输入数字不加在一起 [英] User input numbers not adding together

查看:111
本文介绍了用户输入数字不加在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个从用户(通过键盘)读取整数的程序,向其添加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));

这篇关于用户输入数字不加在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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