java 输入数字到数组_java输入数字存入数组(带字符检测)

package com.hw.h817;

import java.util.Scanner;

public class Check {

public static void main(String[] args){

Scanner s =new Scanner(System.in);

//把pre和str在外面定义,如果在循环中定义会出错

String[] two =new String[6];

String pre;

int[] str =new int[6];

int count = 0;//用于退出循环

while(true){

System.out.println("请依次输入6个数");

count = 0;//如果输入字符不是数字时,从if中出来,重新计数

for(inti=0;i<6;i++){

pre = s.next();//接收字符串

if(!(pre.matches("\\d+"))) {//非(pre全为数字)

System.out.println("输入的" + pre + "不是数字,请重新输入");

break;//这个退出的是for循环,退出后会再次执行while循环,重新输入数字                }

else {

str[i] = Integer.parseInt(pre);//把数字型字符串转换为int型数字                }

count++;//每执行一次数据输入后,count才会+1            }

if(count==6)break;//这个退出的是while循环        }

for(int i=0;i<6;i++){//输出打印数字

if(i==0){

System.out.print(str[i]);

}

else{

System.out.print(","+str[i]);

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值