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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

代码+答案运行结果截图。
回复 使用道具 举报
  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.                 if(a>=-40&&a<=40)
  12.                 {
  13.                         if(b>=-40&&b<=40)
  14.                         {
  15.                                 if(c>=-40&&c<=40)
  16.                                 {
  17.                                         System.out.println(a+b+c);
  18.                                 }
  19.                                 else
  20.                                 {System.out.println("C的值超出范围");}
  21.                         }
  22.                         else
  23.                         {System.out.println("B的值超出范围");}
  24.                 }
  25.                 else
  26.                 {System.out.println("A的值超出范围");}
  27.                
  28.         }
  29. }
复制代码
滔哥滔哥,,,给个机会,刚刚看到,临时又改了下

QQ截图20140815192548.png (43.98 KB, 下载次数: 33)

QQ截图20140815192548.png
回复 使用道具 举报
不淡定,小学生 发表于 2014-8-14 15:06
import java.util.Scanner;
class Abc
{

好了。涛哥
回复 使用道具 举报
菜鸟一号 发表于 2014-8-15 14:51
import java.util.*;
/** 数据范围越界异常处理*/
class ShuzhiFanWeiException extends Exception

谢谢滔哥
回复 使用道具 举报
本帖最后由 依然超级赛亚人 于 2014-8-16 09:51 编辑

  1. /*
  2. 输入为一行,包括了用空格分隔的三个数据A、B、C(数据范围均在-40~40之间)。
  3. 输出为一行,为“A+B+C”的计算结果。
  4. */
  5. class RandomTest02
  6. {
  7.         public static void main(String[] args)
  8.         {
  9.                 int[] array = new int[3];//定义一个长度为3的数组。
  10.                 System.out.print("参加加法运算的三个数为:");
  11.                 for(int i=0;i<3;i++){
  12.                         int x = (int)(Math.random()*2+1);//产生随机数1或2.
  13.                         int y = (int)Math.pow(-1,x);//产生随机数1或-1.
  14.                         array[i]=(int)(Math.random()*40*y);//产生区间(-40,40)内的随机数。
  15.                         //System.out.println(y);
  16.                         System.out.print(array[i]+" ");
  17.                 }
  18.                 System.out.println();
  19.                
  20.                 int sum=0;
  21.                 for(int i=0;i<3;i++){
  22.                         sum+=array[i];
  23.                 }
  24.                 System.out.print("运算后的结果为:"+sum);
  25.                
  26.                
  27.                
  28.         }
  29. }
复制代码
结果图:


回复 使用道具 举报

  1. <P>import java.util.*;
  2. class PlusTest
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner s=new Scanner(System.in);
  7. int[] arr=new int[3];
  8. int sum=0;
  9. System.out.println("请输入三个-40到40之间的整数,以空格分开");
  10. for (int i=0;i<3 ;i++ )
  11. {

  12. try
  13. {
  14. arr[i]=s.nextInt();
  15. if (arr[i]>=-40 && arr[i]<=40)
  16. {
  17. sum+=arr[i];
  18. }
  19. else
  20. {
  21. System.out.println("数字"+arr[i]+"超出范围,请重新输入三个数字:");
  22. i=0;</P>
  23. <P>sum=0;
  24. }
  25. }
  26. catch (InputMismatchException e)
  27. {
  28. throw new InputMismatchException("不能输入非整数");
  29. }
  30. }

  31. System.out.println("三个数的和为:"+sum);
  32. }
  33. }</P>
复制代码
怎么还上传不了图片,放在这里http://pan.baidu.com/s/1gdFxiWN楼主好心给个分激动一下吧
回复 使用道具 举报
import java.util.Scanner;

public class Test1 {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);//
                System.out.println("请输入三个数字:");
                int a = sc.nextInt();
                int b = sc.nextInt();
                int c = sc.nextInt();

                if (a >= -40 && a <= 40 && b >= -40 && b <= 40 && c >= -40 && c <= 40) {
                        System.out.println(a + b + c);
                } else
                        System.out.println("你输入的数据不在范围内");
        }
}




回复 使用道具 举报
本帖最后由 lfs454766767 于 2014-8-19 13:47 编辑
  1. public class SumTest {
  2.         public static void main(String[] args) {
  3.                 Scanner in = new Scanner(System.in);
  4.                 int a,b,c;
  5.                 System.out.println("------请输入-40到40之间的三个数字:-------");
  6.                 a = in.nextInt();
  7.                 b = in.nextInt();
  8.                 c = in.nextInt();
  9.                 while(a<-40||a>40||b<-40||b>40||c<-40||c>40) {
  10.                         System.out.println("您输入的三个值中有超出范围的值,请重新输入:");
  11.                         a = in.nextInt();
  12.                         b = in.nextInt();
  13.                         c = in.nextInt();
  14.                 }
  15.                 System.out.println(a+b+c);
  16.         }
  17. }
复制代码

QQ截图20140819133807.gif (2.57 KB, 下载次数: 41)

QQ截图20140819133807.gif
回复 使用道具 举报
  1. public class SumTest {
  2.         public static void main(String[] args) {
  3.                 Scanner in = new Scanner(System.in);
  4.                 int a,b,c;
  5.                 System.out.println("------请输入-40到40之间的三个数字:-------");
  6.                 a = in.nextInt();
  7.                 b = in.nextInt();
  8.                 c = in.nextInt();
  9.                 while(a<-40||a>40||b<-40||b>40||c<-40||c>40) {
  10.                         System.out.println("您输入的三个值中有超出范围的值,请重新输入:");
  11.                         a = in.nextInt();
  12.                         b = in.nextInt();
  13.                         c = in.nextInt();
  14.                 }
  15.                 System.out.println(a+b+c);
  16.         }
  17. }
复制代码
回复 使用道具 举报
本帖最后由 lfs454766767 于 2014-8-19 13:51 编辑

public class SumTest {
        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                int a,b,c;
                System.out.println("------请输入-40到40之间的三个数字:-------");
                a = in.nextInt();
                b = in.nextInt();
                c = in.nextInt();
                while(a<-40||a>40||b<-40||b>40||c<-40||c>40) {
                        System.out.println("您输入的三个值中有超出范围的值,请重新输入:");
                        a = in.nextInt();
                        b = in.nextInt();
                        c = in.nextInt();
                }
                System.out.println(a+b+c);
        }
}

回复 使用道具 举报
本帖最后由 李天富 于 2014-8-19 22:30 编辑

import java.util.Scanner;
class blys
{
        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();
                while(a<-40||a>40||b<-40||b>40||c<-40||c>40)
                        {
                                System.out.println("超出范围,请重新输入");
                                a=input.nextInt();
                                b=input.nextInt();
                                c=input.nextInt();
                        }
               
               
                System.out.println(a+b+c);
        }
}

~X%{A@RG%8TIY12513%0)8G.jpg (30.19 KB, 下载次数: 41)

~X%{A@RG%8TIY12513%0)8G.jpg
回复 使用道具 举报
内容好少啊
回复 使用道具 举报

终于看到一个像样的
回复 使用道具 举报
还没有看到
回复 使用道具 举报
好像写的越复杂越给分啊...
回复 使用道具 举报
import java.util.Scanner;

public class Test
{
        public static void main(String[] args)
        {
                Scanner input = new Scanner(System.in);
                int x = input.nextInt();
                int y = input.nextInt();
                int z = input.nextInt();
                int sum;
                sum = x + y + z;
                System.out.println(sum);
        }
}

QQ截图20140925124246.jpg (79.2 KB, 下载次数: 40)

QQ截图20140925124246.jpg
回复 使用道具 举报
没有考虑数值取值范围,代码已修改
import java.util.Scanner;

public class Test
{
        public static void main(String[] args)
        {
                int x,y,z,sum;
                Scanner input = new Scanner(System.in);
                x = input.nextInt();
                y = input.nextInt();
                z = input.nextInt();
                if((x>=-40&&x<=40)&&(y>=-40&&y<=40)&&(z>=-40&&z<=40))
                {
                        sum = x + y + z;
                        System.out.println(sum);
                }
                else
                        System.out.println("您输入的数值超出取值范围!");
        }
}

QQ截图20140925125916.jpg (22.23 KB, 下载次数: 34)

QQ截图20140925125916.jpg
回复 使用道具 举报
import java.util.Scanner;
class  TestExam
{
        public static void main(String[] args)
        {
                //创建控制台接收对象
                Scanner input = new Scanner(System.in);
                //键盘输入三个数据
                int A = input.nextInt();       
                int B = input.nextInt();
                int C = input.nextInt();
                //求和
                int result = A+B+C;
                //输出结果
                System.out.println("A+B+C的值:"+result);
        }
}
运行结果:E:\result.bmp
回复 使用道具 举报
import java.util.Scanner;
class  TestExam
{
        public static void main(String[] args)
        {
                //创建控制台接收对象
                Scanner input = new Scanner(System.in);
                //键盘输入三个数据
                int A = input.nextInt();       
                int B = input.nextInt();
                int C = input.nextInt();
                //求和
                int result = A+B+C;
                //输出结果
                System.out.println("A+B+C的值:"+result);
        }
}


回复 使用道具 举报
import java.util.Scanner;


public class Sum {
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                System.out.println("请输入范围是-40到40之间的三个数,中间用空格分开");
                Scanner sc = new Scanner(System.in);
                int x = sc.nextInt();
                int y = sc.nextInt();
                int z = sc.nextInt();
                add(x,y,z);

        }
        public static void  add(int x,int y,int z) {
                if((x>-40&&x<40)&&(y>-40&&x<40)&&(z>-40&&z<40)){
                        int sum = x+y+z;
                        System.out.println(sum);
                }else{
                        System.out.println("超出取值范围");
                }
        }
       

}

1.PNG (2.17 KB, 下载次数: 35)

1.PNG
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马