ascii转换java的问题

时间:2016-10-04 04:39:46

标签: java ascii

嘿,所以我有类型转换int的问题。我这里有一个程序设计用于打印输入数字的两位数,然后我应该使用++运算符递增第一个数字并乘以10然后添加第二个数字并打印新数字(应该是原始加号) 10)。由于ascii,我无法正确增加第一个数字。

 import java.util.Scanner;
    public class Lab1_Redo
     {

        public static void main (String[] args) 
        {
            int Zero = (char)0;

            Scanner Keyboard = new Scanner(System.in);
            System.out.println("Enter a two-digit number:");
            String entered_digits = Keyboard.nextLine();
            int FirstDigit = entered_digits.charAt(0);
            System.out.println("The first digit that was entered was:" + " " + (char)FirstDigit);
            int SecondDigit = entered_digits.charAt(1);
            System.out.println("The second digit that was entered was:" + " " + (char)SecondDigit);
             int AsciiToInt = FirstDigit;
             AsciiToInt++;
             int SecondDigits = (char)SecondDigit;
             int EnteredNumberPlusTen = ((AsciiToInt * 10) + SecondDigits);
             //System.out.println("This new number:" + " " + EnteredNumberPlusTen + " " + "Is the original number plus ten");
            System.out.println(AsciiToInt);
        }
    }

6 个答案:

答案 0 :(得分:0)

int AsciiToInt = FirstDigit - '0';
int digit2 = SecondDigit - '0';

除非你有特殊的理由按字符操纵输入字符,否则你应该这样做:

int input = Integer.parseInt(keyboard.nextLine());
input += 10;

答案 1 :(得分:0)

您可以使用Interger.parseInt(String string)将字符串转换为int。

https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

答案 2 :(得分:0)

将char分配给int时,它将采用ASCII值。 请尝试以下代码。

import java.util.Scanner;
public class Lab1_Redo{
public static void main (String[] args)
{
    int Zero = (char)0;
    Scanner Keyboard = new Scanner(System.in);
    System.out.println("Enter a two-digit number:");
    String entered_digits = Keyboard.nextLine();
    int FirstDigit = Integer.parseInt(String.valueOf(entered_digits.charAt(0)));
    System.out.println("The first digit that was entered was:" + " " + FirstDigit);
    int SecondDigit =  Integer.parseInt(String.valueOf(entered_digits.charAt(1)));
    System.out.println("The second digit that was entered was:" + " " + SecondDigit);
    int EnteredNumberPlusTen = ((++FirstDigit * 10) + SecondDigit);
    System.out.println(EnteredNumberPlusTen);
}}

答案 3 :(得分:0)

import java.util.Scanner;
    public class Lab1_Redo
     {

        public static void main (String[] args) 
        {             

            Scanner Keyboard = new Scanner(System.in);
            System.out.println("Enter a two-digit number:");
            String entered_digits = Keyboard.nextLine();
              char sr[] = entered_digits.toCharArray();
            int FirstDigit = (int) sr[0];
            System.out.println("The first digit that was entered was:" + " " + sr[0]);
            int SecondDigit = (int) sr[1];
            System.out.println("The second digit that was entered was:" + " " + sr[1]);
             int AsciiToInt = FirstDigit;
             AsciiToInt++;
             int SecondDigits = (char)SecondDigit;
             int EnteredNumberPlusTen = ((AsciiToInt * 10) + SecondDigits);
             //System.out.println("This new number:" + " " + EnteredNumberPlusTen + " " + "Is the original number plus ten");
            System.out.println(AsciiToInt);
        }
    }

尝试这个..!

答案 4 :(得分:0)

您输入的数字错误,因为您没有从ascii值中减去48。 0的ascii值是48,所以你不能简单地将强制转换为int并使用。你必须减去它。如果只有2位数字,则代码有效。

修改后的代码:

            Scanner Keyboard = new Scanner(System.in);
            System.out.println("Enter a two-digit number:");
            String entered_digits = Keyboard.nextLine();
            int FirstDigit = entered_digits.charAt(0) - 48;
            System.out.println("The first digit that was entered was:" + " " + FirstDigit);
            int SecondDigit = entered_digits.charAt(1) - 48;
            System.out.println("The second digit that was entered was:" + " " + SecondDigit);
            int EnteredNumberPlusTen = ((++FirstDigit * 10) + SecondDigit);
            System.out.println("This new number:" + " " + EnteredNumberPlusTen + " " + "Is the original number plus ten");

最佳代码:

        Integer number = Keyboard.nextInt();
        int first = number /10;
        int second = number%10;
        EnteredNumberPlusTen = ((++first * 10) + second);
        System.out.println("This new number:" + " " + EnteredNumberPlusTen + " " + "Is the original number plus ten");

答案 5 :(得分:0)

你是对的,因为你处理所提供的两位数字 STRING 的每个解析数字所代表的ASCII值。

让我们提供 65 .....

的值

因为您正在使用 String.charAt()方法,所以您需要更进一步,将解析后的数字转换为Integer值。因为您编码:

int FirstDigit = entered_digits.charAt(0);

这会将54的值放入 FirstDigit 整数变量中,该变量实际上是Ascii字符 6 的ASCII值。这显然不是你想要的。您真正想要的是 FirstDigit 整数变量中的 6 的整数值,为此您需要转换字符 6 到String,然后将数字字符串转换为Integer值,如下所示:

String firstNumber = String.valueOf(entered_digits.charAt(0));
int FirstDigit = Integer.valueOf(firstNumber);
System.out.println("The first digit that was entered was:" + " " + firstNumber);

现在,您实际上正在处理数字字符串的整数表示" 6 "。为你的第二个数字做同样的事情,你就参加了比赛。

提示:完成上述操作后,您不再需要代码行:

int SecondDigits = (char)SecondDigit;

进行必要的修改......

提示:您还可以使用 String.substring()方法代替 String.charAt()方法。使用Java,总有不止一种方法可以为猫,海狸等提供皮肤。