从StdIn读取字符

编程入门 行业动态 更新时间:2024-10-26 22:30:57
本文介绍了从StdIn读取字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public class New { public static void main(String[] args) { System.out.println("Calculator"); float a = StdIn.readFloat(); char sign = StdIn.readChar(); float b = StdIn.readFloat(); float c = 0; if (sign == '+') c = a+b; else if (sign == '-') c = a-b; else if (sign == 'x') c = a*b; else if (sign == '/') c = a/b; System.out.println(c); } }

代码,我试图做一个计算器,使用字符和float数据类型的StdIn输入。

I need some help with this bit of code, I'm attempting to make a calculator that takes StdIn input with both character and float data types.

推荐答案

Scanner class。

像这样:

import java.util.Scanner; public class New { public static void main(String[] args) { System.out.println("Calculator"); Scanner scanner = new Scanner(System.in); System.out.println("Enter Parameter "); System.out.print("a : "); float a = scanner.nextFloat(); System.out.print("+|-|*|/: "); String op = scanner.next(); System.out.print("b : "); float b = scanner.nextFloat(); float c = 0; switch (op) { case "+": c = a + b; break; case "-": c = a - b; break; case "*": c = a * b; break; case "/": c = a / b; break; default: System.out.println("Illegal operant"); } System.out.println("Result: " + c); } }

我添加了一个开关盒而不是else ifs。

I added a switch case instead of the else ifs.

更多推荐

从StdIn读取字符

本文发布于:2023-07-06 07:53:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   StdIn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!