0

感谢您的建议,这是我第一次使用 stackoverflow,因为我对编程还很陌生。我遇到的问题是我的程序在执行 while 循环后不要求输入名称。具体来说,它似乎没有在循环之后执行这一行。 System.out.print("请输入节食者的姓名:"); 字符串名称 = input.nextLine(); 有人可以解释一下我可能会错过使用扫描仪实用程序吗?

import java.util.*;

/**
 * CS-12J
 * Sweetner.java
 * Purpose:
 *
 * @version 1.0 3/18/13
 */

public class Sweetner {

   /**
    * The main method begins the execution of the program
    *
    *@param args not used
    */

    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        String again = "yes";
        while (again.equals("yes")) {
            System.out.print("Enter the dieter's name: ");
            String name = input.nextLine();
            System.out.print("Enter the target weight of the dieter in pounds: ");
            double weightInPounds = input.nextDouble();

            //converts pounds to grams
            final double IBS_TO_GRAMS = 453.59;
            double weightInGrams = weightInPounds * IBS_TO_GRAMS;

            //finds lethal amout of sweetner for mouse
            final double MOUSE_WEIGHT = 30.0;
            final double LETHAL_DOSE = 100.0;
            final double MOUSE_LETHAL_PROPORTION =
                (double) LETHAL_DOSE / MOUSE_WEIGHT;

            //finds lethal amount of sweetner for human of given weight
            double lethalSweetner = MOUSE_LETHAL_PROPORTION * weightInGrams;

            //lethal number of cans
            final double SODA_SWEETNER = 0.001;
            final double SODA_GRAMS = 350;
            double lethalCans = lethalSweetner / (SODA_SWEETNER * SODA_GRAMS);

            //output
            System.out.println("For dieter: " + name);
            System.out.println("Dieter's target weight: " + weightInPounds
                + " pounds");
            System.out.println("Lethal dose in grams of sweetner is: "
                + lethalSweetner);
            System.out.println("Lethal number of cans of soda: "
                + lethalCans);

            //extra credit
            final int CANS_PER_DAY = 15;
            final double DAYS_IN_YEAR = 365.25;
            double yearsToLethal = lethalCans / (CANS_PER_DAY * DAYS_IN_YEAR);
            System.out.println("Years to lethal dose: " + yearsToLethal);
            System.out.print("Do you want to repeat the program? yes or no\n\t\t");
            again = input.next();
        }
    }
}
4

5 回答 5

4

使用 next() 而不是 nextLine() 来获取名称:

String name = input.next(); // to get the name

nextLine() 扫描不可见的换行符,这似乎是用户输入“是”时的情况。因此,如果您想避免使用 next(),只需输入 input.nextLine(); 在循环结束时消耗换行符。

于 2013-03-19T02:57:24.650 回答
0

在循环结束时试试这个:

again = input.nextLine();
于 2013-03-19T02:57:36.880 回答
0

改变你的

String name = input.nextLine();

对此,它将起作用。

String name = input.next();
于 2013-03-19T03:04:27.710 回答
0

input.nextLine() 检索下一个换行符。在您的 System.out.print() 中,您提供了触发 nextLine() 方法的“\n”。

System.out.print("Do you want to repeat the program? yes or no\n\t\t");
again = input.next();

在第二条语句之前,您需要另一个 input.nextLine() ,例如:

System.out.print("Do you want to repeat the program? yes or no\n\t\t");
input.nextLine();
again = input.next();

我希望这能解决你的问题。:)

于 2013-03-19T03:30:43.057 回答
0

如果节食者的名字不能为空

System.out.print("Enter the dieter's name: ");
String name = scanLine.apply( input );

在这里找到扫描线

于 2019-10-06T13:20:14.373 回答