HELLO WORLD--一起加油(🍺)!|

kingwzun

园龄:3年6个月粉丝:111关注:0

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 中国大陆许可协议进行许可。

posted @   kingwzun  阅读(436)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起