开关中的字符 - java 7 [英] Char in a switch - java 7

查看:82
本文介绍了开关中的字符 - java 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



我哪里错了?



我很困惑因为我是char。



我的尝试:



  public   class  SwitchChar {

/ * *
* @param args命令行参数
* /

< span class =code-keyword> public static void main( String [] args){
// TODO代码应用程序逻辑这里
char i;
LOOP:
for (i = 0 ; i< 5 ; i ++){
System.out.println(( int )i);
switch (i ++){
case ' 0'
System.out.println(( int )一世);
System.out.println( A);
case 1
System.out.println(( INT )ⅰ);
System.out.println( B);
break LOOP;
case 2
System.out.println(( INT )ⅰ);
System.out.println( C);
break ;
case 3
System.out.println(( INT )ⅰ);
System.out.println( D);
break ;
case 4
System.out.println(( INT )ⅰ);
System.out.println( E);
case ' E'
System.out.println(( int )i);
System.out.println( F);
}
}
}

}





我不喜欢了解执行结果的步骤。


当i = 0时
- 没有选择的情况,我增加到1

时i = 1 - 中断循环所以从不执行







非常感谢!!! div class =h2_lin>解决方案

您确实理解零在整数形式和字符形式中不相同的常见事实。它们生成并检查的二进制文件不相同。字符是可切换的值,但它不会为您正在使用它的情况映射任何真值。



要解决此问题,请将其中一种类型更改为其他类型,

 for(i = 0; i< 5; i ++){
System.out.println((int)i);
switch(i ++){
case 0:
//这里的代码。
// ...



否则,您应该考虑使用字符值,并使用 Character.valueOf(i); 然后继续前进。看起来像这样,

 for(i = 0; i< 5; i ++){
//映射它。
char ch = Character.valueOf(i);
System.out.println(i); //不需要(int)int;它已经是一个int。
switch(ch ++){
case'0':
//这里代码...



ch ++ 在Java中是合法的,因为字符是表示Unicode字符的数值。增加它,为您提供下一个角色等等。如果这听起来不容易,请阅读指针算法。


当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java -application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


I have the below code:

Where do i wrong?

I am confused because i is char.

What I have tried:

public class SwitchChar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        char i;
        LOOP:
        for (i = 0; i < 5; i++) {
            System.out.println((int)i);
            switch (i++) {
                case '0':
                    System.out.println((int)i);
                    System.out.println("A");
                case 1:
                    System.out.println((int)i);
                    System.out.println("B");
                    break LOOP;
                case 2:
                    System.out.println((int)i);
                    System.out.println("C");
                    break;
                case 3:
                    System.out.println((int)i);
                    System.out.println("D");
                    break;
                case 4:
                    System.out.println((int)i);
                    System.out.println("E");
                case 'E':
                    System.out.println((int)i);
                    System.out.println("F");
            }
        }
    }

}



I don't understand the steps who conduct to the result.

when i = 0 - no case to select, i is incremented to 1
when i = 1 - break loop so for is never executed



Thank a lot!!!

解决方案

You do understand the common fact that zero is not same in integer form, and character form. Their binary, that gets generated and checked up against is not same. A character is a switch-able value, but it will not map any true value for the cases that you are using it for.

To solve this problem, change one of the types to other one,

for (i = 0; i < 5; i++) {
    System.out.println((int)i);
        switch (i++) {
            case 0:
            // Code here.
            // ...


Otherwise, you should consider using character values, and use Character.valueOf(i); and then move onwards. That would look something like this,

for (i = 0; i < 5; i++) {
     // Map it.
     char ch = Character.valueOf(i);
     System.out.println(i); // No need of (int)int; it is already an int.
         switch (ch++) {
             case '0':
             // code here...


Doing the ch++ is legal in Java, because a character is a numeric value that represents the Unicode character. Incrementing it, provides you with the next character and so on. If that doesn't sound easy, read about pointer arithmetic.


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于开关中的字符 - java 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆