yzykg 2009-10-14 12:37
浏览 304
已采纳

java同一个类中两个函数之间的变量值怎么不一样了

写的一个输入二进制,输出十进制的程序
void getInput() {
Scanner in = new Scanner(System.in);
System.out.print("Input a binary number: ");
int [color=red]inpNum[/color] = in.nextInt();
inpBin = String.valueOf(inpNum);

}

void testInput() {

    if ([color=red]inpNum[/color] < NO3) {              /* value must in the scope of int */
        if ([color=red]inpNum[/color] < NO0) {          /* value must bigger than 0 */
            System.out.println("Input must be >=0" + "\n");
            process();
        } else if ([color=red]inpNum[/color] == NO0) {  /* if value equals 0 */
            System.out.println("0");
            System.out.println("Base 2 Equals");
            System.out.println("0");
            System.out.println("Base 10");
        } else {                        
            for (int i=NO0; i<inpBin.length(); i++) {
                if (inpBin.charAt(i) > '1') {   /* value must be a binary */
                    System.out.println("Input must be a Binary number");
                    System.out.println();
                    process();
                    break;  
                } else if (i == inpBin.length()-NO1) {
                    System.out.print(inpBin);
                    System.out.println("\n" + "Base 2 Equals");
                    fact();
                }
            }       
        }
    } else {
        System.out.println("Input must be smaller than 2147483647");
    }
}

⋯⋯⋯
    ⋯⋯⋯


private int inpNum;         // value of input 
private int baseNum;        // used to obtain the value of decimal
private int mulNum;         // used to shift when calculate
private String inpBin;      // the String value of inpNum

在getInput()里将键盘输入的值赋给inpNum,并转化为String值给inpBin
但在testInput()里,inpNum就自动变成了0,inpBin的值还是刚刚赋予的值,这是为什么呢?

  • 写回答

2条回答 默认 最新

  • wanghaolovezlq 2009-10-14 12:43
    关注

    你搞错了

    你这样int inpNum = in.nextInt(); 是在getInput方法定义一个局部变量

    修改为inpNum = in.nextInt();

    代码如下
    [code="java"] void getInput() {
    Scanner in = new Scanner(System.in);
    System.out.print("Input a binary number: ");
    [color=red]inpNum = in.nextInt(); [/color]
    inpBin = String.valueOf(inpNum);
    }

    void testInput() {

    if (inpNum < NO3) { /* value must in the scope of int /
    if (inpNum < NO0) { /
    value must bigger than 0 /
    System.out.println("Input must be >=0" + "\n");
    process();
    } else if (inpNum == NO0) { /
    if value equals 0 /
    System.out.println("0");
    System.out.println("Base 2 Equals");
    System.out.println("0");
    System.out.println("Base 10");
    } else {
    for (int i=NO0; i if (inpBin.charAt(i) > '1') { /
    value must be a binary */
    System.out.println("Input must be a Binary number");
    System.out.println();
    process();
    break;
    } else if (i == inpBin.length()-NO1) {
    System.out.print(inpBin);
    System.out.println("\n" + "Base 2 Equals");
    fact();
    }
    }
    }
    } else {
    System.out.println("Input must be smaller than 2147483647");
    }
    }

    ⋯⋯⋯
    ⋯⋯⋯

    private int inpNum; // value of input
    private int baseNum; // used to obtain the value of decimal
    private int mulNum; // used to shift when calculate
    private String inpBin; // the String value of inpNum [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3