网站禁用右键,网上推广方式,做服装外贸的网站建设,建设个人网上银行登录入口官网题目#xff1a; 每个句子有多个单词组成#xff0c;句子中的每个单词的长度都可能不一样#xff0c;我们假设每个单词的长度Ni为该单词的重量#xff0c;你需要做的就是给出整个句子的平均重量。
保留两位小数的输出方法#xff1a;
System.out.printf(%.2f 每个句子有多个单词组成句子中的每个单词的长度都可能不一样我们假设每个单词的长度Ni为该单词的重量你需要做的就是给出整个句子的平均重量。
保留两位小数的输出方法
System.out.printf(%.2f,变量名);// We have imported the necessary tool classes.
// If you need to import additional packages or classes, please import here.public class Main {public static void main(String[] args) {// please define the JAVA input here. For example: Scanner s new Scanner(System.in);// please finish the function body here.// please define the JAVA output here. For example: System.out.println(s.nextInt());Scanner sc new Scanner(System.in);String s sc.nextLine();float wordsWeight 0;float wordsNumber 1;for(int i 0; i s.length(); i){if(s.charAt(i) ) wordsNumber;else wordsWeight;}float res wordsWeight / wordsNumber;System.out.printf(%.2f,res);}
}