如何让java一次读取多个字符并将其更改为整数 [英] How to let java read multiple characters at once and change them to integers

查看:123
本文介绍了如何让java一次读取多个字符并将其更改为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果A = 0,则B = 1,C = 2且D = 3。如果我输入'A',输出将为'0'。

这对于一个字符来说很容易,但是如果我在一个没有空格的情况下输入4个字符,如'BACC',输出应该是'1022'。但是我该怎么做?请尽快帮助



我尝试过的事情:



我没有'尝试任何东西,因为我是新手java,请尽可能简单的答案,所以我可以理解。

解决方案

你必须立即读取输入作为字符串和然后遍历该字符串的字符。



假设输入来自控制台(键盘)并通过按RETURN键终止,您可以读取输入

扫描仪扫描仪= 扫描仪(System.in); 
字符串 input = scanner.nextLine();



要迭代项目,您需要了解相关信息循环,包含项目的类( String(Java SE 9& JDK 9) [ ^ ]),以及该类的哪些方法可能对要解决的问题有用。



对于字符串,这显然是长度(字符数) )以及如何将字符放在字符串中的特定位置。



因为这可能是作业,你到目前为止没有尝试过任何东西,我不会给你码。



学习编程语言的非常重要的方面是尝试自己动手,阅读该语言的官方文档,阅读一本好书,教程或课程笔记


For example if A = 0, B=1, C=2 and D =3. If i input 'A' , the output would be '0'.
This is easy for one character, but how about if i input 4 characters at one without space like the 'BACC', the output should be '1022'. But how do i do that?. Pls help asap

What I have tried:

I haven't tried anything as im new to java, pls make the answer as simple as possible so i can understand.

解决方案

You have to read the input at once as string and iterate then over the characters of that string.

Assuming that the input is from the console (keyboard) and terminated by pressing the RETURN key, you can read the input with

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();


To iterate over items you need knowledge about loops, the class holding the items (String (Java SE 9 & JDK 9 )[^]), and which methods of that class might be useful for the problem to be solved.

With strings, this is obviously the length (number of characters) and how to get the character at a specific position in the string.

Because this might be homework and you have no tried anything so far, I will not give you code.

Very important aspects of learning a programming language are trying to do it yourself, reading the official documentation for the language, and reading a good book, tutorials or your course notes.


这篇关于如何让java一次读取多个字符并将其更改为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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