初学py 求大佬进来帮帮孩子
  • 板块学术版
  • 楼主BirdBoss
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/9/5 14:53
  • 上次更新2023/11/4 07:39:50
查看原帖
初学py 求大佬进来帮帮孩子
482054
BirdBoss楼主2021/9/5 14:53

java 和 python 到底哪里不一样了?? 题号U136393 大意是要以空格为分割,输出每个字符串长度


这是我java写的,过了

import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception{
	// write your code her

        Scanner read = new Scanner(System.in);
        String s = read.nextLine() + " ";
        read.close();

        int newwordcount = 0;
        //为了记单个单词里字母的数量
        boolean first = true;
        //为了五个换行操作
        int wugehuanhang = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' '){
                if (newwordcount>0){
                    System.out.printf("%d", newwordcount);
                    first = false;
                    newwordcount = 0;
                    wugehuanhang++;
                }
            } else{
                if (newwordcount==0 && !first)
                    System.out.printf(wugehuanhang%5==0 ? "\n":",");
                newwordcount++;

            }

        }

    }
}

这是我py出来的,就20分

string = input() + ' '
len = 0
count = 0
first = True

for each in string:
    if(each == ' '):
        if(len>0):
            print(len,end='')
            first = False
            len = 0
            count += 1
    else:
        if(len==0):
            if(count%5==0):
                if(not first):
                    print()
            else:
                print(',',end='')
        len+=1

几乎是一模一样照着抄的,求大佬提点一下孩子到底是哪里不对了。

2021/9/5 14:53
加载中...