今天在做一个用三元运算符判断奇偶的小练习时遇到“the operation % is undefined for the argument type string,int”错误的小插曲

开始的程序是这样写的

1
2
3
4
5
6
7
8
9
10
11
package com.lixiyu;
import java.util.Scanner;
public class ParityCheck {
public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入一个整数:");
    String line=sc.nextLine();
   String flag=((line%2)==0)?"偶数":"奇数";
    System.out.println("这个数字是:"+flag);
}
}

这是我的写法,但它会提示无法确定类型String,int无法正常使用%的问题,要用%得是整型嘛。所以后来google看到国外论坛有遇到类型问题,他给的解决方法是:Assuming what the user inputs is really a number, you can use Integer.parseInt(weight) and compare that.

意思也就是要让line转换为整型的数,即用到Integer.parseInt()即可解决故改一下下面为

String flag=(Integer.parseInt(line)%2==0)?"偶数":"奇数"; 可以正常运行编译

自己写的正常运行的代码:

1
2
3
4
5
6
7
8
9
10
11
package com.lixiyu;
import java.util.Scanner;
public class ParityCheck {
public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入一个整数:");
    String line=sc.nextLine();
   String flag=(Integer.parseInt(line)%2==0)?"偶数":"奇数";
    System.out.println("这个数字是:"+flag);
}
}

后来看了看书本上给出的参考答案:

1
2
3
4
5
6
7
8
9
10
import java.util.Scanner;
public class ParityCheck {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);// 创建输入流扫描器
        System.out.println("请输入一个整数:");
        long number = scan.nextLong();// 获取用户输入的整数
        String check = (number % 2 == 0) ? "这个数字是:偶数" "这个数字是:奇数";
        System.out.println(check);
    }
}

它书本上面用到的是long(长整型)从获取用户输入数据上就已经控制了整数输入。貌似会更方便点。

路还长,继续学习。

本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1302769,如需转载请自行联系原作者

如何解决提示the operation % is undefined for the argument type string,int的错误相关推荐

  1. 解决Visual Studio 2008 下,打开.dbml(LINQ) 文件时,提示The operation could not be completed. 的问题。...

    环境:XP sp3, Visual Studio2008 sp1 错误现象:打开.dbml(LINQ) 文件,提示 "The operation could not be completed ...

  2. root用户被提示:Operation not permitted

    情景还原 笔者今天在使用root用户删除服务器上的一个文件时,被系统告知:Operation not permitted.甚至不能使用chmod给777权限. 一开始我是很懵逼的状态,还有root用户 ...

  3. php提示 Notice: Use of undefined constant name - assumed

    http://www.php230.com/1410834481.html 我们知道php在数组中写变量有二几种方法,我们出现这种提示就是你写成了[name]这种所以会有Notice: Use of ...

  4. svn提交代码提示Previous operation has not finished; run ‘cleanup‘ if it was interrupted

    场景: 最近提交代码提示Previous operation has not finished; run 'cleanup' if it was interrupted,想直接覆盖我本地代码都不成功. ...

  5. svn 更新提示“ Previous operation has not fnished ; run ‘ deanup‘if t was interrupted”

    三步解决 svn 更新提示 " Previous operation has not fnished ; run ' deanup'if t was interrupted" 第一 ...

  6. ldd -r xxx.so命令的重要作用------见招拆招地解决缺库问题(undefined symbol)

    ldd -r xxx.so命令的重要作用------见招拆招地解决缺库问题(undefined symbol) 参考文章: (1)ldd -r xxx.so命令的重要作用------见招拆招地解决缺库 ...

  7. 水晶报表,解决——提示“您请求的报表需要更多信息.”

    水晶报表,解决--提示"您请求的报表需要更多信息." string sql = "Select * from Orders";string DBConfig_s ...

  8. vue页面渲染(请求返回的后端)data,文本换行与显示bug 的分析与解决提示

    vue页面渲染(请求返回的后端)data,文本换行与显示bug 的分析与解决提示 问题说明: vue项目,后台一经修改文案[介绍内容]后,前端接收显示实时数据,却无法换行? 原因分析: 后台页面的表单 ...

  9. win7 、2008 提示Error 1606 Could Not Access Network Location %SystemDrive%/inetpub/wwwroot/ 的错误解决方法

    win7 .2008 提示Error 1606 Could Not Access Network Location %SystemDrive%/inetpub/wwwroot/ 的错误解决方法 参考文 ...

最新文章

  1. 使用Web.Config Transformation配置灵活的配置文件
  2. 128条形码计算,利用Code 128字体实现条码打印
  3. CSS3无前缀脚本prefixfree.js与Animatable使用
  4. Qt学习(一):两个独立窗口的信号通信
  5. ES6-17 class与对象
  6. 通过听力写代码?盲人程序员就是这样做的
  7. 只针对异常的情况才使用异常_如何以及何时使用异常
  8. 区块链入门与去中心化应用实战 之一 第3章 区块链技术核心原理实现
  9. Java泛型(8):自限定参数协变
  10. jquery 后代 children, find方法
  11. VHDL学习--分频器
  12. 如何提取PDF页面?3种拆分PDF的方法
  13. nat流量,业务流量_10种增加在线业务流量的方法
  14. 计算机桌面调音量的图标不见了,声音图标不见了,音量图标不见了怎么办?
  15. oracle修改查询结果,Oracle使用查询结果更新表的方法
  16. windbg分页机制实验
  17. 【编程题】【Scratch三级】2021.03 接苹果
  18. Microsoft Office for Mac 报错
  19. 计算机基础——5.2 图像与图形
  20. 京东集团副总裁李大学:像CEO一样思考 - Dancen的专栏 - 博客频道 - CSDN.NET

热门文章

  1. Python中使用requests和parsel爬取喜马拉雅电台音频
  2. Winform中实现设置ZedGraph的GraphPane的大小匹配ZedGraphControl
  3. 交互式计算机图形学总结:第三章 几何对象和变换
  4. 3、MySQL 8.0.20在Linux(centos 8)上搭建主从复制
  5. 企业联合体的形式_母公司是否可以用子公司资质进行投标,且不以联合体的形式?...
  6. 中原证券携手神策数据,筑就线上线下融合的数字化运营体系
  7. 倒计时 2 天 | 神策 2019 数据驱动大会即将开幕
  8. 上海证券携手神策数据,引领普惠金融创新升级
  9. react antd 动态表单
  10. iOS - PassData