简体   繁体   English

循环CharAt将字符串转换为ASCII

[英]Looping CharAt to convert a String to ASCII

Hi guys i made this 大家好,我做了这个

import java.util.Scanner;
//Creates a class

public class codeString {

    public static void main(String[] arg) { //creates scanne/giving name
        Scanner ImBack = new Scanner(System.in);

        //print out "enter any String" and asks to put in data
        System.out.print("Enter any String :");
        String Word = ImBack.nextLine();

        int ascii = (int) Word.charAt(0);
        System.out.println(ascii);
        System.out.println((char) Word.charAt(0));
    }
}

But when i run it it converts only 1 letter, I know that i have to make a loop.. so then i went on google and made this 但是,当我运行它时,它只能转换1个字母,我知道我必须做一个循环..于是我在Google上做了这个

for (Word.charAt(0); Word = int; Word = Word) {
    System.out.println("" + Word);
}

printing lots of errors, one of them was asking for toString, but it worked with out the toString for the one letter, so i know i did loop wrong 100%, could anyone help? 打印很多错误,其中之一是要求toString,但它与toString一起工作了一个字母,所以我知道我100%循环错误,有人可以帮忙吗? and will i need a 我需要一个

    length 

in there? 在那里?

You need something like this : 您需要这样的东西:

for (int i = 0; i < Word.length(); i++) {
    System.out.println(Word.charAt(i));
}
  1. Word.length() return to you the length of your word or text Word.length()返回单词或文本的长度
  2. Word.charAt(i) to get character by character Word.charAt(i)逐字符获取字符

You can learn also the Oracle tutorials about Arrays and do...while Loop 您还可以学习有关阵列的Oracle教程, 并... while循环

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

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