2013-03-19 61 views
-2

谢谢你的建议,这是我第一次在stackoverflow上,因为我是编程的新手。我遇到的问题是我的程序在执行while循环后没有要求输入名称。Java while循环它没有问循环后的第一个提示

import java.util.*; 
import java.lang.*; 

/** 
* 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("/n 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 
      double mouseWeight = 30; 
      double lethalDoseMouse = 100; 
      final double MOUSE_LETHAL_PROPORTION = 
       (double) lethalDoseMouse/mouseWeight; 

      //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.println("Do you want to repeat the program? yes or no"); 
      again = input.next(); 
     } 
    } 
} 
+3

我会选择对包括我的真实姓名(和同学的名字)在一个论坛上这样;-) – jahroy 2013-03-19 01:52:11

+1

欢迎SO。请阅读[常见问题]和[问]关于编写好的SO问题的提示。你的问题不能只包含代码。你必须解释你有什么问题。 – 2013-03-19 01:52:16

+1

请自行调试。 提示:在while循环中逐位添加代码以查看逻辑错误的位置 – 2013-03-19 01:53:38

回答

0

试试这个:

import java.util.*; 
import java.lang.*; 

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

public class Sweetener { 

    /** 
    * 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 = Double.valueOf(input.nextLine()); 

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

      //finds lethal amout of sweetner for mouse 
      double mouseWeight = 30; 
      double lethalDoseMouse = 100; 
      final double MOUSE_LETHAL_PROPORTION = 
        (double) lethalDoseMouse/mouseWeight; 

      //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"); 
      again = input.nextLine(); 
     } 
    } 
}