7-20 jmu-Java-01入门-格式化输入输出与字符串 (10 分)
题目
点击查看题目
输入double,然后输入3个浮点数。输出:从左到右依次输出3个double(均保留2位小数输出,宽度为5),格式依次为:右侧填充空格,左侧填充空格,直接输出
输入int,然后输入3个整数(以1个或多个空格分隔)。输出:将3个整数相加后输出。
输入str,然后输入3个字符串。输出:去除空格,然后倒序输出3个字符。
输入line,然后输入一行字符串。输出:转换成大写后输出。
如果输入不是上面几个关键词,输出:输出other。
输出说明 choice=你输入选项
提示:
可使用line.split("\\s+");
将以1个或多个空格分隔开的字符串分割并放入字符串数组。
Scanner.nextLine
与Scanner的其他next函数混用有可能出错。
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(sc.hasNext()) { String s=sc.next(); if(s.compareTo("line")==0) { sc.nextLine();//关键所在 //***********************看上面************************// String t=sc.nextLine(); t=t.toUpperCase(); System.out.println("choice="+s); System.out.println(t); } else if(s.compareTo("str")==0) { String t1=sc.next(); String t2=sc.next(); String t3=sc.next(); System.out.println("choice="+s); System.out.println(t3+t2+t1); } else if(s.compareTo("double")==0) { double a1=sc.nextDouble(); double a2=sc.nextDouble(); double a3=sc.nextDouble(); System.out.println("choice="+s); System.out.printf("%-5.2f,%5.2f,%.2f\n",a1,a2,a3); } else if(s.compareTo("int")==0) { int a1=sc.nextInt(); int a2=sc.nextInt(); int a3=sc.nextInt(); System.out.println("choice="+s); System.out.println(a1+a2+a3); } else { System.out.println("choice="+s); System.out.println("other"); } } sc.close(); } }
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15517491.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步