0

我有以下代码:

    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;”的结尾 我不知道为什么,我只是想为自己开发这个。我以后可能会做一些工作。如果有人能告诉我为什么,那将有很大帮助。

4

3 回答 3

4
Scanner in = new Scanner;

应该:

Scanner in = new Scanner(System.in);
于 2013-04-04T19:45:56.447 回答
4
Scanner in = new Scanner(System.in);
于 2013-04-04T19:48:50.053 回答
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.");
 }
于 2013-04-04T19:49:01.133 回答