实验三 分支、循环、字符串和数组学习

实验内容:

  1. 编写一个Java程序,实现下列功能:
  1. 程序随机分配给客户一个1~100之间的整数,用户输入自己的猜测,程序返回提示信息“猜大了”,“猜小了”或“猜对了”,反复输入直到猜对为止。并显示几次猜对。代码运行结果如图
  2. 提示:随机数产生代码:(int)(Math.random()*100)+1

 

  1. 从键盘输入一个字符串,判断该字符串是否是回文,如果是回文,将该数转换成整形数,并显示出来。 
  1. 提示:判断字符串的第一个字符和最后一个字符是否相同,如果是,判断第二个和倒数第二个,一直找到不匹配字符,或者字符串中所有字符都进行了判断,如果是奇数个字符,那么中间的就不进行判断
  2. 字符串转换为整数:Integer.parseInt(s)
  3. 要用到的函数:s.length   s,charAt    s是字符串

 

 

  1. 从键盘输入5个城市的英文名称保存到字符串数组中,编写程序显示按字母顺序最小的字符串。如果输入城市中有“上海”,显示“上海是中国最大的城市”,按顺序显示这五个城市,输入其中一个要检索的城市,并输出检索城市在数组中的位置,执行结果如下图
  1. 提示:2个字符串a和b比较可以用:a.compareTo(b)
  2. 将数组a排序可以用: Arrays.sort(a) 
  3. 在数组a中用折半查找法找x可以用:Arrays.binarySearch(a,x)

 

 

 

import java.util.Scanner;

public class Random {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number=(int)(Math.random()*100)+1;
        int guess,count=0;
        do{
            System.out.print("请输入猜测的数字:");
            guess=input.nextInt();
            count++;
            if(guess>number)
                System.out.println("猜大了");
            else if(guess<number)
                System.out.println("猜小了");
        }while(guess!=number);
        System.out.println("猜对了,共猜测了"+count+"次");
    }
}
import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a string:");
        String str=input.next();
        int low=0;
        int high=str.length()-1;
        boolean flag=true;
        while (low<high){
            if(str.charAt(low)!=str.charAt(high)){
                flag=false;
                break;
            }
            low++;
            high--;
        }
        int number=Integer.parseInt(str);
        if (flag)
            System.out.println(number+"是回数");
        else
            System.out.println(number+"不是回数");
    }
}
import java.util.Arrays;
import java.util.Scanner;


public class StringTest {
    public static void main(String[] args) {
        String [] city=new String[5];
        Scanner input=new Scanner(System.in);
        System.out.println("请输入五个城市名:");
        for(int i=0;i<5;i++){
            city[i]=input.next();
        }
        String min=city[0];
        for(int i=1;i<5;i++){
            if(city[i].compareTo(min)<0)
                min=city[i];
        }
        System.out.println("最前的城市是 "+ min);
        for(int i=0;i<5;i++){
            if(city[i].equals("shanghai"))
                System.out.println("上海是中国最大的城市 ");
        }
        Arrays.sort(city);
        System.out.println(Arrays.deepToString(city));
        System.out.println("请输入要检索的城市:");
        input.nextLine();
        String str=input.nextLine();
        int index=Arrays.binarySearch(city,str)+1;
        System.out.println("检索的城市的位置是"+index);



    }
}
import java.util.Arrays;

public class PrimeNumber {
    public static void main(String[] args) {
        int count=0;
        int number=2;
        System.out.println("前50个素数是:");
        while(count<50){
            boolean isPrime=true;
            for(int div=2;div<=Math.sqrt(number);div++){
                if(number%div==0){
                    isPrime=false;
                    break;
                }
            }
            if(isPrime){
                count++;
                if(count%10==0){
                    System.out.println(number+"\t");
                }else{
                    System.out.print(number+"\t");
                }

            }
            number++;
        }

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43560803/article/details/85724841
今日推荐