java 编程实现接收键盘输入任意多个整数,并以逗号分开,然后选择数组排重或随机打乱。返回排序输
java编程实现接收键盘输入任意多个整数,并以逗号分开,然后选择数组排重或随机打乱。返回排序输出结果,并写入指定文件asd.txt保存(以追加方式写入)例如,请输入任意多...
java 编程实现接收键盘输入任意多个整数,并以逗号分开,然后选择数组排重或随机打乱。返回排序输出结果,并写入指定文件 asd.txt保存(以追加方式写入)
例如,
请输入任意多个整数:1,2,1,2,5,6,1
请选择:1.数组排重2.随机打乱
你的选择是:1
操作结果:1,2,5,6
成功写入文件
//求代码!! 展开
例如,
请输入任意多个整数:1,2,1,2,5,6,1
请选择:1.数组排重2.随机打乱
你的选择是:1
操作结果:1,2,5,6
成功写入文件
//求代码!! 展开
5个回答
推荐于2016-04-11
展开全部
// 回答完毕,采纳即可
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Drinks
{
public static final int REMOVE = 1;
public static final int RANDOM = 2;
public static final String FILE = "asd.txt";
public static final String LINE = System.getProperty("line.separator");
private static String[] remove(String[] array)
{
String[] result = new String[0];
String tmp = ",";
for(int i = 0; i < array.length; i++)
{
String one = array[i];
if(tmp.indexOf("," + one + ",") == -1)
{
String[] cloned = new String[result.length + 1];
System.arraycopy(result, 0, cloned, 0, result.length);
cloned[cloned.length - 1] = one;
result = cloned;
tmp += one + ",";
}
}
return result;
}
private static void random(String[] array)
{
Arrays.sort(array, new Comparator<String>()
{
@Override
public int compare(String o1, String o2)
{
double rand = Math.random();
if(rand < 0.5)
{
return 1;
}
return -1;
}
});
}
private static int writeFile(String data, String file, boolean isAppend)
{
int code = 1;
try
{
FileWriter fw = new FileWriter(file, isAppend);
fw.write(data);
fw.flush();
fw.close();
}
catch(IOException e)
{
code = -1;
}
return code;
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int step = 1;
String line = "";
String choosed;
loop: while(true)
{
if(step == 1)
{
System.out.print("输入任意多个整数,并以逗号分开:");
line = scanner.nextLine().trim();
if(!line.matches("^\\d+([,,]\\d+)+$"))
{
continue;
}
step = 2;
}
else if(step == 2)
{
System.out.print("请选择:1.数组排重 2.随机打乱:");
choosed = scanner.nextLine().trim();
if(!choosed.matches("^[12]$"))
{
System.out.println("输入错误!");
continue;
}
else
{
System.out.print("你的选择是:" + choosed);
String[] array = line.split("[,,]");
String data;
int code;
String regex = "[\\[\\]]";
switch(Integer.parseInt(choosed))
{
case REMOVE:
String[] result = remove(array);
data = Arrays.toString(result).replaceAll(regex, "") + LINE;
System.out.println("操作结果: " + data);
code = writeFile(data, FILE, true);
if(code == 1)
{
System.out.println("成功写入文件");
}
else
{
System.out.println("失败写入文件");
}
break;
case RANDOM:
random(array);
data = Arrays.toString(array).replaceAll(regex, "") + LINE;
System.out.println("操作结果: " + data);
code = writeFile(data, FILE, true);
if(code == 1)
{
System.out.println("成功写入文件");
}
else
{
System.out.println("失败写入文件");
}
break;
default:
break;
}
while(true)
{
System.out.println("是否继续?继续按3,退出按4");
line = scanner.nextLine().trim();
if("3".equals(line))
{
step = 1;
continue loop;
}
else if("4".equals(line))
{
scanner.close();
break loop;
}
else
{
continue;
}
}
}
}
}
}
}
展开全部
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**
* java 编程实现接收键盘输入任意多个整数,并以逗号分开, 然后选择数组排重或随机打乱。返回排序输出结果,并写入指定文件
* asd.txt保存(以追加方式写入) 请选择:1.数组排重2.随机打乱 1,2,1,2,5,6,1
*
* @author cp
*
*/
public class Random {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("1、输入a结束输入");
List<String> list = new ArrayList<>();
String a = "";
int count = 0;
while (true) {
count++;
System.out.print("请输入第" + count + "个数字:");
a = scanner.next();
if (a.equals("a")) {
break;
}
list.add(a);
}
System.out.print("你输入的数字为:");
for (String s : list) {
System.out.print(s + ",");
}
System.out.println();
int menu=-1;
do{
System.out.println("1、数组排重");
System.out.println("2、随机打乱");
System.out.println("3、退出系统");
System.out.print("请选择:");
menu= scanner.nextInt();
switch (menu) {
case 1:
System.out.print("排重之后:");
Set<String> s=paichun(list);
//迭代器输出set集合
Iterator<String> its=s.iterator();
while(its.hasNext()){
System.out.print(its.next()+",");
}
System.out.println();
break;
case 2:
System.out.print("打乱之后为:");
String[] st1=daluan(list);
for (int i = 0; i < st1.length; i++) {
System.out.print(st1[i]+",");
}
System.out.println();
break;
default:
break;
}
}while(menu!=3);
}
//排重方法
public static Set<String> paichun(List<String> list1){
//set中不能有重复的元素,所以最后回返回一个没有重复元素的set集合
Set<String> s=new HashSet<>();
for (int i = 0; i <list1.size(); i++) {
s.add(list1.get(i));
}
return s;
}
/**
* 将排重之后的集合,传到数组里然后在数组中随机打乱
* @param list2
* @return
*/
public static String[] daluan(List<String> list2){
String[] s1=new String[list2.size()];
Set<String> set=paichun(list2);
//将排重之后的集合,传到数组里
Iterator<String> its=set.iterator();
int i=0;
while(its.hasNext()){
s1[i]=its.next();
i++;
}
//在数组中随机打乱
int a=(int)(Math.random()*s1.length+1)-1;//产生一个随机数,在数组的下标范围之内
for (int j = 0; j < 3; j++) {
String temp=s1[j];
s1[j]=s1[a];
s1[a]=temp;
}
return s1;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**
* @author -----
* @DATE 2015-9-2
*/
public class TestArray {
private Integer[] data;
public void inputDataFromConsole() {
Scanner sc = new Scanner(System.in);
System.out.println("键盘输入任意多个整数,并以逗号分开");
String dataStr = sc.nextLine();
String[] dataStrArray = dataStr.split(",");
List<Integer> dataList = new ArrayList<Integer>();
for (String s : dataStrArray) {
Integer item = null;
try {
item = Integer.parseInt(s);
} catch (Exception e) {
e.printStackTrace();
// 如果输入有误,直接忽略把,你也可以更换逻辑
continue;
}
dataList.add(item);
}
if (dataList.size() == 0) {
System.out.println("没有输入数据,所以系统将推出");
System.exit(1);
}
data = new Integer[dataList.size()];
dataList.toArray(data);
}
public void command() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入指令:1.数组排重2.随机打乱");
String command = sc.next("\\d");
System.out.println("你选择了" + command + ":");
switch (Integer.parseInt(command)) {
case 1:
distinct();
break;
case 2:
shuffle();
break;
default:
// do nothing,it must be error
}
}
public void distinct() {
Set<Integer> set = new HashSet<Integer>();
Collections.addAll(set, data);
data = new Integer[set.size()];
set.toArray(data);
System.out.println(set);
}
public void shuffle() {
List<Integer> list = Arrays.asList(data);
Collections.shuffle(list);
list.toArray(data);
System.out.println(list);
}
public static void main(String args[]) {
TestArray ta = new TestArray();
ta.inputDataFromConsole();
ta.command();
}
}
就帮到这里,文件写入,你可以自己补充,主要部分我帮你实现了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
while(true){
test();
}
}
//入口方法
public static void test(){
System.out.println("请输入一批整数,用逗号隔开。");
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println("请选择:1.数组排重2.随机打乱;3.重新输入一批整数");
while (sc.hasNext()) {
//接收整数
int i = sc.nextInt();
switch(i){
case 1:
System.out.println("数组排重:"+i);
sortNum(str);
break;
case 2:
System.out.println("随机打乱:"+i);
mix(str);
break;
case 3:
test();
}
}
sc.close();
}
//数组排重
public static void sortNum(String str){
String[] array = str.split(",");
Arrays.sort(array);//排下序
StringBuilder sb = new StringBuilder();
for (String s : array) {
if(sb.indexOf(s) != -1){
continue;
}
sb.append(s).append(",");
}
String newStr = sb.toString().substring(0,sb.length() - 1);
//将newStr写入文件,自己完成
System.out.println(newStr);
}
//随机打乱
public static void mix(String str){
List<String> list=Arrays.asList(str.split(","));
Collections.shuffle(list);
String newStr = new String();
for(String s:list){
newStr+=s+",";
}
//去掉最后一个逗号
newStr = newStr.substring(0, newStr.length()-1);
//将newStr写入文件,自己完成
System.out.println(newStr);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-09-02
展开全部
package test;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Iterator;
import java.util.List;
public class a10 {
static int[] retArr(String is){
int[] a=new int[20];
String[] strArr=is.split(",");
for(int i=0;i<strArr.length;i++){
a[i]=Integer.parseInt(strArr[i]);
}
return a;
}
static int[] retArrl(int[] is){
int[] a=new int[20];
Random r=new Random();
List<Integer> list=new ArrayList<Integer>();
int i=0;
while(list.size()<is.length){
i=r.nextInt(is.length);
if(!list.contains(i)) list.add(i);
}
i=0;
Iterator<Integer> iter=list.iterator();
while(iter.hasNext()){
a[i]=iter.next();
i++;
}
return a;
}
private static int[] retArr2(int[] is) {
// TODO Auto-generated method stub
int[] a=new int[20];
List<Integer> list=new ArrayList<Integer>();
int i=0;
for(i=0;i<is.length;i++){
if(!list.contains(is[i])) list.add(is[i]);
}
Iterator<Integer> iter=list.iterator();
i=0;
while(iter.hasNext()){
a[i]=iter.next();
i++;
}
return a;
}
public static void main(String[] args) throws IOException{
Scanner sc=new Scanner(System.in);
System.out.println("please input your num:");
String str=sc.nextLine();
System.out.println("please input your choice:");
int ch=sc.nextInt();
int[] b=retArr(str) ;
System.out.print("yours input is:");
for(int i=0;i<b.length;i++){
System.out.print(b[i]+" ");
}
System.out.println();
System.out.println("yours choice is:"+ch);
for(int i=0;i<retArrl(b).length;i++){
System.out.print(retArrl(b)[i]+" ");
}
System.out.println();
for(int i=0;i<retArr2(b).length;i++){
System.out.print(retArr2(b)[i]+" ");
}
System.out.println();
File myfile=new File("F://e/javatest/asd.txt");
try{
myfile.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
FileOutputStream out=new FileOutputStream(myfile);
int[] c;
if(ch=='2'){
c=retArrl(b);
try{
for(int i=0;i<c.length;i++){
out.write(c[i]);
out.write('\0');
}
out.close();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
else if(ch == '1'){
c=retArr2(b);
try{
for(int i=0;i<c.length;i++){
out.write(c[i]);
}
out.close();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
else
c=b;
try{
for(int i=0;i<c.length;i++){
out.write(c[i]);
}
out.close();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}
具体思路就是这样,有错误,不想找了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询