A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 滔哥 黑马帝   /  2014-8-14 12:36  /  20644 人查看  /  85 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文



这是一个非常简单的题目,意在考察你编程的基础能力。千万别想难了哦。输入为一行,包括了用空格分隔的三个数据A、B、C(数据范围均在-40~40之间)。输出为一行,为“A+B+C”的计算结果。


样例输入:
  1. 22 1 3
复制代码

样例输出:

  1. 26
复制代码

回帖提交答案:代码+答案运行结果截图。


正确答案奖励:2技术分

85 个回复

倒序浏览

RE: 第13课:A+B+C问题(练习)

本帖最后由 Dongjiawei 于 2014-8-14 13:25 编辑
  1. class Test
  2. {
  3.         public static void main(String[ ] args)
  4.         {
  5.                 int x=21;
  6.                 int y=7;
  7.                 int z=12;
  8.                 int num=x+y+z;
  9.                 System.out.println(x+" "+y+" "+z);
  10.                 System.out.println(num);
  11.         }
  12. }
复制代码

回复 使用道具 举报
  1. import java.util.Scanner;
  2. class Test1
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                
  7.                 Scanner input = new Scanner(System.in);
  8.                 short a = input.nextShort();
  9.                 short b = input.nextShort();
  10.                 short c = input.nextShort();
  11.                 System.out.println(a+b+c);               
  12.                
  13.         }
  14. }
复制代码


不会提交结果截图肿么办:(
回复 使用道具 举报
本帖最后由 冒牌高手 于 2014-8-15 14:50 编辑

代码如下
  1. import java.util.Scanner;
  2. class P
  3. {
  4. public static void main(String[] args)
  5. {
  6. //System.out.println("Hello World!");
  7. Scanner sc=new Scanner(System.in);
  8. int[] arr=new int[3];
  9. int sum=0;
  10. for (int i=0;i<3 ;i++ )
  11. {
  12. System.out.println("please input the number NO."+(i+1));
  13. arr[i]=sc.nextInt();
  14. if (arr[i]>=-40 && arr[i]<=40)
  15. {
  16. sum+=arr[i];
  17. }
  18. else
  19. {
  20. System.out.println("wrong num,input again:");
  21. i--;
  22. }

  23. }
  24. System.out.println("the num you input is");
  25. for (int i=0;i<3 ;i++)
  26. {
  27. System.out.print(arr[i]+" ");
  28. }
  29. System.out.println("\r\nthe sum of your nums is");
  30. System.out.println(sum);
  31. }
  32. }
复制代码

捕获.PNG (3.8 KB, 下载次数: 91)

捕获.PNG

评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报
本帖最后由 sk0806 于 2014-8-14 15:40 编辑
  1. import java.util.Scanner;
  2. class Demo
  3. {
  4.         public static void main(String [] args)
  5.         {
  6.                 int a,b,c;
  7.                 Scanner input=new Scanner(System.in);
  8.                 a=input.nextInt();
  9.                 b=input.nextInt();
  10.                 c=input.nextInt();
  11.                 System.out.println(a+b+c);
  12.         }
  13. }
复制代码
结果截图:

Demo.png (51.68 KB, 下载次数: 114)

Demo.png
回复 使用道具 举报
本帖最后由 不淡定,小学生 于 2014-8-15 19:54 编辑

import java.util.Scanner;
class Abc
{
   public static void main(String[] args)  
   {   
          int a,b,c;
         Scanner input=new Scanner(System.in);
        a=input.nextInt();
        b=input.nextInt();
        c=input.nextInt();
         if((a>=-40&&a<=40)&&(b>=-40&&b<=40)&&(c>=-40&&c<=40))
        System.out.println(a+b+c);
        else
        System.out.println("取值超范围");
    }
}

QQ图片20140815195314.jpg (77.7 KB, 下载次数: 97)

QQ图片20140815195314.jpg

点评

好了  发表于 2014-8-15 19:37
取值范围哪去了?  发表于 2014-8-15 17:26
回复 使用道具 举报
liqiaohui 发表于 2014-8-14 13:23
不会提交结果截图肿么办

滔哥。。。。   
这个回复没附件 然后我也没权限上传图片。。
没分就没分了吧  图片这事麻烦滔哥指点一下
回复 使用道具 举报
sk0806 高级黑马 2014-8-14 15:14:20
8#
liqiaohui 发表于 2014-8-14 15:12
滔哥。。。。   
这个回复没附件 然后我也没权限上传图片。。
没分就没分了吧  图片这事麻烦滔哥指点一 ...

麻烦滔哥指点
回复 使用道具 举报
本帖最后由 fxwb2005 于 2014-8-14 15:48 编辑

复制代码
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;

  3. public class Test2 {

  4.         public void prin() throws Exception {
  5.                 BufferedReader input = new BufferedReader(new InputStreamReader(
  6.                                 System.in));
  7.                 System.out.println("请输入三个数字,以一个空格隔开:");
  8.                 String nums = input.readLine();
  9.                 int num = 0;
  10.                 for (int i = 0; i < nums.length(); i++) {
  11.                         if (nums.charAt(i) != ' ') {
  12.                                 if (Integer.parseInt(nums.charAt(i) + "") <= 40
  13.                                                 && Integer.parseInt(nums.charAt(i) + "") >= -40) {
  14.                                         num += Integer.parseInt(nums.charAt(i) + "");
  15.                                 }
  16.                         }

  17.                 }
  18.                 System.out.println(num);

  19.         }

  20.         public static void main(String[] args) throws Exception {
  21.                 Test2 test = new Test2();
  22.                 test.prin();
  23.         }

  24. }
复制代码




评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报

好了。。。{:3_47:}
回复 使用道具 举报
  1. import java.util.Scanner;
  2. class Test1
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                
  7.                 Scanner input = new Scanner(System.in);
  8.                 short a = input.nextShort();
  9.                 short b = input.nextShort();
  10.                 short c = input.nextShort();
  11.                 System.out.println(a+b+c);               
  12.                
  13.         }
  14. }
复制代码
谢谢滔哥指点   已经重新提交

`{[`NW]T1[(WX}Z3Z$ZDJTI.jpg (23.87 KB, 下载次数: 112)

`{[`NW]T1[(WX}Z3Z$ZDJTI.jpg
回复 使用道具 举报
程序代码:

  1. import java.util.*;
  2. public class Test {
  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 int a  = sc.nextInt();
  6.                 int b  = sc.nextInt();
  7.                 int c  = sc.nextInt();
  8.                 int num = a+b+c;
  9.                 System.out.println(a+"  "+b+"   "+c);
  10.                 System.out.println(num);
  11.         }
  12. }
复制代码


运行图
现在还没学到IO流,,只学了Scanner类

a.jpg (10.51 KB, 下载次数: 89)

a.jpg
回复 使用道具 举报
liqiaohui 发表于 2014-8-14 16:06
谢谢滔哥指点   已经重新提交

乔巴,nextShort()方法多接受的短整数的取值范围是多少??你知道吗
回复 使用道具 举报
茄子 发表于 2014-8-14 17:04
乔巴,nextShort()方法多接受的短整数的取值范围是多少??你知道吗

nextShort()返回的是一个short值  接收的也是short值  short取值范围是 -2的15次方到2的15次方-1   -3万多到3万多。。。。   这道题我等于没考虑取值范围
回复 使用道具 举报
liqiaohui 发表于 2014-8-14 18:17
nextShort()返回的是一个short值  接收的也是short值  short取值范围是 -2的15次方到2的15次方-1   -3 ...

诺,明白了!!!谢谢
回复 使用道具 举报
本帖最后由 liqiaohui 于 2014-8-14 18:58 编辑

感谢滔哥和茄子的指点  我只顾求值而忽略了取值范围  
修正了编码  求大神再次指点
  1. import java.util.Scanner;
  2. class Test1
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 int a=0,b=0,c=0;
  7.                 Scanner input = new Scanner(System.in);
  8.                 a = input.nextInt();
  9.                 b = input.nextInt();
  10.                 c = input.nextInt();
  11.                 if(a>=-40&&a<=40&&b>=-40&&b<=40&&c>=-40&&c<=40)
  12.                         System.out.println(a+b+c);
  13.                 else
  14.                         System.out.println("输入的数据不在范围内!");               
  15.         }
  16. }
复制代码




评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报
import java.util.Scanner;
/*
* 输入为一行,包括了用空格分隔的三个数据A、B、C(数据范围均在-40~40之间)
* 输出为一行,为“A+B+C”的计算结果。
*
* */

public class Text1 {
        public static void main(String[] args) {
                           int a,b,c;
                                  
                       Scanner in = new Scanner(System.in);     //输入流。
       
                            a = in.nextInt();
                                b = in.nextInt();
                                c = in.nextInt();
                                if(a>=-40&&a<=40 & b>=-40&&b<=40 & c>=-40&&c<=40)
                                        System.out.println(a+b+c);
                            else
                                    System.out.println("输入的数据超出范围内");
        }
}

QQ图片20140814220215.jpg (35.67 KB, 下载次数: 94)

运行截图

运行截图

评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报
滔哥辛苦  必须顶起!

QQ截图20140815103901.png (139.72 KB, 下载次数: 109)

QQ截图20140815103901.png
回复 使用道具 举报
本帖最后由 ⋛⋌⋚JEEP 于 2014-8-15 10:44 编辑
  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;

  3. public class Test {
  4.         
  5.         public static void main(String[] args) {
  6.                 System.out.println("请输入按空格分隔的三个数:");
  7.                 Scanner sca = new Scanner(new BufferedInputStream(System.in));//键盘录入
  8.                
  9.                 //输入int型整数a,b,c
  10.                 int a = sca.nextInt();
  11.                 int b = sca.nextInt();
  12.                 int c = sca.nextInt();
  13.                 client(a,b,c);
  14.         }
  15.         
  16.         //定义一个范围[-40,40]的求和函数
  17.         private static void client(int a, int b, int c) {
  18.                 int sum = 0;
  19.                 int[] arrs = new int[]{a,b,c};//定义一个存储a,b,c的数组
  20.                
  21.                 for(int arr:arrs) {//遍历
  22.                         if(arr>=-40 &&arr<=40)
  23.                                         sum+=arr; //累加求和
  24.                         else {
  25.                                 System.out.println("您所输入的范围有误,请重新输入!");
  26.                                 sum=0;//只要有有一个数不满足条件,sum清零
  27.                                 break;
  28.                         }        
  29.                 }
  30.                 if(sum!=0)
  31.                         System.out.println(sum);
  32.         }
  33. }
复制代码

练习1.jpg (31.67 KB, 下载次数: 106)

数据范围外结果,如图

数据范围外结果,如图

练习2.jpg (7.88 KB, 下载次数: 92)

数据范围内结果,如图

数据范围内结果,如图

评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报
import java.util.*;
/** 数据范围越界异常处理*/
class ShuzhiFanWeiException extends Exception
{
        ShuzhiFanWeiException(String msg)
        {
                super(msg);
        }
}

class Method
{
        public static int[]  Scanf() throws ShuzhiFanWeiException,InputMismatchException  //数值输入方法
        {
                int arr[] = new int[3];
                Scanner input = new Scanner(System.in);
                for(int i=0;i<3;i++)
                {
                        
                        try
                        {
                                arr=input.nextInt();
                        }
                        catch (InputMismatchException e)
                        {
                                throw new InputMismatchException("不能输入非整数");
                        }
                        if(arr>40 || arr<-40)
                        {
                                throw new ShuzhiFanWeiException("数据超出给定的范围");
                        }
                }
                return arr;
        }
        public static int Add(int[] arr) //数值相加方法
        {
                int sum=0;
                for(int i=0;i<3;i++)
                {
                        sum+=arr;
                }
                return sum;
        }        
}
class TestDemo
{
        public static void main(String[] args)
        {
                int arr[] = new int[3];
                boolean state = true;
                int sum=0;
                while(state)
                        try
                        {
                                System.out.println("请输入三个-40至40的整数");
                                arr=Method.Scanf();
                                sum=Method.Add(arr);
                                state=false;
                        }
                        catch (InputMismatchException e)
                        {
                                System.out.println(e.getMessage());
                        }
                        catch (ShuzhiFanWeiException e)
                        {
                                System.out.println(e.getMessage());
                        }
                System.out.println(sum);
        }
}

QQ图片20140815144638.jpg (61.25 KB, 下载次数: 88)

QQ图片20140815144638.jpg

评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报
1234下一页
您需要登录后才可以回帖 登录 | 加入黑马