我的代码运行不正常

时间:2017-04-30 13:04:12

标签: java

import java.util.Scanner;
public class NewClass {
public static void main(String[]args){ 
   Scanner input=new Scanner(System.in);
    String productMin=""; 
   System.out.print("How much money do you have? ");
    double money=input.nextDouble();
      double minPrice=0;
         double total=0;
          double productPrice;
           System.out.print("Please, insert  the items in the invoice (the 
     name of product and its price):\n Insert \\\"stop\\\" as the name of 
     product to finish your input \n");
      String productName=input.next();
       productPrice=input.nextDouble();
       total=total+productPrice;

   while(!input.next().equals("stop"))
   {

     if(minPrice<productPrice)          
     {
       minPrice=productPrice;
       productMin=productName;

     }

    productName=input.next();
       total=total+productPrice; 
   }
   if(total<=money)
     {
       System.out.println("You have enough money !");
       System.out.printf("%s is the item with the minimum price(which is SR 
         %.3f)\n",productMin,minPrice);
   }
   else
       System.out.println("You don\'t have enough money");
   System.out.println(total);


     }//end of main method      
  }//end of calss 

这是我的代码,并没有给我产品的最低价格及其名称我尝试了很多东西但仍然没有,而且我已经没时间了,有人可以告诉我这里的错误是什么? ? 这个输出应该是

你有多少钱? 100 请插入发票中的项目(产品名称及其价格):插入“停止”作为产品名称以完成输入  香蕉20.200 水15.300 蜂蜜37.500 停 你有足够的钱 水是最低价格(15.500科威特第纳尔)的项目

这是我的输出 你有多少钱? 100 请在发票中插入物品(产品名称及其价格):  插入“停止”作为产品名称以完成输入 ss 20.100 dd 15.200 ff 37.500 停 你有足够的钱! ss是具有最低价格的项目(SR 20.100)

2 个答案:

答案 0 :(得分:0)

在这一行

while(!input.next().equals("stop")) 

如果用户未输入stop会怎样? - 他/她仍然输入了一些东西......

答案 1 :(得分:0)

您只是忘记将产品价格作为while循环中的输入,您将其作为字符串。