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