概述
代码略显粗糙
有可优化的地方可以留言指教,不胜感激
import java.util.Scanner;
public class array{
public static void main(String[] args) {
//定义一个浮点数类型的数组
double[] array1 = new double[1];
Scanner input = new Scanner(System.in);
//输入想要添加进去的数字
for (int i = 0; i < array1.length ;i++ ) {
System.out.println("Enter the number");
array1[i] = input.nextDouble();
//跳出是否继续添加数字
System.out.println(" if you continue enter, y/n?");
char answer = input.next().charAt(0);
switch(answer){
case 'y'://选择继续添加的情况
double[] array2 = new double[array1.length + 1];
for(int j = 0;j < array1.length; j++){
array2[j] = array1[j];
}
array1 = array2;
continue;
case 'n'://选择不添加的情况
break;
}
}
//遍历数组
for (int i = 0;i < array1.length;i++) {
System.out.print(array1[i] + "t");
}
}
}
最后
以上就是粗心信封为你收集整理的日常练习 数组扩容的全部内容,希望文章能够帮你解决日常练习 数组扩容所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复