为什么在输入y时我的代码会继续?

时间:2015-07-01 23:16:47

标签: java

我是编程(Java)和堆栈溢出的新手。我的代码工作,除非我运行它并按 y 它不断重复"我想继续"并陷入无处循环(其他一切正常)。有人可以告诉我为什么会这样,我该怎么做才能解决它。

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class ch03_ex3_DownloadTime {

    public static void main(String[] args) {
        int dSpeed = 0;
        int fSize = 0;
        Scanner sc = new Scanner(System.in);

        { // get the input from the user
            System.out.println("Welcome to the Download Time Estimator");
            System.out.print("Enter file size (MB): ");
            fSize = sc.nextInt();
            System.out.print("Enter download speed (MB/sec): ");
            dSpeed = sc.nextInt();
        }
        int dTime = fSize / dSpeed;
        int hours;
        int remainder;
        remainder = dTime % 3600;
        hours = dTime / 3600;
        int minutes;
        int seconds = remainder % 60;
        minutes = remainder / 60;
        String message = "\n" +
            "This download will take approximately " + hours + " hours " + minutes + " minutes " + seconds + " seconds \n";
        System.out.println(message);

        boolean isContinuing = true;
        boolean inputIsInvalid = true;
        while (inputIsInvalid) {
            System.out.print("Continue? (y/n): ");

            String choice = sc.next();

            if ("y".equalsIgnoreCase(choice)) {
                inputIsInvalid = false;
            } else if ("n".equalsIgnoreCase(choice)) {
                inputIsInvalid = false;
                isContinuing = false;
            } else {
                System.err.print("Error: Only valid answers are Y/N. ");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class ch03_ex3_DownloadTime {

public static void main(String[] args)
{
test t=new test();
t.chk();

}


}



class test{
    public void chk(){
    int dSpeed = 0;
        int fSize = 0;
        Scanner sc = new Scanner(System.in);



        {         // get the input from the user
            System.out.println ("Welcome to the Download Time Estimator");
            System.out.print("Enter file size (MB): ");
            fSize = sc.nextInt();
            System.out.print("Enter download speed (MB/sec): ");
            dSpeed = sc.nextInt();
     }
        int dTime = fSize / dSpeed;
        int hours;
        int remainder;
        remainder= dTime % 3600;
        hours = dTime / 3600;
        int minutes;
        int seconds= remainder % 60;
        minutes = remainder / 60;
        String message = "\n" +
                         "This download will take approximately " + hours +  " hours "
                       + minutes  + " minutes "
                       + seconds + " seconds \n";
        System.out.println(message);

    System.out.println("do you want to retry");
    System.out.println("press 1 if yes");
    System.out.println("press 2 if no");

    int a=sc.nextInt();

    if(a==1)
    {
    chk();
    }

    else
    {
    System.out.println("thanks");
    }


    }
    }