Topic: [新手求助]关于switch的用法

  Print this page

1.[新手求助]关于switch的用法 Copy to clipboard
Posted by: nonamexz
Posted on: 2005-09-14 14:36

现在在自学JAVA,在网上找了本<<21天JAVA 2 自学通>>,已经看到第五天了,在完成书上布置的作业时我遇到了一些问题.
题目如下:

创建一个类,它读取从最开始的10个数字(one到ten)中的单词,并把它们转换成简单的long整数.使用switch语句来进行转换,并用命令行参数来给出要转换的单词.
(题目在书中第99页)

1 class en2num
2 {
3 public static void main(String[] en)
4 {
5 int num;
6 if(en.length<=0)
7 {
8 System.out.println("缺少必要参数,请输入one到ten中的任一单词,形如:");
9 System.out.println("java en2num one");
10 }
11 else
12 {
13 switch(en[0])
14 {
15 case "one":
16 num=1;
17 case "two":
18 num=2;
19 case "three":
20 num=3;
21   ..........        
22 }
23 }
24 }
25 }


当我写到这里时,Eclipse开始报错了,出错位置是在第13行,出错提示:"case常量必须是char,byte,short或int,而不是String".
写到这里我真不知道应该怎么做了,如何才能让case的值为char,byte,short或int呢?

刚学编程,问的问题可能会很傻,请大家不要见笑.

2.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-09-14 15:18

为什么非要用switch?
switch固然在很多场合比那种if...else形式的语句有效率
但是,我们也没必要一定要强行在你举的例子里面用switch吧。
Smile

3.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: nonamexz
Posted on: 2005-09-14 16:15

书上要求用switch来进行处理的,我也知道这个问题用if-else来做的话会容易很多,但我也想知道用switch应该怎么去做,能否实现?

4.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: why
Posted on: 2005-09-14 19:43

不能就是不能
如果不是一定要完成的話,就放棄這书上的要求
否則就當智力遊戲,用multiple case's:
只要讀第一個字母,有四個case's可以確定;再讀第二個字母,就搞定了
one
two three
four five
six seven
eight
nine
ten
相信閣下懂怎樣編寫吧.

注意,case 與 case 之間要有 break;

5.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: nonamexz
Posted on: 2005-09-14 21:50

谢谢Why的点拔
呵呵,我也知道这个用if做会容易很多
我只想明白,如果不惜一切代价,是否真的可以用switch来实现Smile

6.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: why
Posted on: 2005-09-14 22:43

nonamexz wrote:
谢谢Why的点拔
呵呵,我也知道这个用if做会容易很多
我只想明白,如果不惜一切代价,是否真的可以用switch来实现Smile

Now you know it's possible.
Please post your solution here when it's done.

Oops..
> 不能就是不能
What I meant is 不能让case的值为String

> 再讀第二個字母,就搞定了
should read
再讀第二個字母,use another switch-case,就搞定了

7.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: nonamexz
Posted on: 2005-09-15 12:09

why,我已经把这道题目做出来了,这是我有史以来写过的最长的一个java程序啦.
Smile

class en2num
{
  public static void main(String[] en)  {
    
    long num=0;
    char charEn;    
    if(en.length<=0)
    {
      System.out.println("缺少必要参数,请输入one到ten中的任一单词,形如:");
      System.out.println("java en2num one");
    }
    else
    {
      charEn=en[0].charAt(0);
      switch(charEn)
      {
        case 'o':
          num=1;
          break;
        case 't':
          switch(en[0].charAt(1))
          {
            case 'w':
              num=2;
              break;
            case 'h':
              num=3;
              break;
            case 'e':
              num=10;
              break;
          }
          break;
        case 'f':
          switch (en[0].charAt(1))
          {
          case 'o':
            num=4;
            break;

          case 'i':
            num=5;
            break;
          }
          break;
        case 's':
          switch (en[0].charAt(1))
          {
          case 'i':
            num=6;
            break;

          case 'e':
            num=7;
            break;
          }
          break;
        case 'e':
          num=8;
          break;
        case 'n':
          num=9;
          break;        
      }
      System.out.println(num);
    }
  }
}

8.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-09-15 15:00

通常情况下,类名首字母大写哈

9.Re:[新手求助]关于switch的用法 [Re: bluecrystal] Copy to clipboard
Posted by: nonamexz
Posted on: 2005-09-15 17:14

bluecrystal wrote:
通常情况下,类名首字母大写哈


嗯,记下来了,谢谢.

10.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: beyond1984
Posted on: 2005-09-21 16:26

Don't u think it so boring???
you'd better use class to encapsulate the input string as follows:

public class B {
public B() {
}
public static final int one = 1; //*
public static final int two = 2; //*
public static final int three = 3; //*

public static void main(String [] args){
Enum e = new Enum("two"); //Enum e = new Enum(args[0]); all right.
switch (e.getVal()){
case one:
System.out.println(e.getVal());
break;
case two:
System.out.println(e.getVal());
break;
case three:
System.out.println(e.getVal());
break;
default:
break;
}
}
}

class Enum{
private int val = 0;
public Enum(String str){
if(str == "one"){ //*
val = 1;
}
if(str == "two"){ //*
val = 2;
}
if(str == "three"){ //*
val = 3;
}
}
public int getVal(){
return val;
}
}

But this is not a good way to use swich, you must be sure there should be a 'switch' rather than else.Anyway what i wrote is boring.too,since u should maintain the place with tag *, as to make them synchronized.

11.Re:[新手求助]关于switch的用法 [Re: nonamexz] Copy to clipboard
Posted by: beyond1984
Posted on: 2005-09-21 16:44


public class B {
public B() {
}
public static void main(String [] args){
Enum e = new Enum("two");
switch (e.getVal()){
case Enum.one:
System.out.println(e.getVal());
break;
case Enum.two:
System.out.println(e.getVal());
break;
case Enum.three:
System.out.println(e.getVal());
break;
default:
break;
}
}
}

class Enum{
private int val = 0;
public static final int one = 1;
public static final int two = 2;
public static final int three = 3;
public Enum(String str){
if(str == "one"){
val = one;
}
if(str == "two"){
val = two;
}
if(str == "three"){
val = three;
}
}
public int getVal(){
return val;
}
}
//Oh,sth changed,sth improved, right? U can only maintain the class Enum and
//make your code clear.Good luck.

12.Re:[新手求助]关于switch的用法 [Re: beyond1984] Copy to clipboard
Posted by: why
Posted on: 2005-09-21 19:07

beyond1984 wrote:

...
public Enum(String str){
if(str == "one"){
val = one;
}
if(str == "two"){

Sad
use equals(), not ==


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923