Introduction to Java Programming编程题12.1<NumberFormatException>

Introduction to java programming Introduction to java programming 作者:Y.Daniel Liang 立即下载

运行结果:

br:javacode br$ javac NumberFormatException.java
br:javacode br$ java NumberFormatException 45 34
Please use java NumberFormatException operand1 operator operand2
java NumberFormatException 45x + 34
Wrong input 45x
java NumberFormatException 45 1 33
Wrong input: 1
java NumberFormatException 45 / 3
45 / 3 = 15.0

NumberFormatException.java (不使用异常处理)


class NumberFormatException {
    public static void main(String[] args) {
        if (args.length != 3) {
            System.out.println("Please use java NumberFormatException operand1 operator operand2");
            System.exit(1);
        }

        if (isDigital(args[0])) {
            if (args[1].length() == 1) {
                if (args[1].charAt(0) == '+' || args[1].charAt(0) == '-'
                        || args[1].charAt(0) == '*' || args[1].charAt(0) == '/') {
                    if (isDigital(args[2])) {
                        char operator = args[1].charAt(0);
                        double result = 0;
                        switch (operator) {
                            case '+':
                                result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
                                break;
                            case '-':
                                result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
                                break;
                            case '*':
                                result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
                                break;
                            case '/':
                                result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
                                break;
                        }
                        System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result);
                        System.exit(0);
                    }
                } else
                    System.out.println("Wrong input: " + args[1]);
            } else
                System.out.println("Wrong input: " + args[1]);
        }

        System.exit(1);
    }

    public static boolean isDigital(String args) {
        for (int i = 0; i < args.length(); i++)
            if (!Character.isDigit(args.charAt(i))) {
                System.out.println("Wrong input " + args);
                return false;
            }

        return true;
    }


}

NumberFormatException1.java (使用异常处理)

class NumberFormatException1 {
    public static void main(String[] args) {

      try {
        if (args.length != 3)
            throw new IllegalArgumentException ("Please use java NumberFormatExceptionX operand1 operator operand2");

        if (isDigital(args[0])) {
            if (args[1].length() == 1) {
                if (args[1].charAt(0) == '+' || args[1].charAt(0) == '-'
                        || args[1].charAt(0) == '*' || args[1].charAt(0) == '/') {
                    if (isDigital(args[2])) {
                        char operator = args[1].charAt(0);
                        double result = 0;
                        switch (operator) {
                            case '+':
                                result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
                                break;
                            case '-':
                                result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
                                break;
                            case '*':
                                result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
                                break;
                            case '/':
                                result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
                                break;
                        }
                        System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result);
                    }
                }
            }
        }
      }
      catch (IllegalArgumentException e) {
        System.out.println(e);
      }

    }

    public static boolean isDigital(String args) {
        for (int i = 0; i < args.length(); i++)
            if (!Character.isDigit(args.charAt(i)))
                return false;

        return true;
    }


}
Introduction-to-programming-with-java 立即下载

相关推荐

Introduction to Java Programming(10th Edition)课后练习答案

由 Luiz Arantes Sa编写,并不是随书配套的答案,可以作为参考

NumberFormatException异常【java】

【java】NumberFormatException异常 课本P426 T12.1 ( NumberFormatException异常)程序清单7-9是一个简单的命令行计算器。注意,如果某个操作数非数值,程序就会中止。编写一个程序,利用异常处理器来处理非数值操作数;然后编写另一个不使用异常处理器的程序,达到相同的目的。程序在退出之前应该显示一条消息,通知用户发生了操作数类型错误(参见图12-12 )。 源代码 package leiPackage; import java.util.*; public

lei20172017的博客 3811

Introduction to Java Programming answer 8【Java语言程序设计 第八版 8】官方答案答案

作者给的答案,应该是最权威的了,Introduction to Java Programming answer 8【Java语言程序设计 第八版 8】答案

第十二章第一题(NumberFormatException异常)(Numberformatexception exception)

第十二章第一题(NumberFormatException异常)(Numberformatexception exception) *12.1(NumberFormatException异常)程序清单7-9是一个简单的命令行计算器。注意,如果某个操作数非数值,程序就会终止。编写一个程序,利用异常处理器来处理非数值操作数;然后编写另一个不使用异常处理器的程序,达到同样的目的。程序在退出之前应该显示一条消息,通知用户发生了操作数类型错误。 *12.1(Numberformatexception excepti

jxh1025_的博客 5723

Introduction to Java Programming(8th edition)

This book is a brief version of Introduction to Java Programming, Comprehensive Version, 8E. This version is designed for an introductory programming course, commonly known as CS1. This version contains the first twenty chapters in the comprehensive version. This book uses the fundamentals-first approach and teaches programming concepts and techniques in a problem-driven way. The fundamentals-first approach introduces basic programming concepts and techniques before objects and classes. My own experience, confirmed by the experiences of many colleagues, demonstrates that new programmers in order to succeed must learn basic logic and fundamental programming techniques such as loops and stepwise refinement. The fundamental concepts and techniques of loops, methods, and arrays are the foundation for programming. Building the foundation prepares students to learn object-oriented programming, GUI, database, and Web programming. Problem-driven means focused on problem-solving rather than syntax. We make introductory programming interesting by using interesting problems. The central thread of this book is on solving problems. Appropriate syntax and library are introduced to support the writing of a program for solving the problems. To support teaching programming in a problemdriven way, the book provides a wide variety of problems at various levels of difficulty to motivate students. In order to appeal to students in all majors, the problems cover many application areas in math, science, business, financials, gaming, animation, and multimedia. 精美文字版pdf电子书。

Introduction to Java Programming, Comprehensive Version, 10th Edition

Introduction to Java Programming, Comprehensive Version, 10th Edition- Y. Daniel Liang(java语言程序设计第十版完整版,包含基础篇与进阶篇)完整英文原版;文字版;高清;无水印;加标签

Introduction to Java Programming

中文是《java语言程序设计基础篇》和《java语言程序设计进阶篇》,适合java初学者。v/:* {behavior:url(#default#VML);}o/:* {behavior:url(#default#VML);}w/:* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);}

blizzard_fengcoco的专栏 1523

Introduction to Java programming 8th

This comprehensive version covers fundamentals of programming object oriented programming GUI programming algorithms and data structures concurrency networking internationalization advanced GUI database and Web programming It is designed to prepare students to become proficient Java programmers A brief version Introduction to Java Programming Brief Version Eighth Edition is available for a first course on programming commonly known as CS1 The brief version contains the first 20 chapters of the comprehensive version ">This comprehensive version covers fundamentals of programming object oriented programming GUI programming algorithms and data structures concurrency networking internationalization advanced GUI database and Web programming It is designed to prepare students to become proficient Java progra [更多]

Intro to Java Programming(Liang.10th)--07

Chapter77.1 Introduction7.2 Array Basics7.2.1 Declaring Array Variables7.2.2 Creating ArraysNote7.2.3 Array Size and Default Values 7.1 Introduction A single array variable can reference a large colle...

Hello 878

Introduction to java programming 8th Edtion(彩色英文原版)

本书是Java语言的经典教材,中文版分为基础篇和进阶篇,主要介绍程序设计基础、面向对象编程、GUI程序设计、数据结构和算法、高级Java程序设计等内容。本书以示例讲解解决问题的技巧,提供大量的程序清单,每章配有大量复习题和编程练习题,帮助读者掌握编程技术,并应用所学技术解决实际应用开发中遇到的问题。您手中的这本是其中的基础篇,主要介绍了基本程序设计、语法结构、面向对象程序设计、继承和多态、异常处理和文本I/O、抽象类和接口等内容。本书可作为高等院校程序设计相关专业的基础教材,也可作为Java语言及编程开发爱好者的参考资料。 作者:(美国)粱勇(Y.Daniel Liang) 译者:戴开宇 梁勇(Y.Daniel Liang),现为阿姆斯特朗亚特兰大州立大学计算机科学系教授。之前曾是普度大学计算机科学系副教授。并两次获得普度大学杰出研究奖。他所编写的Java教程在美国大学Java课程中采用率极高。同时他还兼任Prentice Hall Java系列丛书的编辑。他是“Java Champion”荣誉得主,并在世界各地为在校学生和程序员做JAVA程序设计方法及技术方面的讲座。 戴开宇,复旦大学软件学院教师,工程硕士导师。中国计算机学会会员。博士毕业于上海交通大学计算机应用专业,2011~2012年在美国佛罗里达大学作访问学者。承担多门本科专业课程、通识教育课程以及工程硕士课程,这些课程被评为校精品课程,上海市重点建设课程,IBM—教育部精品课程等。

introduction to java programming

introduction to java programming

板凳——————————————————(昏鸦)Introduction to Java Programming

//Introduction to Java Programming //p30/603 2021年03月21日 星期日 13时46分12秒 import javax.swing.JOptionPane; import java.util.Scanner; public class hello{ /* public static void main(String[] args){ System.out.println(“Welcome to Java!”); } wannian07@wannian07-P

fengye207161的博客 403

Introduction to Java Programming AP Version 无水印pdf

Introduction to Java Programming AP Version 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

java程序设计英文_Java语言程序设计.基础篇(英文版.第6版)

Welcome to Introduction to Java Programming, Sixth Edition. This edition is a substantial' improvement on the previous edition in respect to clarity, content, presentation, code listings, and exercise...

weixin_36370128的博客 746

Introduction.to.Java.Programming

赏心悦目的Java教程,英文版。 英文彩色排版,阅读方便,清晰明了

Aras Innovator生命周期图实战:从配置到权限管理的全流程解析

本文详细解析了Aras Innovator生命周期图的全流程配置与权限管理,从基础概念到实战应用,涵盖状态设置、路径配置、权限控制等核心功能。通过实际案例和代码示例,帮助用户掌握如何高效管理数据生命周期,提升业务流程的规范性和效率。

ee345的博客 168

大一的java考试试题_华南理工大学大一JAVA复习题

《华南理工大学大一JAVA复习题》由会员分享,可在线阅读,更多相关《华南理工大学大一JAVA复习题(17页珍藏版)》请在人人文库网上搜索。1、2.1从输入对话框读入double型的华氏度,将其转换为摄氏度,并在消息对话框中显示结果。Celsius = (5/9)*(Fahrenheit-32)import javax.swing.JOptionPane;public class Exercise2...

weixin_33377571的博客 850

配置思科交换机冗余链路汇聚

本文对交换机端口汇聚配置进行说明

istrangeboy&begin zero的博客 6684
上一篇: Introduction to Java Programming编程题11.1<The triangle class>
下一篇: Introduction to Java Programming编程题12.2<InputMismatchException>
Okwarry
博客等级 码龄15年 21粉丝 111原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值