java每组数据输出一行_java 新手 求帮助 对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行...

青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。publicstaticvoidmain(String[]args){//TODO自动生...

青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

public static void main(String[] args) {

// TODO 自动生成的方法存根

Scanner c = new Scanner(System.in);

String[] ss = new String[10];

int temp = 0;

boolean flag = true;

while(flag){

String s = c.nextLine();

if(s.matches("\n")){

for(int i=0;i

getAverage(ss[i]);

}

flag = false;

}

ss[temp] = s;

temp++;

}

}

public static void getAverage(String s){

DecimalFormat df = new DecimalFormat("0.00");

if(s.matches("\\d+\\s+")){

String[] s1 = s.split(" ");

int[] i1 = new int[s1.length-1];

for(int i=0;i

i1[i] = Integer.parseInt(s1[i+1]);

}

if(Integer.parseInt(s1[0])==i1.length&&Integer.parseInt(s1[0])>2&&Integer.parseInt(s1[0])<=100){

Arrays.sort(i1);

int temp = 0;

for(int j=1;j

temp+=i1[j];

}

String ss = df.format(temp);

double db = Double.parseDouble(ss);

System.out.println(db/(i1.length-2));

}

}

}

}

编译可以通过,但是按下回车键没反映,啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 Java输出保留小数的结果,可以使用 DecimalFormat 类。具体步骤如下: 1. 首先,需要导入 java.text.DecimalFormat 类。 2. 创建一个 DecimalFormat 对象,并指定输出格式。例如,输出结果保留小数,可以使用以下代码: ``` DecimalFormat df = new DecimalFormat("#.00"); ``` 其中,# 表示可选的数字,. 表示小数点,0 表示必须的数字。 3. 使用 DecimalFormat 对象格式化数字,并输出结果。例如: ``` double num = 3.1415926; System.out.println(df.format(num)); ``` 这段代码将输出 3.14。 需要注意的是,如果要对浮点数进行四舍五入,可以使用 Math.round() 方法。例如: ``` double num = 3.1415926; double result = Math.round(num * 100) / 100.0; System.out.println(result); ``` 这段代码将输出 3.14。 ### 回答2: 在Java中,我们可以使用DecimalFormat类来输出保留小数。 首先,我们需要导入java.text.DecimalFormat类。然后,我们可以创建一个DecimalFormat对象,并通过指定格式来设置保留小数数。格式通常通过使用#字符来代表可选的数字,使用0字符来代表必须存在的数字,使用.字符来代表小数点。 下面是一个例子: ```java import java.text.DecimalFormat; public class DecimalFormatExample { public static void main(String[] args) { double num = 123.456789; DecimalFormat df = new DecimalFormat("0.00"); String result = df.format(num); System.out.println(result); // 输出结果为123.46 } } ``` 在上面的例子中,我们将数字123.456789保留小数后,得到的结果是123.46。 如果希望输出结果中包含千分隔符,可以在格式中使用逗号,像这样: ```java import java.text.DecimalFormat; public class DecimalFormatExample { public static void main(String[] args) { double num = 1234567.89; DecimalFormat df = new DecimalFormat("#,##0.00"); String result = df.format(num); System.out.println(result); // 输出结果为1,234,567.89 } } ``` 在这个例子中,我们将数字1234567.89保留小数并添加千分隔符后,得到的结果是1,234,567.89。 通过使用DecimalFormat类,我们可以方便地进行数字格式化操作并输出保留指定小数数的结果。 ### 回答3: 在Java中,可以使用String.format()方法或者DecimalFormat类来保留小数数。 1. 使用String.format()方法: ``` double num = 3.1415926; String result = String.format("%.2f", num); System.out.println(result); ``` 这段代码会输出"3.14",其中"%.2f"是格式化字符串中的一个符,表示保留小数。 2. 使用DecimalFormat类: ``` import java.text.DecimalFormat; double num = 3.1415926; DecimalFormat df = new DecimalFormat("0.00"); String result = df.format(num); System.out.println(result); ``` 这段代码也会输出"3.14"。在DecimalFormat类中,可以通过指定格式模式来控制保留小数数。在这个例子中,"0.00"表示保留小数。 无论是使用String.format()方法还是DecimalFormat类,都可以通过改变符或者格式模式来调整保留小数数。例如,如果想保留小数,只需要将符或格式模式中的".2"改为".3"即可。 需要注意的是,保留小数数仅对输出的字符串有效,不会改变原始的数值。因此,在进行进一步计算时,仍然需要使用原始的数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值