我有以下代码:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class weatherCalc {
public static void main(String args[]) {
while (true) {
int division = 8125/1000;
Scanner in = new Scanner;
System.out.println("How far, in inches, is it moving on a 50-mile = 0.75 in? (Please use decimels)");
int weatherInput = in.nextInt();
System.out.println("How long is the time period in hours? (Please use decimels)");
int weatherTime = in.nextInt();
int weatherOutput = weatherInput/division*50/weatherTime;
System.out.println("The storm is travling at "+ weatherOutput +"MPH.");
}
}
}
你看,“System.out.println中的系统”(以英寸为单位,它在50英里= 0.75英寸处移动?(请使用分米)“);”带有红色的下划线以及“Scanner in = new Scanner”的结尾;我无法弄清楚为什么,我只是想为自己开发这个。我稍后可能会对它进行一些研究。如果有人能告诉我原因,那会很有帮助。
答案 0 :(得分:4)
Scanner in = new Scanner;
应该是:
Scanner in = new Scanner(System.in);
答案 1 :(得分:4)
Scanner in = new Scanner(System.in);
答案 2 :(得分:1)
你的while循环应该是这样的:(观察针对每个更改过的代码指定的注释)
Scanner in = new Scanner(System.in);//Move the Scanner declaration outside loop . Don't create it every-time within the loop.
while (true) {
int division = 8125/1000;
System.out.println("How far, in inches, is it moving on a 50-mile = 0.75 in? (Please use decimels)");
int weatherInput = in.nextInt();
System.out.println("How long is the time period in hours? (Please use decimels)");
double weatherTime = in.nextDouble();//Take double value from input as u have specified that you want input in decimals.
double weatherOutput = weatherInput/division*50/weatherTime;//Changed weatherOutput type to double so that you get the result in double.
System.out.println("The storm is travling at "+ weatherOutput +"MPH.");
}