我正在为编程类做这个,当我在jGrasp中编译程序时,我不断收到这些错误:
TimeCalculator.java:94: error: variable dblMinutes might not have been initialized
+ timeFormat.format(dblMinutes) + " minutes, and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
TimeCalculator.java:94: error: variable dblSecondsAfterMinutes might not have been initialized
+ timeFormat.format(dblMinutes) + " minutes, and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
TimeCalculator.java:98: error: variable dblMinutes might not have been initialized
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblHours) + " hours, " + timeFormat.format(dblMinutes) + " minutes, and "
^
TimeCalculator.java:99: error: variable dblSecondsAfterMinutes might not have been initialized
+ timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
TimeCalculator.java:101: error: variable dblMinutes might not have been initialized
else if (dblMinutes >= 1)
^
TimeCalculator.java:102: error: variable dblSecondsAfterMinutes might not have been initialized
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblMinutes) + " minutes and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
6 errors
我的程序应该接受用户输入(以秒为单位的数量)并将其转换为天,小时,分钟和秒。然后应该显示最终金额。
到目前为止,这是我的代码:
/*
This program prompts the user to enter an amount of seconds.
When the user enters an amount, it is stored in a variable and
calculated to how many days, hours, minutes, and seconds there
are in the given amount of seconds. The information is then
output for the user to read.
60 seconds = 1 minute
3600 seconds = 1 hour
86400 seconds = 1 day
*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class TimeCalculator
{
public static void main(String[] args)
{
DecimalFormat timeFormat = new DecimalFormat("#0.#");//used for formatting output
//declaring needed variables
String strStartingSeconds;
double dblStartingSeconds, dblMinutes, dblHours, dblDays, dblSecondsAfterDays, dblSecondsAfterHours, dblSecondsAfterMinutes;
//get input from user for amount of seconds
strStartingSeconds = JOptionPane.showInputDialog("Enter the amount of seconds: ");
//convert input to a double
dblStartingSeconds = Double.parseDouble(strStartingSeconds);
if (dblStartingSeconds >= 86400)//check to see if it is one day or more
{
dblDays = dblStartingSeconds / 86400;//find how many days
dblSecondsAfterDays = dblStartingSeconds % 86400;//calculate how many seconds are left by finding the remainder
if (dblSecondsAfterDays >= 3600)//check to see if there is one hour or more
{
dblHours = dblSecondsAfterDays / 3600;//find how many hours
dblSecondsAfterHours = dblSecondsAfterDays % 3600;//calculate how many seconds are left by finding the remainder
if (dblSecondsAfterHours >= 60)//Check to see if there is one or more minutes
{
dblMinutes = dblSecondsAfterHours / 60;//Calculate how many minutes
dblSecondsAfterMinutes = dblSecondsAfterHours % 60;//calculate how many seconds are left by finding the remainder
}
else
{
dblMinutes = 0;//If there wasn't enough seconds, assign minutes as 0
}
}
else
dblHours = 0;//If there wasn't enough hours, assign hours as 0
}
else
{
dblDays = 0;//assign days as 0 since there wasn't enough seconds
if (dblStartingSeconds >= 3600)//check to see if there is one hour or more
{
dblHours = dblStartingSeconds / 3600;//find how many hours
dblSecondsAfterHours = dblStartingSeconds % 3600;//calculate how many seconds are left by finding the remainder
if (dblSecondsAfterHours >= 60)//Check to see if there is one or more minutes
{
dblMinutes = dblSecondsAfterHours / 60;//Calculate how many minutes
dblSecondsAfterMinutes = dblSecondsAfterHours % 60;//calculate how many seconds are left by finding the remainder
}
else
{
dblMinutes = 0;//assign minutes as 0 since there wasn't enough seconds
}
}
else
{
dblHours = 0;//assign hours as 0 since there wasn't enough hours
if (dblStartingSeconds >= 60)//Check to see if there is one or more minutes
{
dblMinutes = dblStartingSeconds / 60;//Calculate how many minutes
dblSecondsAfterMinutes = dblStartingSeconds % 60;//calculate how many seconds are left by finding the remainder
}
else
{
dblMinutes = 0;//Assign minutes as 0 since there wasn't enough seconds
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblStartingSeconds) + " seconds.");//Displays how many seconds there was
}
}
}
//Display the correct amount of time based on whether or not there was enough seconds for days, hours, or minutes
if (dblDays >= 1)
{
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblDays) + " days, " + timeFormat.format(dblHours) + " hours, "
+ timeFormat.format(dblMinutes) + " minutes, and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
}
else if (dblHours >= 1)
{
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblHours) + " hours, " + timeFormat.format(dblMinutes) + " minutes, and "
+ timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
}
else if (dblMinutes >= 1)
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblMinutes) + " minutes and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
else
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblStartingSeconds) + " seconds.");
System.exit(0);//close application
}
}
我没有开玩笑,花了几个小时看这个,却找不到我做错了什么!我对编程很陌生,所以任何事情都会有所帮助(我可能对专业程序员做了一些愚蠢和明显的事情)。
谢谢, -Cashe
答案 0 :(得分:0)
double dblMinutes = 0;
在此处阅读有关初始化字段的信息:http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
答案 1 :(得分:0)
除了您的初始化问题... java.util.concurrent.TimeUnit是处理转换时间单位时最好的朋友。