【问题标题】:Java: Combining the inputs of a while loopJava:组合while循环的输入
【发布时间】:2015-10-21 10:26:49
【问题描述】:

如何在 while 循环下组合输入?


 try {
  System.out.println("Keep selecting the ingrients that you want until you                 press (0)");
  int ingrientsID = Integer.parseInt(br.readLine());
  while (!"0".equals(ingrientsID )) {
  System.out.println("One more?");
  ingrientsID = Integer.parseInt(br.readLine());
  if (ingrientsID == 0) {
  break; 
  } 
  }

【问题讨论】:

  • while (!"0".equals(ingrientsID ))?首先你的问题是什么
  • 请澄清问题。你想对输入做什么?你会加减乘等......
  • 你的问题不清楚......它已经在做打算做的事情
  • 我投票结束这个问题,因为不清楚你在问什么。请澄清您的具体问题或添加其他详细信息以准确突出您的需求。
  • “combine”是指“串联”还是“加法”?当您尝试阅读Integer时,它似乎对我来说很重要,这应该很简单。

标签: java loops input while-loop


【解决方案1】:

您正在尝试执行以下操作:

int ingrientsID = Integer.parseInt(br.readLine());
while (ingrientsID != 0) {
    System.out.println("One more?");
    ingrientsID = Integer.parseInt(br.readLine());
    // no breaks needed for ingrientsID == 0 as loop condition will 
    // automatic stop iterate for ingrientsID == 0
    // do more stuffs
} 

【讨论】:

  • 你忘了添加配料:/
【解决方案2】:
  boolean flag=true;

  System.out.println("Keep selecting the ingrients that you want until you                 press (0)");
  int ingrientsID = 0,temp=0;
  while (flag) {//until user enters 0  
  temp=Integer.parseInt(br.readLine());
  ingrientsID += temp;//to sum each input that you type
  if (temp==0) {//is user enters 0 then
  flag=false;
  }
  else
  System.out.println("One more?"); 
}
System.out.println("sum: "+ingrientsID);//final result 

【讨论】:

    【解决方案3】:

    这里是代码和一些cmets。

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    //import java.util.Scanner;
    
    public class Ingredients {
    
        public static void main(String [] args)throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            //or Scanner scan = new Scanner(System.in);
            int ingredientID=0;
            int sumOfIngredients=0;
    
    
            System.out.println("Keep selecting the ingrients that you want until you press (0)");
    
            //use do while to make it run at least once
            do{
                //get input, add to sumofIngredients
                ingredientID=Integer.parseInt(br.readLine());
            //or ingredientID= scan.nextInt(); if you're using scanner
    
                //add the ingredient
                sumOfIngredients+=ingredientID;
    
           //I don't think you need to ask "one more?". 
    //You have given the instruction (to keep selecting the ingredients till the user presses 0 )
            }while(ingredientID!=0);
    
                //print the ingredients!
            System.out.println("sum of ingredients: "+ sumOfIngredients);           
        }
    }
    

    下次请不要忘记使用“作业”标签;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2018-03-23
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多