プログラミングにかなり慣れていないので、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();
}
}
}