编写一段程序实现如果输入的一组数中含非整数数字,输出数字相加的和以及"attention"字符,如果全部是数字便输出数字的和。

程序1:

package mian;
import java.util.Scanner;
public class mian { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int s=0;
String str=null;
while(sc.hasNext()){
if(sc.hasNextInt()){//判断是否是数字
s+=sc.nextInt(); }else{
str=sc.next();//不写这句话会导致只会加非数字前数字的和,必须将这个非数字字符串吸收了
} }
System.out.println(s);
if(str!=null){
System.out.println("attention");
}sc.close();
} }

程序2:

import java.util.Scanner;
public class Main{
//return -1: -10(带负号)
//return 1: +10(带正号)
//return 0 非数字字符串
//return 2: 10(不带正负号) public static int checkNum(String str){//判断是否是数字串
if(str.charAt(0)=='-'){
for(int i=1;i<str.length();i++){
if(str.charAt(i)-'0'>9||str.charAt(i)-'0'<0){
return 0;
}
}return -1; }else if(str.charAt(0)=='+'){
for(int i=1;i<str.length();i++){
if(str.charAt(i)-'0'>9||str.charAt(i)-'0'<0){
return 0;
}
}return 1;
}else{
for(int i=0;i<str.length();i++){
if(str.charAt(i)-'0'>9||str.charAt(i)-'0'<0){
return 0;
}
}return 2; } }
public static void main(String []args){
int flag=0,s=0,sum=0;
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){ String a=sc.next();
if(checkNum(a) == 1){
for(int i=1;i<a.length();i++){
s=s*10+a.charAt(i)-'0';
}sum+=s;s=0;
}else if(checkNum(a) == -1){
for(int i=1;i<a.length();i++){
s=s*10+a.charAt(i)-'0';
}sum+=-s;s=0; }else if(checkNum(a) == 2){
for(int i=0;i<a.length();i++){
s=s*10+a.charAt(i)-'0';
}sum+=s;s=0; }else{
flag=1;
//continue;
}
}
if(flag==0){
System.out.println(sum);
}else{
System.out.println(sum);
System.out.println("attention");
} sc.close(); } }

java关于hasNext()的更多相关文章

  1. Java使用hasNext()输入不定长数组

    1 Scanner scan = new Scanner(System.in); 2 String s = scan.nextLine(); 3 Scanner sc = new Scanner(s) ...

  2. Java的23种设计模式 <二>

    1.单例模式(Singleton Pattern) 定义:Ensure a class has only one instance, and provide a global point of acc ...

  3. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  4. poj 1001 求高精度幂(Java, BigDecimal, pow, hasNext, stripTrailingZeros, toPlainString)

    求高精度幂 Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 180325   Accepted: 43460 Descripti ...

  5. java.util.Scanner中hasNext()方法和next()方法的区别

    先说结论: 两者均根据空格划分数据 两者在没有数据输入时均会等待输入 next()方法会将空格划分的数据依次输出,运行一次,输出一个 hasNext()方法会跟着next()方法移动,当前数据不为空, ...

  6. Java 常用类库一,main方法传参String[] args;获取输入Scanner ;hasNext();hasNextInt()

    1. main方法传参 package com.zmd.common_class_libraries; /** 给mian方法传参测试 */ public class MainArgsTest { p ...

  7. java Scanner中的hasNext()方法

    hasNext()方法判断输入(文件.字符串.键盘等输入流)是否还有下一个输入项,若有,返回true,反之false. Scanner sc = new Scanner(new File(" ...

  8. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  9. java基础_集合List与Set接口

    List接口继承了Collection的方法  当然也有自己特有的方法向指定位置添加元素   add(索引,添加的元素); 移除指定索引的元素   remove(索引) 修改指定索引的元素   set ...

随机推荐

  1. numpy中的max()函数

    1.ndarray.max([int axis]) 函数功能:求ndarray中指定维度的最大值,默认求所有值的最大值. axis=0:求各column的最大值 axis=1:求各row的最大值

  2. 如何查看Ubuntu系统版本

    在Ubuntu终端,常用以下几种方式查看系统版本: 一.uname -a 二.cat /etc/issue 三.cat /etc/lsb-release 四.lsb_release -a 五.cat ...

  3. 压力测试-apachebench

    压力测试-apachebench 1.      压力测试 压力测试的概念\定义: 性能测试Performance Test :是指通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各项 ...

  4. 无线客户端掉线(Disassociate and DeleteReason)

  5. LVS负载均衡软件使用及(LVS简介、三种工作模式、十种调度算法)

    一.LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案, ...

  6. SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd0 in position 2: invalid continuation byte

    [root@hostuser src]# python3 subprocess_popen.py File "subprocess_popen.py", line 23Syntax ...

  7. 8.1.1默认的map函数、reduce函数、分区函数

    1.1.1         默认的map函数和reduce函数 (1)Maper和Reuducer默认类 如果没有指定maper类和reduce类,则会用默认的Maper和Reuducer类去处理数据 ...

  8. c#DDOS代码

    //在工程属性中设置"允许不安全代码"为true ?using System; using System.Net; using System.Net.Sockets; using ...

  9. 【已解决】iOS11使用MJRefresh上拉加载结束tableView闪动、跳动的问题

    更新提示: [2018年11月20日更新] 经过放置在项目中运行发现,如果在快速滚动tableview的时候会在下面这行代码中崩溃(慢慢的滚动是没关系的-): CGFloat cellHeight = ...

  10. 执行脚本,且以脚本名保存log

    !/bin/bash path="/sys/devices/platform/soc/fd880000.i2c-pld/i2c-0/i2c-4/i2c-15/15-0060" f_ ...