正在使用变量?
Variables are being used?
我在同一问题上看到了一些帖子,所以如果我错过了阅读内容,我深表歉意,但这个问题真的让我很烦。所以我这里有这段代码:
int megaBytes = 0;
int megaByteSpeed = 0;
int totalSeconds = 0;
int secondsRemainder = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
Scanner sc = new Scanner(System.in);
和
if (megaBytes < 1000)
{
totalSeconds = megaBytes / megaByteSpeed;
hours = totalSeconds / 3600;
secondsRemainder = totalSeconds % 3600;
minutes = secondsRemainder / seconds;
seconds = minutes % 60;
}
我知道我的编码可能有很多错误,但我是 Java 的新手,我开始有点慌张。
我得到的错误是说变量; megaByteSpeed
、totalSeconds
和 secondsRemainder
未被使用。它们在数学部分很明显。有谁知道为什么会这样?这导致我的项目在 运行.
中途停止
感谢您提供任何信息,
斯库尔。
完整代码:
public class Ch03_ex3_DownloadTime {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println("Welcome to the Download Time Estimator!");
System.out.println();
int megaBytes = 0;
int megaByteSpeed = 0;
int totalSeconds = 0;
int secondsRemainder = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
Scanner sc = new Scanner(System.in);
while (megaBytes != 1000)
{
System.out.print("Enter download size: ");
megaBytes = sc.nextInt();
System.out.print("Enter download speed: ");
megaByteSpeed = sc.nextInt();
if (megaBytes < 1000)
{
totalSeconds = megaBytes / megaByteSpeed;
hours = totalSeconds / 3600;
secondsRemainder = totalSeconds % 3600;
minutes = secondsRemainder / seconds;
seconds = minutes % 60;
}
String message = "\n"
+ "This download will take approximately " + hours + "hours" + minutes + "minutes and " + seconds + "seconds";
System.out.println(message);
}
}
}
首先让我给你一个提示:按照网站说明如何post编码。
现在,根据我从你的代码中得到的信息,我看到你除以 0
这里:
minutes = secondsRemainder / seconds;
这是您需要更改以停止循环的部分
boolean quit = false;
while (!quit)
{
System.out.print("Enter download size: ");
megaBytes = sc.nextInt();
System.out.print("Enter download speed: ");
megaByteSpeed = sc.nextInt();
if (megaBytes < 1000)
{
totalSeconds = megaBytes / megaByteSpeed;
hours = totalSeconds / 3600;
secondsRemainder = totalSeconds % 3600;
minutes = secondsRemainder / seconds;
seconds = minutes % 60;
}
String message = "\n"
+ "This download will take approximately " + hours + "hours" + minutes + "minutes and " + seconds + "seconds";
System.out.println(message);
System.out.println("Do you want to continue 1= yes 0 = no");
int ans = 0;
ans = sc.nextInt();
if(ans ==0){
quit = true;
}
}
我在同一问题上看到了一些帖子,所以如果我错过了阅读内容,我深表歉意,但这个问题真的让我很烦。所以我这里有这段代码:
int megaBytes = 0;
int megaByteSpeed = 0;
int totalSeconds = 0;
int secondsRemainder = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
Scanner sc = new Scanner(System.in);
和
if (megaBytes < 1000)
{
totalSeconds = megaBytes / megaByteSpeed;
hours = totalSeconds / 3600;
secondsRemainder = totalSeconds % 3600;
minutes = secondsRemainder / seconds;
seconds = minutes % 60;
}
我知道我的编码可能有很多错误,但我是 Java 的新手,我开始有点慌张。
我得到的错误是说变量; megaByteSpeed
、totalSeconds
和 secondsRemainder
未被使用。它们在数学部分很明显。有谁知道为什么会这样?这导致我的项目在 运行.
感谢您提供任何信息, 斯库尔。
完整代码:
public class Ch03_ex3_DownloadTime {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println("Welcome to the Download Time Estimator!");
System.out.println();
int megaBytes = 0;
int megaByteSpeed = 0;
int totalSeconds = 0;
int secondsRemainder = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
Scanner sc = new Scanner(System.in);
while (megaBytes != 1000)
{
System.out.print("Enter download size: ");
megaBytes = sc.nextInt();
System.out.print("Enter download speed: ");
megaByteSpeed = sc.nextInt();
if (megaBytes < 1000)
{
totalSeconds = megaBytes / megaByteSpeed;
hours = totalSeconds / 3600;
secondsRemainder = totalSeconds % 3600;
minutes = secondsRemainder / seconds;
seconds = minutes % 60;
}
String message = "\n"
+ "This download will take approximately " + hours + "hours" + minutes + "minutes and " + seconds + "seconds";
System.out.println(message);
}
}
}
首先让我给你一个提示:按照网站说明如何post编码。
现在,根据我从你的代码中得到的信息,我看到你除以 0 这里:
minutes = secondsRemainder / seconds;
这是您需要更改以停止循环的部分
boolean quit = false;
while (!quit)
{
System.out.print("Enter download size: ");
megaBytes = sc.nextInt();
System.out.print("Enter download speed: ");
megaByteSpeed = sc.nextInt();
if (megaBytes < 1000)
{
totalSeconds = megaBytes / megaByteSpeed;
hours = totalSeconds / 3600;
secondsRemainder = totalSeconds % 3600;
minutes = secondsRemainder / seconds;
seconds = minutes % 60;
}
String message = "\n"
+ "This download will take approximately " + hours + "hours" + minutes + "minutes and " + seconds + "seconds";
System.out.println(message);
System.out.println("Do you want to continue 1= yes 0 = no");
int ans = 0;
ans = sc.nextInt();
if(ans ==0){
quit = true;
}
}