简体   繁体   English

charAt()和Math.pow()

[英]charAt() and Math.pow()

Background: 背景:

I am making a simple base converter for a class assignment. 我正在为类赋值创建一个简单的基本转换器。 I am fairly close to completion but need to tidy up the conversion algorithm to convert the user inputted value (in a given base) to a base 10 value. 我已经接近完成,但是需要整理转换算法以将用户输入的值(以给定的基数)转换为以10为基数的值。

Attempt: 尝试:

import java.util.Scanner;

public class BaseConverter {
public static void main(String[] args) {
    String numIn, intro1, prompt1, prompt2, output1;
    int baseIn;
    double numOut;
    boolean flag = true;
    Scanner kb = new Scanner(System.in);

    intro1 = "This program converts values in different number bases to a decimal value.";
    prompt1 = "Type in a number base (2 - 9): ";

    while (flag == true){
        System.out.println(intro1);
        System.out.println(prompt1);
        baseIn = kb.nextInt();
        //Checking base value for outliers outside given range
        if (baseIn < 2 || baseIn > 9) {
            System.out.println("Base must be between 2 and 9");
            System.exit(1);
        }
        prompt2 = "Type in a base "+baseIn+" number: ";
        System.out.println(prompt2);
        numIn = kb.next();

        // System.out.println(numStore);
        int counter = 0;
        // Let's pretend baseIn is 3 and i starts at 3 
        for (int i = numIn.length(); i >= 1; i--){
            numOut = (numIn.charAt(i-1) * Math.pow(baseIn, counter++));
            System.out.println(numOut);
        }
    }//while
}// method
}//class

The problem: 问题:

This line does not return the expected value 此行不返回预期值

numOut = (numIn.charAt(i-1) * Math.pow(baseIn, counter++));

For example, in string "10", numOut should be (0*(2*0)) or zero on the first iteration of the for loop. 例如,在字符串“ 10”中,在for循环的第一次迭代中numOut应该为(0 *(2 * 0))或零。 Instead, it returns 48.0. 相反,它返回48.0。

My Thoughts: 我的想法:

I have a sneaking suspicion it has to do with the charAt() method as debugging the Math.pow() method showed it returning expected values. 我有一个偷偷的怀疑,它与charAt()方法有关,因为调试Math.pow()方法显示它返回了期望值。 Suppose it's something to do with all the different variable types? 假设这与所有不同的变量类型有关? I'm unsure. 我不确定。

Yes you're right charAt is the problem. 是的,您是对的charAt是问题所在。

When you type let's say "10", the integer value of the character '0' is 48 and for '1' it's 49 according to the encoding table Java uses to encode characters. 当您输入“ 10”时,根据Java用于编码字符的编码表,字符'0'的整数值为48,对于'1' ”,其值为49。

在此输入图像描述

If you take a look at it, you see that 0 is encoded as 0x0030 = 3*16^1 = 48 , 1 as 0x0031 = 3*16^1 + 1*16^0 = 49 and so on. 如果看一下,您会看到0编码为0x0030 = 3*16^1 = 48编码为0x0031 = 3*16^1 + 1*16^0 = 49 ,依此类推。

If you want to get the numeric value of the character itself you can use 如果要获取角色本身的数值,可以使用

numOut = Character.getNumericValue(numIn.charAt(i-1)) * Math.pow(baseIn, counter++);

The charAt method returns the char of your input, in this case '0' , not 0 . charAt方法返回输入的char ,在本例中为'0' ,而不是0 The Unicode value for the char '0' is not 0 , it's 48 . char '0'的Unicode值不为0 ,而是48

Fortunately, the values '0' through '9' are consecutive Unicode values, 48 through 57 respectively, so you can "subtract" out the 48 by subtracting '0' before multiplying. 幸运的是,值'0''9'分别是连续的Unicode值, 4857 ,所以你可以通过在乘法之前减去'0'来“减去” 48

numOut = ( (numIn.charAt(i-1) - '0') * Math.pow(baseIn, counter++));

You'll still need to validate that what the user typed is in fact a valid "digit" in the chosen base. 您仍然需要验证用户键入的内容实际上是所选基数中的有效“数字”。

You'll also want to add the values of numOut together to get the decimal result at the end. 您还需要将numOut的值一起添加以在结尾处获取小数结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM