gpt4 book ai didi

java - double 列表,打印字符串

转载 作者:行者123 更新时间:2023-12-01 07:11:23 26 4
gpt4 key购买 nike

我有一个值列表(天气数据),编写该列表的人在没有要报告的值时使用值“9999”。我导入了文本文件并使用以下代码来获取数据并进行编辑:

import java.io.*;
import java.util.*;

public class weatherData {

public static void main(String[] args)
throws FileNotFoundException{
Scanner input = new Scanner(new File("PortlandWeather2011.txt"));
processData(input);
}

public static void processData (Scanner stats){
String head = stats.nextLine();
String head2 = stats.nextLine();
System.out.println(head);
System.out.println(head2);
while(stats.hasNextLine()){
String dataLine = stats.nextLine();
Scanner dataScan = new Scanner(dataLine);
String station = null;
String date = null;
double prcp = 0;
double snow = 0;
double snwd = 0;
double tmax = 0;
double tmin = 0;
while(dataScan.hasNext()){
station = dataScan.next();
date = dataScan.next();
prcp = dataScan.nextInt();
snow = dataScan.nextInt();
snwd = dataScan.nextInt();
tmax = dataScan.nextInt();
tmin = dataScan.nextInt();
System.out.printf("%17s %10s %8.1f %8.1f %8.1f %8.1f %8.1f \n", station, date(date), prcp(prcp), inch(snow), inch(snwd), temp(tmax), temp(tmin));
}
}

}
public static String date(String theDate){
String dateData = theDate;
String a = dateData.substring(4,6);
String b = dateData.substring(6,8);
String c = dateData.substring(0,4);
String finalDate = a + "/" + b + "/" + c;
return finalDate;

}

public static double prcp(double thePrcp){
double a = (thePrcp * 0.1) / 25.4;
return a;
}

public static double inch(double theInch){
double a = theInch / 25.4;
if(theInch == 9999){
a = 9999;
}
return a;
}


public static double temp(double theTemp){
double a = ((0.10 * theTemp) * 9/5 + 32);
return a;
}
}

我遇到的问题是获取值并检查所有出现“9999”的情况,并打印出“----”。我不知道如何获取 double 类型的值并打印出字符串。

此代码获取值并检查值 9999,但不对其执行任何操作。这就是我的问题所在:

public static double inch(double theInch){
double a = theInch / 25.4;
if(theInch == 9999){
a = "----";
}
return a;
}

如果我在这个问题中提供了太多信息,我很抱歉。如果你需要我澄清就问吧。感谢您的帮助!

最佳答案

您需要修改 inch 函数以返回字符串,而不是 double 值。

public static String inch(double theInch){
if(theInch == 9999){
return "----";
}
return Double.toString(theInch/25.4);
}

关于java - double 列表,打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com