我想不确定如何使用开关? [英] I want to undrestan how i can use switch?
本文介绍了我想不确定如何使用开关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package expnum;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numbers[];
int number;
int word;
String expression[]={"+" , "-" , "*" , "/"};
System.out.println("please enter 3 numbers:");
number=input.nextInt();
System.out.println("please enter 2 expression (+ , - ,* , /):");
word = input.nextInt();
// public void answer(int expression){}
class answer
{
// public static void answer (String[]args)
{
int expression=5;
switch(expression)
{
case 1: System.out.println(" + ");break;
case 2: System.out.println(" - ");break;
case 3: System.out.println(" * ");break;
case 4: System.out.println(" / ");break;
default: System.out.println("in valid");break;
}
}
}
{
}
}
是真的吗?
我想要一个给出数字和表达式(+-*/)的程序,然后给出结果.
is it true?
iwant a program that i give numbers and expression(+ - * / ) and the program give a result.
Is it useful?
推荐答案
那是您使用开关的方式,但是您所拥有的将不会做任何有用的事情你.
That is how you use a switch but what you have there isn''t going to do anything useful for you.
mf_arian写道:
mf_arian wrote:
我想要一个给出数字和表达式(+-*/)的程序,然后给出结果.
iwant a program that i give numbers and expression(+ - * / ) and the program give a result.
如果您将表达式表示为整数,那么您可以传递数字以切换您编写的代码块,但是如果您得到表达式(*,您是否会打扰您?) ,+ ...),那么您就可以将该表达式值传递给switch块.最后是用于表达式求值的代码..?
If u getting the expression as integer then u can pass the number to switch block you have written but u if u getting expression(*,+...)then u can pass that expression value the switch block.finally where''s the code for expression evaluation..?
这篇关于我想不确定如何使用开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文