java中用Scanner怎么把从键盘输入的数据保存在数组中并进行冒泡排序 10
3个回答
展开全部
给你两个代码 自己拼下
package com.kk.grammaticaldevice.basic;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerTest {
public static String readConsole() {
Scanner scanner = new Scanner(System.in);
System.out.print("please in put a String: ");
String temp = "";
try {
temp = scanner.next();
} catch (InputMismatchException e) {
System.out.println("InputMismatchException: " + e.getMessage());
} catch (Exception e) {
System.out.println("Exception: "帆凯厅 + e.getMessage());
}
System.out.println("Input is " + temp);
return temp;
}
public static void scannerNext() {
String text = "This is a sentence.";
Scanner s = new Scanner(text);
String word1 = s.next(); // "This"
String word2 = s.next(); // "is"
String word3 = s.next(); // "a"
String word4 = s.next(); // "sentence."
System.out.println(word1 + "\n" + word2 + "\n" + word3 + "\n" + word4);
}
public static void readFromConsole() {
System.out.println("Please input a line.If input 'exit' then exit."态隐);
String str = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
while ((str = br.readLine()) != null) {
if (str.equalsIgnoreCase("exit"))
break;
System.out.println(str.toUpperCase()); /孙悔/ 将str所有字符都转换成大写
}
} catch (IOException ioe) {
System.out.println("Exception: " + ioe.getMessage());
}
}
public static void execute() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("please in put a String: ");
String temp = "";
temp = scanner.next();
System.out.println("input is : " + temp);
if (temp.equalsIgnoreCase("end")) {
System.out.println("app is end.");
break;
}
}
}
public static void main(String[] args) {
execute();
}
}
------------------------------------------------------------
package com.kk.print;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Sort {
public static void main(String[] args) {
// NumberSort.doMain();
// MaxMinNumber.doMain();
// NumberSort.doMain();
}
}
/** 求用java比较四个数大小源代码 */
class NumberSort {
public static void doMain() {
int[] array = { 11, 77, 22, 44 };
// 也可以从控制台输出
int[] newArray = sort(array);
for (int i = 0; i < newArray.length; i++) {
System.out.println(newArray[i] + " ");
}
}
public static int[] sort(int[] intArray) {
int tempArray[] = intArray;
int length = tempArray.length;
int tempValue = 0;
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (tempArray[i] > tempArray[j]) {
tempValue = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = tempValue;
}
}
}
return tempArray;
}
}
/**
* 写一个 Ex4SelectionSort.java 其中包含4个methods: main, sort, findIndexOfNextSmallest ,
* arrayPrint.
*/
class Ex4SelectionSort {
/** 显示数组信息 */
public static void arrayPrint(double[] darr) {
StringBuffer sb = new StringBuffer();
sb.append("[");
if (darr.length > 0) {
for (int i = 0; i < darr.length - 1; i++) {
sb.append(darr[i]).append(",");
}
sb.append(darr[darr.length - 1]).append("]");
} else {
sb.append("]");
}
System.out.println(sb);
}
/**
* @param unsorted
* 一个命名为 unsorted 的double数组
* @param startIndex
* 从数组的什么位置开始找最小值
* @return 返回值是 数组中最小值的 索引
*/
public static int findIndexOfNextSmallest(double[] unsorted, int startIndex) {
if (unsorted.length == 0) {
return -1;
}
if (unsorted.length == 1) {
return 0;
}
double temp = unsorted[0];
int minIndex = 0;
for (int i = startIndex; i < unsorted.length; i++) {
if (temp > unsorted[i]) {
temp = unsorted[i];
minIndex = i;
}
}
System.out.print("数组 ");
arrayPrint(unsorted);
System.out.println("中最小的元素是 " + temp + "索引是 " + minIndex);
return minIndex;
}
/**
* @return 返回从小到大重新排列的数组
* @param toBeSorted
* 要排序的原始数组
*/
public static double[] sort(double[] toBeSorted) {
System.out.println("排序开始...");
double position;
for (int i = 0; i < toBeSorted.length; i++) {
for (int k = i + 1; k < toBeSorted.length; k++) {
if (toBeSorted[k] < toBeSorted[i]) {
position = toBeSorted[i];
toBeSorted[i] = toBeSorted[k];
toBeSorted[k] = position;
}
}
}
System.out.println("...排序结束");
return toBeSorted;
}
public static void doMain() {
double[] arr = { 154.1, 324.1, -9.0, 0.67, 266.5, 68.5, -162.7, -0.41,
18.2 };
arrayPrint(arr);
findIndexOfNextSmallest(arr, 0);
double[] result = sort(arr);
arrayPrint(result);
}
}
/** java中从控制台输入一些整数然后求这些整数的最大最小值要求用while或do-while来做输入的循环。 */
class MaxMinNumber {
public static void doMain() {
try {
Scanner s = new Scanner(System.in);
System.out.print("请输入要输入的数字序列的最大个数:");
int size = s.nextInt();
int[] iArray = new int[size];
int max = 0;
int min = 0;
int temp;
String inputStr = "";
System.out.println("开始输入要比较的数字序列,输入 '000' 表示结束,不计入.最多输入 " + size
+ " 个数字.");
int count = -1;
while (true) {
count++;
if (count >= size) {
break;
}
System.out.print("请输入第 " + (count + 1) + " 个数字:");
inputStr = s.next();
if ("000".equals(inputStr)) {
break;
}
temp = Integer.parseInt(inputStr);
iArray[count] = temp;
if (count == 0) {
max = min = temp;
} else {
if (max < temp) {
max = temp;
}
if (min > temp) {
min = temp;
}
}
}
for (int i : iArray) {
System.out.print(i + "\t");
}
System.out.println("\nmax === " + max + " ; min === " + min);
} catch (InputMismatchException ime) {
System.out.println("exception : 必须输入数字!程序已结束... "
+ ime.getMessage());
} catch (NumberFormatException nfe) {
System.out.println("exception : 必须输入数字!程序已结束... "
+ nfe.getMessage());
} catch (Exception e) {
System.out.println("exception : 程序已结束... " + e.getMessage());
}
}
}
package com.kk.grammaticaldevice.basic;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerTest {
public static String readConsole() {
Scanner scanner = new Scanner(System.in);
System.out.print("please in put a String: ");
String temp = "";
try {
temp = scanner.next();
} catch (InputMismatchException e) {
System.out.println("InputMismatchException: " + e.getMessage());
} catch (Exception e) {
System.out.println("Exception: "帆凯厅 + e.getMessage());
}
System.out.println("Input is " + temp);
return temp;
}
public static void scannerNext() {
String text = "This is a sentence.";
Scanner s = new Scanner(text);
String word1 = s.next(); // "This"
String word2 = s.next(); // "is"
String word3 = s.next(); // "a"
String word4 = s.next(); // "sentence."
System.out.println(word1 + "\n" + word2 + "\n" + word3 + "\n" + word4);
}
public static void readFromConsole() {
System.out.println("Please input a line.If input 'exit' then exit."态隐);
String str = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
while ((str = br.readLine()) != null) {
if (str.equalsIgnoreCase("exit"))
break;
System.out.println(str.toUpperCase()); /孙悔/ 将str所有字符都转换成大写
}
} catch (IOException ioe) {
System.out.println("Exception: " + ioe.getMessage());
}
}
public static void execute() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("please in put a String: ");
String temp = "";
temp = scanner.next();
System.out.println("input is : " + temp);
if (temp.equalsIgnoreCase("end")) {
System.out.println("app is end.");
break;
}
}
}
public static void main(String[] args) {
execute();
}
}
------------------------------------------------------------
package com.kk.print;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Sort {
public static void main(String[] args) {
// NumberSort.doMain();
// MaxMinNumber.doMain();
// NumberSort.doMain();
}
}
/** 求用java比较四个数大小源代码 */
class NumberSort {
public static void doMain() {
int[] array = { 11, 77, 22, 44 };
// 也可以从控制台输出
int[] newArray = sort(array);
for (int i = 0; i < newArray.length; i++) {
System.out.println(newArray[i] + " ");
}
}
public static int[] sort(int[] intArray) {
int tempArray[] = intArray;
int length = tempArray.length;
int tempValue = 0;
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (tempArray[i] > tempArray[j]) {
tempValue = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = tempValue;
}
}
}
return tempArray;
}
}
/**
* 写一个 Ex4SelectionSort.java 其中包含4个methods: main, sort, findIndexOfNextSmallest ,
* arrayPrint.
*/
class Ex4SelectionSort {
/** 显示数组信息 */
public static void arrayPrint(double[] darr) {
StringBuffer sb = new StringBuffer();
sb.append("[");
if (darr.length > 0) {
for (int i = 0; i < darr.length - 1; i++) {
sb.append(darr[i]).append(",");
}
sb.append(darr[darr.length - 1]).append("]");
} else {
sb.append("]");
}
System.out.println(sb);
}
/**
* @param unsorted
* 一个命名为 unsorted 的double数组
* @param startIndex
* 从数组的什么位置开始找最小值
* @return 返回值是 数组中最小值的 索引
*/
public static int findIndexOfNextSmallest(double[] unsorted, int startIndex) {
if (unsorted.length == 0) {
return -1;
}
if (unsorted.length == 1) {
return 0;
}
double temp = unsorted[0];
int minIndex = 0;
for (int i = startIndex; i < unsorted.length; i++) {
if (temp > unsorted[i]) {
temp = unsorted[i];
minIndex = i;
}
}
System.out.print("数组 ");
arrayPrint(unsorted);
System.out.println("中最小的元素是 " + temp + "索引是 " + minIndex);
return minIndex;
}
/**
* @return 返回从小到大重新排列的数组
* @param toBeSorted
* 要排序的原始数组
*/
public static double[] sort(double[] toBeSorted) {
System.out.println("排序开始...");
double position;
for (int i = 0; i < toBeSorted.length; i++) {
for (int k = i + 1; k < toBeSorted.length; k++) {
if (toBeSorted[k] < toBeSorted[i]) {
position = toBeSorted[i];
toBeSorted[i] = toBeSorted[k];
toBeSorted[k] = position;
}
}
}
System.out.println("...排序结束");
return toBeSorted;
}
public static void doMain() {
double[] arr = { 154.1, 324.1, -9.0, 0.67, 266.5, 68.5, -162.7, -0.41,
18.2 };
arrayPrint(arr);
findIndexOfNextSmallest(arr, 0);
double[] result = sort(arr);
arrayPrint(result);
}
}
/** java中从控制台输入一些整数然后求这些整数的最大最小值要求用while或do-while来做输入的循环。 */
class MaxMinNumber {
public static void doMain() {
try {
Scanner s = new Scanner(System.in);
System.out.print("请输入要输入的数字序列的最大个数:");
int size = s.nextInt();
int[] iArray = new int[size];
int max = 0;
int min = 0;
int temp;
String inputStr = "";
System.out.println("开始输入要比较的数字序列,输入 '000' 表示结束,不计入.最多输入 " + size
+ " 个数字.");
int count = -1;
while (true) {
count++;
if (count >= size) {
break;
}
System.out.print("请输入第 " + (count + 1) + " 个数字:");
inputStr = s.next();
if ("000".equals(inputStr)) {
break;
}
temp = Integer.parseInt(inputStr);
iArray[count] = temp;
if (count == 0) {
max = min = temp;
} else {
if (max < temp) {
max = temp;
}
if (min > temp) {
min = temp;
}
}
}
for (int i : iArray) {
System.out.print(i + "\t");
}
System.out.println("\nmax === " + max + " ; min === " + min);
} catch (InputMismatchException ime) {
System.out.println("exception : 必须输入数字!程序已结束... "
+ ime.getMessage());
} catch (NumberFormatException nfe) {
System.out.println("exception : 必须输入数字!程序已结束... "
+ nfe.getMessage());
} catch (Exception e) {
System.out.println("exception : 程序已结束... " + e.getMessage());
}
}
}
展开全部
没有 楼下说的猜稿 那么复杂陵此,就首先放进数组 ,但必须校验 必须是数字穗汪孝!因为你要排序的!
然后 进行冒泡排序后 输出数据 便可!
然后 进行冒泡排序后 输出数据 便可!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你就把所有得到的放进数组里就好啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询