有没有办法将其转换为" while循环"或者"做while while循环"?

时间:2015-04-18 04:35:48

标签: java

我正在尝试将此代码转换为while循环或do while循环。我不太了解对这个问题的最佳理解。有人可以帮忙吗?

这是我试图为其创建循环的项目。我不确定循环会用于什么:

Barking Lot是一个狗寄宿设施。它提供任何天数和登机 其他服务,如梳理和散步。 Barking Lot目前有三个 员工,40名狗客户名单,以及任何时候登上八只狗的空间。一个要求 程序必须能够估算一天的利润。该设施一次可以登上八只狗;它 对于体重超过50磅的狗每天收费25美元,对于小型犬每天收费20美元。该 设施的费用包括每只狗每天2美元的食物(无论狗的大小)和30美元 公用事业每天。为程序开发逻辑,并实现允许程序的程序 用户输入登上大型犬的数量;假设其余的都是小型犬而且那个 设施已满。产出是当天收取的总收入,总支出和 区别。允许用户一次请求无限数量的利润估算

这是我的代码:

import java.util.Scanner;

public class Bark {
  public static void main(String[] args) {
    Scanner Scanner = new Scanner(System.in);
    int x = 20;
    int y = 25;
    int Small = 0;
    int Large = 0;

    System.out.println("Enter number of small dogs: ");
    Small = Scanner.nextInt();
    System.out.println("Enter number of large dogs: ");
    Large = Scanner.nextInt();

    int Revenue = ((Small * x) + (Large * y));
    int Food = ((Small + Large) * (2));
    int Facility = 30;
    int Expenses = (Food + Facility);
    int Difference = (Revenue - Expenses);

    if ((Small + Large) <= 8) {
      System.out.println("Revenue is " + ((Small * x) + (Large * y)));
      System.out.println("Expenses = " + (Food + Facility));
      System.out.println("Difference = " + (Revenue - Expenses));

    } else
      System.out.println("The number of dogs has exceeded the facility limit.");
  }
}

3 个答案:

答案 0 :(得分:1)

不知道你想要循环的代码的确切部分(我的意思是你想要实现的结果)。所以我会留下一个编码风格的建议,这可能会在不久的将来帮助你! ; - )

    // Comment your code. You are going to need it and avoid some people
    // swearing at your code. :-)

    // Always use first letter lowercase and other words in upper when
    // programming in Java (variables, methods). Upper for classes.
    int variableName = 0;
    public class Bark { ... }
    int final MY_CONSTANT = 0;
    public void myMethod() { ... }

    // Not good to use variables like "x". You will easily loose track of
    // what they mean.
    int valueOfSomething = 20;

如果你想要的是重新处理(比如在完成后再次运行代码),这就是你可以做的。

    public class Bark {
       public static void main(String[] args) {

           // Declare variables
           Scanner scanner = new Scanner(System.in);
           int valueSmall = 20;
           int valueLarge = 25;
           int small = 0;
           int large = 0;
           int quit = 0;

           while (quit == 0) {
               // Ask for user input
               System.out.println("Enter number of small dogs: ");
               small = scanner.nextInt();
               System.out.println("Enter number of large dogs: ");
               large = scanner.nextInt();

               // Perform accountability
               int revenue = ((small * valueSmall) + (large * valueLarge));
               int food = ((small + large) * (2));
               int facility = 30;
               int expenses = (food + facility);
               int difference = (revenue - expenses);

               if ((small + large) <= 8) {
                   System.out.println("Revenue is " + revenue);
                   System.out.println("Expenses = " + expenses);
                   System.out.println("Difference = " + difference);

               } else {
                   System.out.println("The number of dogs has exceeded the facility limit.");
               }

               // Ask if user wants to quit (No input validation!)
               System.out.println("Quit? Enter 0 for NO or 1 for YES:");
               quit = scanner.nextInt();
           }

           System.out.println("Goodbye");
        }
    }

累积狗数量的另一种方法,直到达到极限。

    public class Bark {
       public static void main(String[] args) {

           // Declare variables
           Scanner scanner = new Scanner(System.in);
           int valueSmall = 20;
           int valueLarge = 25;
           int small = 0;
           int large = 0;

           while (small + large <= 8) {
               // Ask for user input, notice I am accumulating using +=
               System.out.println("Enter number of small dogs: ");
               small += scanner.nextInt();
               System.out.println("Enter number of large dogs: ");
               large += scanner.nextInt();

               // Perform accountability
               int revenue = ((small * valueSmall) + (large * valueLarge));
               int food = ((small + large) * (2));
               int facility = 30;
               int expenses = (food + facility);
               int difference = (revenue - expenses);

               // Print the report
               System.out.println("Revenue is " + revenue);
               System.out.println("Expenses = " + expenses);
               System.out.println("Difference = " + difference);
           }

           // If number exceeds the limit, your loop breaks and you print the message.
           System.out.println("The number of dogs has exceeded the facility limit.");
           System.out.println("Goodbye");
        }
    }

答案 1 :(得分:0)

看起来你想退出一次小+大&gt; 8,这应该是你的循环条件:

import java.util.Scanner;

public class Bark {
    public static void main(String[] args) {
        Scanner Scanner = new Scanner(System.in);
        int x = 20;
        int y = 25;
        int Small = 0;
        int Large = 0;

        while (Small + Large <= 8) {
            System.out.println("Enter number of small dogs: ");
            Small = Scanner.nextInt();
            System.out.println("Enter number of large dogs: ");
            Large = Scanner.nextInt();

            int Revenue = ((Small * x) + (Large * y));
            int Food = ((Small + Large) * (2));
            int Facility = 30;
            int Expenses = (Food + Facility);
            int Difference = (Revenue - Expenses);
            if ((Small + Large) <= 8) {
                System.out.println("Revenue is " + ((Small * x) + (Large * y)));
                System.out.println("Expenses = " + (Food + Facility));
                System.out.println("Difference = " + (Revenue - Expenses));
            }
        }

    System.out.println("The number of dogs has exceeded the facility limit.");
    }
}

以下是测试结果:

> javac Bark.java

> java Bark
Enter number of small dogs:
1
Enter number of large dogs:
2
Revenue is 70
Expenses = 36
Difference = 34
Enter number of small dogs:
4
Enter number of large dogs:
4
Revenue is 180
Expenses = 46
Difference = 134
Enter number of small dogs:
4
Enter number of large dogs:
5
The number of dogs has exceeded the facility limit.

答案 2 :(得分:0)

在我看来,你真正使用循环的唯一地方就是如上所述,当没有更多的空间登上狗时,或者因为问题说明了#34;允许用户请求无限制一次的利润估计数&#34; 对于那个,你只需指定一个关闭程序的变量。但是,如果您希望程序始终至少运行一次,那么在该情况下使用do-while循环可能会更好。对于以前的家伙回答,while循环就足够了,因为如果你已经有40只狗在登机时你不想运行它。

<强> ::::: EDIT ::::: 阅读你的问题,很清楚你错过了原始答案中的一些内容。 它接近结束时首先关闭它只能进入大型犬,所以 SmallDogsBoarded = 8 - numberOfLargeDogsBoarded (但是我可能会将8变成一个变量,因此公司可以获得8只以上的狗未来),所以声明一个变量&#39; maxBoardedDogs = 8 &#39;因此 smallDogsBoarded = maxBoardedDogs - numberOfLargeDogsBoarded

转到实际问题,如果你想让它成为一个循环,我会做类似的事情

do{
  print line to user 'enter number of large dogs boarded or '-1' to quit'
  get numberOfLargeDogsBoarded

  */* say they enter something like a '-1' for quit, or a 47, in this case numberOfLargeDogsBoarded --MUST-- be somewhere between 0 and maxBoardedDogs (0-8 here) */*
  if(numberofLargeDogsBoarded is valid){
     calculate smallDogsBoarded(*explained above*)
     calculate profits using a formula
else 
     prompt user to re-enter the numberOfLargeDogsBoarded as it wasnt -1 and it WASNT between 0-8
     get user answer, calculate the profits.
}(while numberOfLargeDogsBoarded not equal to -1[our escape sequence])