创建一个java程序,关于有switch循环的

来源:百度知道 编辑:UC知道 时间:2024/06/16 18:34:21
创建一个程序,用于将两个整数值和一个运算符存储在相应的变量中,并执行必要的运算(使用switch语句)参考提示:a.创建一个类并定义main方法。b.使main方法的参数来获得用户通过命令行参数传递给main的数据。c.使用switch进行必要的判断和计算。d.输出计算结果。
初学java,麻烦高手指点哈呢,非常感谢!

帮你写一个睡觉,你等着 。我开台式机……你等等……笔记本没Eclipse……

---------------------------------------

import java.util.Scanner;

public class TestSwitch {
public static void main(String[] args) {
System.out.print("输入运算式(例如5+2):");
Scanner checkIn = new Scanner(System.in);
String strCheckIn=checkIn.next();
int a=Integer.parseInt(strCheckIn.substring(0, 1));
int b=Integer.parseInt(strCheckIn.substring(2));
char c=strCheckIn.charAt(1);
switch (c){
case '+':System.out.println(a+b);break;
case '-':System.out.println(a-b);break;
case '*':System.out.println(a*b);break;
case '/':System.out.println(a/b);break;
default:System.out.println("输入错误!格式:3-1");

}

}
}

自己研究吧,晚安

就是简单的加减乘除呗??

需要指出的是:switch 是属于分支结构,而不是一个循环。

路过~