关于数组方面的JAVA一、 输入五个数字,输出这五个数字中的最大数、最小数、总和、平均值。二、 有15个数存放在一个数组

关于数组方面的JAVA
一、 输入五个数字,输出这五个数字中的最大数、最小数、总和、平均值。
二、 有15个数存放在一个数组中,输入一个数,要求查找出该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”。15个数用赋初值的方法在程序中给出。
三、 某个公司想用电话来传送数据,但又担心被人窃听。数据是由四个0~9的十进制数组成,然后按如下规则加密:每位数字都加上7,然后用和除以10的余数取代该数字;再把第1位与第3位交换,第2位与第4位交换。将加密后的结果输出;再单独编写一个程序,读取一个加密后的四位数字的整数,把它解密成原来的数。
四、 输入五个数字,放置在一个一维数组中,倒置该数组并输出。
五、 打印杨晖三角形(输出若干行,自己决定)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
lifangyi 1年前 已收到1个回答 举报

wyy99 幼苗

共回答了27个问题采纳率:77.8% 举报

import java.util.Scanner;
public class Du {
public static void main(String[] args) {
question1();
question2();
question3();
question4();
question5();
}
private static void question5() {
int number = 5;
int[][] ary = new int[number][];

for(int i = 0; i < ary.length; i++){
ary[i] = new int[i+1];
ary[i][0] = 1;
ary[i][i] = 1;
for(int j = 1; j < ary[i].length - 1; j++){
ary[i][j] = ary[i-1][j] + ary[i-1][j-1];
}
}

for(int i = 0; i < ary.length; i++){
for(int j = 0; j < ary[i].length; j++){
System.out.print(ary[i][j] + " ");
}
System.out.println();
}
}
private static void question4() {
System.out.println("n Please input 5 number, separate with space:");
String[] number = new Scanner(System.in).nextLine().split("\s+");

System.out.print("Reversed numbers are: ");
for(int i = number.length - 1; i >= 0; i--){
System.out.print(number[i] + "t");
}
}
private static void question3() {
System.out.print("nPlease input a number between 0000 and 9999:");
String number = new Scanner(System.in).next();

System.out.println("The number to encode is: " + number);
String encode = encode(number);
System.out.println("Encoded number is: " + encode);
String decode = decode(encode);
System.out.println("Decoded number is: " + decode);
}
private static String decode(String encode) {
StringBuilder sb = new StringBuilder(encode);
char first = sb.charAt(0);
char second = sb.charAt(1);

sb.setCharAt(0, sb.charAt(2));
sb.setCharAt(2, first);
sb.setCharAt(1, sb.charAt(3));
sb.setCharAt(3, second);

encode = sb.toString();
sb = new StringBuilder();

for(int i = 0, value = 0;i < encode.length(); i++){
value = Integer.parseInt(String.valueOf(encode.charAt(i)));
if(value >= 7){
sb.append(value-7);
}else{
sb.append((10 + value)-7);
}
}

return sb.toString();
}
private static String encode(String number) {
String str = String.valueOf(number);
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length(); i++){
sb.append(((str.charAt(i) - 48) + 7) % 10);
}

char first = sb.charAt(0);
sb.setCharAt(0, sb.charAt(2));
sb.setCharAt(2, first);
char second = sb.charAt(1);
sb.setCharAt(1, sb.charAt(3));
sb.setCharAt(3, second);

return sb.toString();
}
private static void question2() {
int[] nums = {1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15};

System.out.print("nPlease input a number to search in the array:");
int input = new Scanner(System.in).nextInt();

for(int i = 0; i < nums.length; i++){
if(nums[i] == input){
System.out.println("Element at position " + (i+1) + "=" + input);
return;
}
}

System.out.println("Not found " + input + " in the ary ");
}
private static void question1() {
System.out.print("Please input 5 numbers, separate with space:");

String[] input = new Scanner(System.in).nextLine().split("\s+");

double sum = 0;
double max = Double.parseDouble(input[0]);
double min = max;
double item = 0;
for(int i= 0; i < input.length; i++){
item = Double.parseDouble(input[i]);
if(item > max){
max = item;
}else{
min = item;
}

sum += item;
}

System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
System.out.println("Sum is: " + sum);
System.out.println("Average is: " + sum / input.length);
}
}
--------------------------testing
Please input 5 numbers, separate with space:11 22 3355 66 9999
Max is: 9999.0
Min is: 66.0
Sum is: 13453.0
Average is: 2690.6
Please input a number to search in the array:12
Element at position 12=12
Please input a number between 0000 and 9999:2345
The number to encode is: 2345
Encoded number is: 1290
Decoded number is: 2345
Please input 5 number, separate with space:
11 22 55 99 623
Reversed numbers are: 623 99 55 22 11 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

1年前

1
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.336 s. - webmaster@yulucn.com