翁恺老师java慕课学习【Day4】

单词长度实例

package lianxi;

import java.util.Scanner;

public class dancichangdu {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        String a = in.nextLine();
        int len = a.length();
        int count = 1;
        for(int i = 0;i<len;i++) {
            if(a.charAt(i)=='.') {        
                break;
            }
            else {
                if(a.charAt(i)==' ') {
                    count++;               //单词个数,这里将空格也看成单词
                }
            }
                
        }
        int [] num= new int [count];      //利用数组,记录每个单词的位置及长度
        int j = 0;
        for(int i=0;i<len;i++) {
            if(a.charAt(i)=='.') {
                break;
            }
            else {
                if(a.charAt(i)!=' ') {
                    num[j]++;                       //每个单词长度,数组自动初始化为0
                }
                else {
                    j++;
                }
            }
        }
        for(int i=0;i<count;i++) {
            if(num[i]!=0) {
                System.out.print(num[i]);
            }
            if(i!=count-1&&num[i]!=0) {              //每个单词后输出空格,最后一个单词不输出
                System.out.print(' ');
            }
        }
        
    }

}

GPS数据处理实例

package lianxi;

import java.util.Scanner;

public class GPSshujvchuli {

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        
        String words = new String(in.nextLine());
        
        String hhh = null;       //现在外面定义字符串并初始化,在里面也能用。如在里面定义,则不能在外面使用,所定义变量会消失
        String mmm = null;
        String sss = null;
        
        while(!words.equals("END")) {       //校验此行语句是否为END
            if(!words.startsWith("$GPRMC")) {
                words = in.nextLine();
                continue;       //结束当前循环
            }
            
            int index = 1;
            char a = words.charAt(index);       words.charAT(n)方法:读取第n位上的一个字符
            int sum = 0;
            while(a != '*') {
                sum ^=a;
                index++;
                a = words.charAt(index);
            }
            sum %= 65536;
            
            int check = Integer.parseInt(words.substring(index+1),16);       //Integer.parseInt(a,n)方法: 将字符串a转化成n进制的数字
            if(check!=sum) {                                                 //words.substring(n)方法:读取字符串words第n位到末尾的全部字符
                words = in.nextLine();                                       //字符串从0开始排名      
                continue;
            }
            
            String [] part = words.split(",");       //words.split("x")方法:以符号x为分隔符,将字符串words分割成若干字符串
            if(!part[2].equals("A")) {      //part[2].equals("a")方法:判断字符串part[2]是否与字符串a相同
                words = in.nextLine();
                continue;
            }
            
            int hh = 0;
            int mm = 0;
            int ss = 0;
            
            
            hh = Integer.parseInt(part[1].substring(0,2));
            mm = Integer.parseInt(part[1].substring(2,4));
            ss = Integer.parseInt(part[1].substring(4,6));
            int h = (hh+8)%24;       //将UTC时间转化成北京时间(跨日)
            if (h<10) {
                hhh = new String("0"+Integer.toString(h));       //确保都是以两位数输出,也可用printf来格式化输出
            }
            else {
                hhh = Integer.toString(h);
            }
            if(mm<10) {
                mmm = new String("0"+Integer.toString(mm));
            }
            else {
                mmm = Integer.toString(mm);
            }
            if(ss<10) {
                sss = new String("0"+Integer.toString(ss));
            }
            else {
                sss = Integer.toString(ss);
            }
            
            words = in.nextLine();
            
        }
        
        System.out.print(hhh+":"+mmm+":"+sss);
        
        

    }

}

七、函数

7.1函数的定义与调用

public static void sum(int a,int b){
return; }

void——不返回值,不能使用带值的return,可以没有return,return表示结束函数的运行,并返回一个值

sum——函数名称

(int a, int b)——()里面为参数,需要正确的数量与顺序。如没有参数,也要加()

7.2函数参数与函数内的变量

本地变量:定义在{}内的变量,在{}外并不存在

分解质因数实例

package lianxi;

import java.util.Scanner;

public class fenjiezhiyinshu {
    
    public static boolean isPrime(int i) {       //判断一个数是否为素数
        boolean isPrime = true;
        for(int k=2;k<i;k++) {
            if(i%k==0) {
                isPrime = false;
                break;
            }
        }
        return isPrime;
    }
    
    public static int print(int n) {
        for(int i=2;i<100000;i++) {       //从i=2开始,分解为i与另一个数n两项相乘的形式
            if(isPrime(i)) {
                n=n/i;
                System.out.print(i+"x");       //在i后加上x,最后的n是素数,直接结束程序,不用加x
                if(isPrime(n)) {
                    System.out.print(n);
                    break;
                }
                else {
                    break;
                }
                
            }
        }
        return n;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if(isPrime(n)) {
            System.out.print(n+"="+n);
        }
        else {
            System.out.print(n+"=");       
            while(!isPrime(n)) {       //若n不为素数,则从i=2再次分解成两项
                n=print(n);
            }
        }

    }

}

 

posted on 2022-01-16 20:05  冬马和纱  阅读(50)  评论(0)    收藏  举报