java中输入一个数字之后,怎么把这个数字拆分成一个字一个字的呢?
我想编写一个功能,就是你随机输入一个数字。例如342345243,他就会把他拆分成第一个数字3第二个数字4第三个数字2,第4个数字这样直到最后一个数字,怎么实现呢?而且我输入的那个随机的数字是随机的哦,有可能只有一位数,也有可能是8位数字。求大神出现~
我想编写一个功能,就是你随机输入一个数字。例如342345243,他就会把他拆分成第一个数字3第二个数字4第三个数字2,第4个数字这样直到最后一个数字,怎么实现呢?而且我输入的那个随机的数字是随机的哦,有可能只有一位数,也有可能是8位数字。求大神出现~
*
* @author Administrator
*/
public class Split {
public static void main(String args[]){
System.out.println("请输入数字,按Enter键结束!");
Scanner s=new Scanner(System.in);
String str=s.next();
char[] c=str.toCharArray();
System.out.println("把你输入的数字一个一个地输出,如下所示:");
for(int i=0;i<c.length;i++){
System.out.print(c[i]+" ");
}
System.out.println();
}
}
运行结果如下所示:
run:
请输入数字,按Enter键结束!
987524652
把你输入的数字一个一个地输出,如下所示:
9 8 7 5 2 4 6 5 2
成功生成(总时间:14 秒)
之后一个函数搞定
toCharArray
public char[] toCharArray()Converts this string to a new character array.
Returns:
a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
请问这些数字变成这样之后不是字符型了而已吗?怎么变成int型或者float型呢?
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字");
int a = scanner.nextInt();
while(a != 0){
int n = a % 10;
System.out.println(n);
a /= 10;
}
}
}
public class TestString {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
//System.out.println(((Integer)a).toString());
char[] ch = ((Integer)a).toString().toCharArray();
for(int i = 0;i < ch.length;i++) {
System.out.print(ch[i] + " ");
}
System.out.println();
}
}
public static String numberToString(int number) {
StringBuilder result = new StringBuilder(); do {
result.append(number % 10);
number /= 10;
} while (number != 0); return result.reverse().toString();
} public static void main(String[] args) {
int n = 342345243;
System.out.println(numberToString(n));
}
}
int n = 342345243;
print(n);
}
static void print(int n) {
if(n == 0) return;
print(n / 10);
System.out.print(n - n / 10 * 10);//n % 10
}
}
* 三位数全中且顺序对:奖金10000
*三位数都有:奖金3000
*有一个数匹配:奖金1000 */
import java.util.Scanner;
public class E3_14 {
public static void main(String[] args){
int number=(int)(Math.random()*900+100);
boolean mathAll=false;
boolean mathOne=false;
int count=0;
Scanner input=new Scanner(System.in);
int guess=input.nextInt();
input.close();
if(){
//判断mathAll,或者matchOne ,然后改变他们的布尔值
}
if(guess==number){
System.out.println("Match ! You win $10,000");
}
if(mathAll){
System.out.println("Match all digits! You win $3,000");
}
if(mathOne){
System.out.println("Match one digit! You win $1,000");
}
else{
System.out.println("Not match");
}
}
}