我认为JAVA不是学会的,而是写会的,多做题多增长经验才能锻炼你的写代码能力,我最近在网上又找了几道适合初学者联系JAVA的习题供大家参考:
1.随机一个数字然后由人输入数据猜数字,猜大了猜小了都输出提示信息,5次机会,猜不出来就失败;
package f;
import java.util.Random;
import java.util.Scanner;
public class A2 {
public static boolean compare(Scanner sc,int target,int[] guess,int i) {
boolean res = false;
while(true) {
System.out.println("第"+(i+1)+"次猜的数字为:");
String ss=sc.nextLine();
try {
int in=Integer.parseInt(ss);
if(in>target) {
System.out.println("大");
guess[i]=in;
}else if(in<target) {
System.out.println("小");
guess[i]=in;
}else
res=true;
break;
} catch (Exception e) {
// TODO: handle exception
System.out.println("请输入整数");
}
}
return res;
}
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
Random r= new Random();
int target=r.nextInt(71)+10;
int guess[]=new int[5];
boolean res=false;
for(int i=0;i<guess.length;i++) {
res=compare(input, target, guess, i);
if(res) {
System.out.println("成功!");
break;
}
}
if(!res) {
System.out.println("失败!");
for(int temp:guess) {
System.out.print(temp+"\t");
}
}
}
}
2.把整形数字分开输出
package f;
public class A3 {
public static void main(String[] args) {
int num = 1234;
String str = "" + num;
// 不太建议的方法1
String ss = "";
for (int i = 0; i < str.length(); i++) {
ss = str.charAt(i) + ss;
}
System.out.println(ss);
// 方法2
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
sb.insert(0, str.charAt(i));
}
System.out.println(sb.toString());
// 方法3
StringBuilder sb2 = new StringBuilder(str);
sb2.reverse();
System.out.println(sb2.toString());
}
}
3.判断回文数字
package f;
public class A4 {
public static void main(String[] args) {
int kk = 1233210;
String str=""+kk;
String revstr=new StringBuilder(str).reverse().toString();
boolean bb=str.equals(revstr);
if(bb) {
System.out.println("是回文数字");
}else {
System.out.println("不是回文数字");
}
}
}
4.随机一个数组然后找到其中的最小值
package f;
import java.util.Random;
public class A8 {
public static void main(String[] args) {
int[] arr=new int[10];
Random r= new Random();
for(int i=0;i<arr.length;i++)
arr[i]=r.nextInt();
for(int temp:arr)
System.out.print(temp+"\t");
System.out.println();
int min=Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++) {
if(arr[i]<min) {
min=arr[i];
}
}
System.out.println(min);
}
}
5.用数组存储是十个数,然后去掉其中的最大值和最小值,算出平均数
package f;
import java.util.Scanner;
public class A9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] arr = new double[10];
for (int i = 0; i < arr.length; i++)
inputNumber(arr, i, sc);
for (double temp : arr)
System.out.println(temp);
double min = min(arr);
double max = max(arr);
double res = 0;
for (double temp : arr)
res += temp;
res -= min;
res -= max;
res /= 8;
System.out.println("平均分" + res);
}
//找出最大值
public static double max(double[] arr) {
// TODO Auto-generated method stub
double res = Double.MIN_VALUE;
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > res)
res = arr[i];
}
}
return res;
}
//找出最小值
public static double min(double[] arr) {
// TODO Auto-generated method stub
double res = Double.MAX_VALUE;
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] < res)
res = arr[i];
}
}
return res;
}
//输入值
public static void inputNumber(double[] arr, int i, Scanner sc) {
// TODO Auto-generated method stub
while (true) {
System.out.println("请打分");
String ss = sc.nextLine();
try {
double d = Double.parseDouble(ss);
if (d >= 0 && d <= 10) {
arr[i] = d;
break;
}
System.out.println("输入数据不合理");
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入数据不合法");
}
}
}
}
6.定义一个数字放入大写字母然后用循环把内容换成小写字母
package f;
public class A14 {
public static void main(String[] args) {
char a = 'A';
char[] arr = new char[26];
shuru(arr, a);
for (char trmp : arr)
System.out.print(trmp + "\t");
zhuanhua(arr);
System.out.println();
for (char trmp : arr)
System.out.print(trmp + "\t");
}
public static void zhuanhua(char[] arr) {
// TODO Auto-generated method stub
for (int i = 0; i < arr.length; i++) {
arr[i] = (char) (arr[i] + 32);
}
}
public static void shuru(char[] arr, char a) {
// TODO Auto-generated method stub
for (int i = 0; i < arr.length; i++) {
arr[i] = (char) (a + i);
}
}
}