一、生成十个随机数
import java.util.*;
class RandomNumber{
int K;
RandomNumber(int k){
K=k;
}
int[] a=new int[99];
Random random=new Random();
void myArray(){
for (int i=0;i<K;i++){
int j=random.nextInt(100);
if (j<50){
--i;
}else{
a[i]=j;
}
}
}
void print(){
for (int i=0;i<K;i++){
System.out.println(+a[i]);
}
}
}
public class Stochastic {
public static void main(String args[]){
RandomNumber randomNumber=new RandomNumber(10);
randomNumber.myArray();
randomNumber.print();
}
}
运行结果:
二、字母倒叙排列
import java.util.*;
public class Antitone {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
String Cat=scanner.nextLine();
System.out.println(Cat.length());
char Sun[]=Cat.toCharArray();
for (int i=(Cat.length()-1);i>=0;i--){
System.out.print(Sun[i]);
}
}
}
运行结果:
三、判断大写字母
public class StringDemo{
public static void main(String[] args){
int i;
int count=0;
String text="Hello,Java";
System.out.println("字符串内容:"+text);
char chr=text.charAt(0);
if(chr>=65&&chr<=90){
char[] charArr=text.toCharArray();
System.out.println("大写字母有:");
for(i=0;i<charArr.length;i++){
if(charArr[i]>=65&&charArr[i]<=90){
count=count+1;
System.out.println(charArr[i]);
}
}
System.out.println("大写字母的个数:"+count+"\n");
}
else{
System.out.println("第一个字母不是大写字母");
}
}
}
运行结果: