Java方法简单学�?2&&黑马p68、p72、p73、p74
来自黑马程序员Java课程p68、p72、p73、p74
复制数组
package leetcode;
import java.util.Scanner;
public class itheima_p72 {
public static void main(String[] args) {
int[] arr = {0,1,2,3,4,5,6,7,8,9,};
System.out.println("请依次输入你想复制的数组索引,第一个数组为开头,第二个数组为结尾: ");
Scanner sc = new Scanner(System.in);
int from = sc.nextInt();
Scanner ac = new Scanner(System.in);
int to = ac.nextInt();
int arr3[] = copyOfRange(arr,from,to);
for (int i = 0; i < arr3.length; i++) {
int a = arr3[i];
System.out.print(a+" ");
}
}
public static int[] copyOfRange(int[] arr,int from,int to){
int index = 0;
int newArr[] = new int[to - from];
for (int i = from; i < to; i++) {
newArr[index] = arr[i];
index++;
}
return newArr;
}
}
求质�?
前面好像也写过一个求质数的,只不过那个是键盘录入来求质数,跟这这个不太一样,这道题一样也是来自黑马程序员Java课程
package leetcode;
/*
判断101-200之间有多少个质数并打印出�?/
*/
public class itheima_p72_2 {
public static void main(String[] args) {
int number = 0;
for (int i = 101; i <= 200; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if(i % j == 0){
flag = false;
break;
}
}
if(flag == true){
System.out.println(i+"为质�?");
number++;
}
}
System.out.println("一共有"+number+"个质�?");
}
}
开发验证码
这里生成随机大小写字母需要用到ASCII码来进行转换,来将大小写字母进行存储到数组中,对其进行随机抽取即�?
package leetcode;
import java.util.Random;
public class itheima_p72_3 {
public static void main(String[] args) {
char[] chs = new char[52];
for (int i = 0; i < chs.length; i++) {
if (i <= 25) {
chs[i] = (char) (97 + i);//例如a的ASCII码为97,则第一次循环为97+0=97,转换后即为a
} else {
chs[i] = (char) (65 + i - 26);//同理
}
}
for (int i = 0; i < chs.length; i++) {
//System.out.print(chs[i] + " ");
}
String result = "";
Random r = new Random();
for (int i = 0; i <= 3; i++) {
int index = r.nextInt(chs.length);
//System.out.print(chs[index]+" ");
result = result + chs[index];
}
int number = r.nextInt(10);
//System.out.println(number);
result = result + number;
System.out.print("生成验证码为: "+result);
}
}
评委打分
package leetcode;
import java.util.Scanner;
public class itheima_p72_4 {
public static void main(String[] args) {
int arr1[] = getScore();
System.out.print("\n最大得分为:" + getMax(arr1));
System.out.print("\n最小得分为:" + getMin(arr1));
int result = (getSum(arr1) - getMax(arr1) - getMin(arr1)) / (arr1.length-2);
System.out.print("\n最终平均得分为: " + result);
}
public static int getSum(int arr[]) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
public static int[] getScore() {
int[] arr = new int[6];
System.out.println("请输入老师们的六个评分:");
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; ) {
int score = sc.nextInt();
if (score >= 0 && score <= 100) {
arr[i] = score;
i++;
} else {
System.out.println("成绩超出范围,请重新录入,当前录入成绩为�?" + (i + 1) + "�?");
}
}
return arr;
}
public static int getMax(int arr[]) {
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int getMin(int arr[]) {
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 doathuwant!