java中string s=9.9怎么转换为double
时间: 2024-05-02 15:18:30 浏览: 69
可以使用Double.parseDouble()方法将字符串转换为double类型。如下所示:
```
String s = "9.9";
double d = Double.parseDouble(s);
```
注意,如果字符串s中包含非数字字符,则会抛出NumberFormatException异常。
相关问题
java String集合统计大于某个Double数值有多少个
如果你要统计字符串集合中大于某个Double数值的元素个数,可以将字符串转换为Double类型后再进行比较。具体实现可以参考如下代码:
```java
List<String> list = Arrays.asList("1.1", "2.2", "3.3", "4.4", "5.5", "6.6", "7.7", "8.8", "9.9");
double target = 5.0; // 某个Double数值
long count = list.stream().mapToDouble(Double::valueOf).filter(num -> num > target).count();
System.out.println(count); // 输出大于某个Double数值的元素个数
```
这段代码中,我们首先将字符串集合转换为Double类型的Stream,然后使用filter()方法过滤掉小于等于某个Double数值的元素,最后使用count()方法统计个数。需要注意的是,如果字符串中包含非数字字符,转换为Double类型时会抛出NumberFormatException异常。
歌手大赛计算选手成绩的方法是去掉一个最高分和一个最低分后再计算平均分,而学校评估一个班级的学生成绩的平均分时,是计算全班同学的平均得分。SongGame和School类都实现了ComputeAverage接口,但实现方式不同。 1)编写接口ComputeAverage,声明一个public double average(double x[])方法 2)编写SongGame类,实现ComputeAverage接口 3)编写School类 实现ComputeAverage接口 4)编写主类Application,给定一组10个学生成绩数据a[]和一个歌手的10个得分b[],分别计算学生平均分和歌手最后得分并输出java
1)ComputeAverage接口代码:
```
public interface ComputeAverage {
public double average(double x[]);
}
```
2)SongGame类代码:
```
public class SongGame implements ComputeAverage {
public double average(double x[]) {
double max = x[0];
double min = x[0];
double sum = 0;
for (int i = 0; i < x.length; i++) {
if (x[i] > max) {
max = x[i];
}
if (x[i] < min) {
min = x[i];
}
sum += x[i];
}
return (sum - max - min) / (x.length - 2);
}
}
```
3)School类代码:
```
public class School implements ComputeAverage {
public double average(double x[]) {
double sum = 0;
for (int i = 0; i < x.length; i++) {
sum += x[i];
}
return sum / x.length;
}
}
```
4)主类Application代码:
```
public class Application {
public static void main(String[] args) {
double[] a = {80, 90, 85, 95, 70, 75, 88, 92, 86, 79};
double[] b = {9.5, 9.8, 9.3, 9.7, 9.2, 9.0, 9.6, 9.4, 9.1, 9.9};
ComputeAverage studentAverage = new School();
ComputeAverage singerAverage = new SongGame();
double studentAvg = studentAverage.average(a);
double singerAvg = singerAverage.average(b);
System.out.println("学生成绩平均分:" + studentAvg);
System.out.println("歌手最后得分:" + singerAvg);
}
}
```
输出结果为:
```
学生成绩平均分:84.0
歌手最后得分:9.48
```
阅读全文