test01.
写代码实现如下效果的九九乘法表 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 ....... 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
public class test01 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) { //总共9行
for (int j = 1; j <= i ; j++) { //第 i 行有 j 列
System.out.print(j + "*" + i + "=" + (j*i) + "\t"); // \t 制表符
}
System.out.println(); //每一行输出结束后换行
}
}
}
test02.
判断101-200之间有多少个素数,并输出所有素数。(只能被1和它本身整除的自然数 为素数)
public class test02 {
public static void main(String[] args) {
int count = 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){
count ++;
System.out.println("素数为:"+i);
}
}
System.out.println("素数个数为:"+count);
}
}
test03.
有数组 String[] s=new String[]{"hello","word","!"}; ,请使用不同的方式进行遍历
public class test03 {
public static void main(String[] args){
String[] s=new String[]{"hello","word","!"};
for (int x=0;x<s.length;x++){
System.out.print(s[x]+"\t");
}
System.out.println();
for (String value : s){
System.out.print(value);
}
}
}
test04.
设计一个方法求一组数的平均值和最大值
public class test04 {
public static void main(String[] args){
int[] arr = {10,20,30,40,50};
int max = arr[0];
int sum = 0;
for (int i = 0;i<arr.length;i++){
sum += arr[i];
if (max<arr[i]){
max = arr[i];
}
}
System.out.println("平均值为:"+sum/ arr.length);
System.out.println("最大值为:"+max);
}
}
test05.
产生10个随机数,范围在[1,100] 使用Math.random()来实现
public class test05 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
double d = Math.random() * (100 - 1 + 1) + 1;
System.out.println(d);
}
}
}
test06.
随机产生一个长度为4位的验证码,包含大小写字母以及数字
public class test06 {
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char[] data;
public void init(){
data = s.toCharArray();//字符串对象中的字符转换为一个字符数组
}
public String getRandom(){
char[] randomChars = new char[4];
int ranDom;
for (int i=0;i<randomChars.length;i++){
ranDom = (int) (Math.random()*data.length);
randomChars[i] = data[ranDom];
}
return new String(randomChars);
}
public static void main(String[] args){
test06 rd = new test06();
rd.init();
String rs = rd.getRandom();
System.out.println("验证码:"+rs);
}
}
test07.
有一组手机号,随机抽取一个中奖号码
public class test07 {
public static void main(String[] args){
String[] phoneNum = {"15261712533","13878964123","19785423697","19512386475","13578542395"};
int randInput = (int) (Math.random()* phoneNum.length);//Math.random()提供的类型double,需要的类型int
System.out.println("中奖号码为:"+phoneNum[randInput]);
}
}