我编写了一个简单的命令行程序,用于确定通过给定接口传输大文件需要多长时间。我不明白为什么,但它总是产生这样的输出:无限秒或无限分钟传输10.0千兆字节/10240.0兆字节。
我不明白为什么它会给我输出。
import java.util.Scanner;
public class TransferTime {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double speed = 0;
double gigabytes = 10;
System.out.println("This will help you find out how long it will take to transfer a file");
System.out.println();
System.out.println("How many gigabytes do you want to transfer?");
gigabytes = input.nextDouble();
System.out.println("Enter 2 for USB 2, 3 for USB 3, f for FireWire 800 or t for Thunderbolt");
String connection = input.next();
System.out.println();
if (connection == "2") {
speed = 480;
} else if (connection == "f")
speed = 800;
else if (connection == "3") {
speed = 625;
} else if (connection == "f") {
speed = 100;
} else if (connection == "t") {
speed = 1280;
}
double megabytes = gigabytes * 1024;
double seconds = megabytes / speed;
double minutes = seconds / 60;
System.out.println("It will take " + seconds + " seconds or " + minutes
+ " minutes to transfer " + gigabytes + " gigabytes/"
+ megabytes + " megabytes");
input.close();
}
}
答案 0 :(得分:2)
除以0会给你无穷大。你除以0是因为速度保持为0,因为所有条件都是假的(提示 - 不要将字符串与==进行比较)。